33 * @brief UTC offset handling utilities (validation, conversion, parsing).
44 *
55 */
6- /* Copyright (C) 2019-2025 by Arjan van Vught mailto:info@gd32-dmx.org
6+ /* Copyright (C) 2019-2026 by Arjan van Vught mailto:info@gd32-dmx.org
77 *
88 * Permission is hereby granted, free of charge, to any person obtaining a copy
99 * of this software and associated documentation files (the "Software"), to deal
3232 * @namespace global
3333 * @brief Holds global UTC offset variable used system-wide.
3434 */
35- namespace global
36- {
35+ namespace global {
3736/* *
3837 * @brief The current UTC offset in seconds.
3938 */
4039extern int32_t g_utc_offset;
4140} // namespace global
4241
43- namespace hal ::utc
44- {
42+ namespace utc {
4543
4644constexpr int32_t kUtcOffsetMin = -12 ; // /< Minimum valid UTC offset (hours)
4745constexpr int32_t kUtcOffsetMax = 14 ; // /< Maximum valid UTC offset (hours)
@@ -50,8 +48,7 @@ constexpr int32_t kUtcOffsetMax = 14; ///< Maximum valid UTC offset (hours)
5048 * @struct Offset
5149 * @brief Represents a fractional UTC offset.
5250 */
53- struct Offset
54- {
51+ struct Offset {
5552 int32_t hours; // /< Signed hour component
5653 uint32_t minutes; // /< Unsigned minute component
5754};
@@ -68,19 +65,14 @@ constexpr Offset kValidOffsets[] = {{-9, 30}, {-3, 30}, {3, 30}, {4, 30}, {5, 30
6865 * @param[out] utc_offset_seconds Resulting offset in seconds
6966 * @return true if valid; false otherwise
7067 */
71- inline bool ValidateOffset (int32_t hours, uint32_t minutes, int32_t & utc_offset_seconds)
72- {
73- if (hours >= kUtcOffsetMin && hours <= kUtcOffsetMax )
74- {
75- if (minutes == 0 )
76- {
68+ inline bool ValidateOffset (int32_t hours, uint32_t minutes, int32_t & utc_offset_seconds) {
69+ if (hours >= kUtcOffsetMin && hours <= kUtcOffsetMax ) {
70+ if (minutes == 0 ) {
7771 utc_offset_seconds = hours * 3600 ;
7872 return true ;
7973 }
80- for (const auto & offset : kValidOffsets )
81- {
82- if (offset.hours == hours && offset.minutes == minutes)
83- {
74+ for (const auto & offset : kValidOffsets ) {
75+ if (offset.hours == hours && offset.minutes == minutes) {
8476 utc_offset_seconds = (hours >= 0 ) ? (hours * 3600 + static_cast <int32_t >(minutes) * 60 ) : (hours * 3600 - static_cast <int32_t >(minutes) * 60 );
8577 return true ;
8678 }
@@ -94,22 +86,17 @@ inline bool ValidateOffset(int32_t hours, uint32_t minutes, int32_t& utc_offset_
9486 * @param utc_offset_seconds UTC offset in seconds
9587 * @return true if offset is valid; false otherwise
9688 */
97- inline bool IsValidOffset (int32_t utc_offset_seconds)
98- {
89+ inline bool IsValidOffset (int32_t utc_offset_seconds) {
9990 if (utc_offset_seconds == 0 ) return true ;
10091 int32_t hours = utc_offset_seconds / 3600 ;
101- uint32_t minutes = (utc_offset_seconds >= 0 ) ? static_cast <uint32_t >(utc_offset_seconds - hours * 3600 ) / 60
102- : static_cast <uint32_t >((hours * 3600 - utc_offset_seconds)) / 60 ;
92+ uint32_t minutes = (utc_offset_seconds >= 0 ) ? static_cast <uint32_t >(utc_offset_seconds - hours * 3600 ) / 60 : static_cast <uint32_t >((hours * 3600 - utc_offset_seconds)) / 60 ;
10393
104- if (minutes == 0 && hours >= kUtcOffsetMin && hours <= kUtcOffsetMax )
105- {
94+ if (minutes == 0 && hours >= kUtcOffsetMin && hours <= kUtcOffsetMax ) {
10695 return true ;
10796 }
10897
109- for (const auto & offset : kValidOffsets )
110- {
111- int32_t offset_seconds = (offset.hours >= 0 ) ? offset.hours * 3600 + static_cast <int32_t >(offset.minutes * 60 )
112- : offset.hours * 3600 - static_cast <int32_t >(offset.minutes * 60 );
98+ for (const auto & offset : kValidOffsets ) {
99+ int32_t offset_seconds = (offset.hours >= 0 ) ? offset.hours * 3600 + static_cast <int32_t >(offset.minutes * 60 ) : offset.hours * 3600 - static_cast <int32_t >(offset.minutes * 60 );
113100 if (utc_offset_seconds == offset_seconds) return true ;
114101 }
115102 return false ;
@@ -121,15 +108,11 @@ inline bool IsValidOffset(int32_t utc_offset_seconds)
121108 * @param[out] hours Signed hour component
122109 * @param[out] minutes Unsigned minute component
123110 */
124- inline void SplitOffset (int32_t utc_offset_seconds, int32_t & hours, uint32_t & minutes)
125- {
111+ inline void SplitOffset (int32_t utc_offset_seconds, int32_t & hours, uint32_t & minutes) {
126112 hours = utc_offset_seconds / 3600 ;
127- if (utc_offset_seconds >= 0 )
128- {
113+ if (utc_offset_seconds >= 0 ) {
129114 minutes = static_cast <uint32_t >((utc_offset_seconds - hours * 3600 ) / 60 );
130- }
131- else
132- {
115+ } else {
133116 minutes = static_cast <uint32_t >(((hours * 3600 ) - utc_offset_seconds) / 60 );
134117 }
135118}
@@ -142,15 +125,12 @@ inline void SplitOffset(int32_t utc_offset_seconds, int32_t& hours, uint32_t& mi
142125 * @param[out] minutes Unsigned minute component
143126 * @return true if parse was successful and valid; false otherwise
144127 */
145- inline bool ParseOffset (const char * buffer, uint32_t buffer_length, int32_t & hours, uint32_t & minutes)
146- {
128+ inline bool ParseOffset (const char * buffer, uint32_t buffer_length, int32_t & hours, uint32_t & minutes) {
147129 if (buffer == nullptr ) return false ;
148130
149- if (buffer_length == 5 )
150- {
131+ if (buffer_length == 5 ) {
151132 static constexpr const char kZeroOffset [5 ] = {' 0' , ' 0' , ' :' , ' 0' , ' 0' };
152- if (memcmp (kZeroOffset , buffer, sizeof (kZeroOffset )) == 0 )
153- {
133+ if (memcmp (kZeroOffset , buffer, sizeof (kZeroOffset )) == 0 ) {
154134 hours = 0 ;
155135 minutes = 0 ;
156136 return true ;
@@ -179,6 +159,6 @@ inline bool ParseOffset(const char* buffer, uint32_t buffer_length, int32_t& hou
179159 int32_t dummy;
180160 return ValidateOffset (hours, minutes, dummy);
181161}
182- } // namespace hal:: utc
162+ } // namespace utc
183163
184164#endif // UTC_H_
0 commit comments