2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
30 // time difference, 32 bits always sufficient
31 typedef int32_t timeDelta_t
;
33 typedef uint32_t timeMs_t
;
36 typedef uint64_t timeUs_t
;
37 #define TIMEUS_MAX UINT64_MAX
39 typedef uint32_t timeUs_t
;
40 #define TIMEUS_MAX UINT32_MAX
43 #define TIMEZONE_OFFSET_MINUTES_MIN -780 // -13 hours
44 #define TIMEZONE_OFFSET_MINUTES_MAX 780 // +13 hours
46 static inline timeDelta_t
cmpTimeUs(timeUs_t a
, timeUs_t b
) { return (timeDelta_t
)(a
- b
); }
47 static inline int32_t cmpTimeCycles(uint32_t a
, uint32_t b
) { return (int32_t)(a
- b
); }
49 #define FORMATTED_DATE_TIME_BUFSIZE 30
53 typedef struct timeConfig_s
{
54 int16_t tz_offsetMinutes
; // Offset from UTC in minutes, might be positive or negative
57 PG_DECLARE(timeConfig_t
, timeConfig
);
59 // Milliseconds since Jan 1 1970
60 typedef int64_t rtcTime_t
;
62 rtcTime_t
rtcTimeMake(int32_t secs
, uint16_t millis
);
63 int32_t rtcTimeGetSeconds(rtcTime_t
*t
);
64 uint16_t rtcTimeGetMillis(rtcTime_t
*t
);
66 typedef struct _dateTime_s
{
83 // buf must be at least FORMATTED_DATE_TIME_BUFSIZE
84 bool dateTimeFormatUTC(char *buf
, dateTime_t
*dt
);
85 bool dateTimeFormatLocal(char *buf
, dateTime_t
*dt
);
86 bool dateTimeFormatLocalShort(char *buf
, dateTime_t
*dt
);
88 void dateTimeUTCToLocal(dateTime_t
*utcDateTime
, dateTime_t
*localDateTime
);
89 // dateTimeSplitFormatted splits a formatted date into its date
90 // and time parts. Note that the string pointed by formatted will
91 // be modified and will become invalid after calling this function.
92 bool dateTimeSplitFormatted(char *formatted
, char **date
, char **time
);
94 bool rtcHasTime(void);
96 bool rtcGet(rtcTime_t
*t
);
97 bool rtcSet(rtcTime_t
*t
);
99 bool rtcGetDateTime(dateTime_t
*dt
);
100 bool rtcSetDateTime(dateTime_t
*dt
);
102 void rtcPersistWrite(int16_t offsetMinutes
);
103 bool rtcPersistRead(rtcTime_t
*t
);