Update actions to ubuntu-latest (#14114)
[betaflight.git] / src / main / common / time.h
blob33121739ab04255e937eec8f29a4d2b1a2ba1d79
1 /*
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)
8 * any later version.
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/>.
21 #pragma once
23 #include <stdbool.h>
24 #include <stdint.h>
26 #include "platform.h"
28 #include "pg/pg.h"
30 // time difference, 32 bits always sufficient
31 typedef int32_t timeDelta_t;
32 // millisecond time
33 typedef uint32_t timeMs_t ;
34 // microsecond time
35 #ifdef USE_64BIT_TIME
36 typedef uint64_t timeUs_t;
37 #define TIMEUS_MAX UINT64_MAX
38 #else
39 typedef uint32_t timeUs_t;
40 #define TIMEUS_MAX UINT32_MAX
41 #endif
43 #define TIMEZONE_OFFSET_MINUTES_MIN -780 // -13 hours
44 #define TIMEZONE_OFFSET_MINUTES_MAX 780 // +13 hours
46 #define SECONDS_PER_MINUTE 60.0f
48 static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_t)(a - b); }
49 static inline int32_t cmpTimeCycles(uint32_t a, uint32_t b) { return (int32_t)(a - b); }
51 #define FORMATTED_DATE_TIME_BUFSIZE 30
53 #ifdef USE_RTC_TIME
55 typedef struct timeConfig_s {
56 int16_t tz_offsetMinutes; // Offset from UTC in minutes, might be positive or negative
57 } timeConfig_t;
59 PG_DECLARE(timeConfig_t, timeConfig);
61 // Milliseconds since Jan 1 1970
62 typedef int64_t rtcTime_t;
64 rtcTime_t rtcTimeMake(int32_t secs, uint16_t millis);
65 int32_t rtcTimeGetSeconds(const rtcTime_t *t);
66 uint16_t rtcTimeGetMillis(const rtcTime_t *t);
68 typedef struct _dateTime_s {
69 // full year
70 uint16_t year;
71 // 1-12
72 uint8_t month;
73 // 1-31
74 uint8_t day;
75 // 0-23
76 uint8_t hours;
77 // 0-59
78 uint8_t minutes;
79 // 0-59
80 uint8_t seconds;
81 // 0-999
82 uint16_t millis;
83 } dateTime_t;
85 // buf must be at least FORMATTED_DATE_TIME_BUFSIZE
86 bool dateTimeFormatUTC(char *buf, dateTime_t *dt);
87 bool dateTimeFormatLocal(char *buf, dateTime_t *dt);
88 bool dateTimeFormatLocalShort(char *buf, dateTime_t *dt);
90 void dateTimeUTCToLocal(dateTime_t *utcDateTime, dateTime_t *localDateTime);
91 // dateTimeSplitFormatted splits a formatted date into its date
92 // and time parts. Note that the string pointed by formatted will
93 // be modified and will become invalid after calling this function.
94 bool dateTimeSplitFormatted(char *formatted, char **date, char **time);
96 bool rtcHasTime(void);
98 bool rtcGet(rtcTime_t *t);
99 bool rtcSet(const rtcTime_t *t);
101 bool rtcGetDateTime(dateTime_t *dt);
102 bool rtcSetDateTime(dateTime_t *dt);
104 void rtcPersistWrite(int16_t offsetMinutes);
105 bool rtcPersistRead(rtcTime_t *t);
107 #endif // USE_RTC_TIME