optimize math (#5287)
[betaflight.git] / src / main / common / time.h
blob813e4a4f33e55cd6198b6510d39e14db760fd875
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include <stdbool.h>
21 #include <stdint.h>
23 #include "platform.h"
25 #include "pg/pg.h"
27 // time difference, 32 bits always sufficient
28 typedef int32_t timeDelta_t;
29 // millisecond time
30 typedef uint32_t timeMs_t ;
31 // microsecond time
32 #ifdef USE_64BIT_TIME
33 typedef uint64_t timeUs_t;
34 #define TIMEUS_MAX UINT64_MAX
35 #else
36 typedef uint32_t timeUs_t;
37 #define TIMEUS_MAX UINT32_MAX
38 #endif
40 static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_t)(a - b); }
42 #define FORMATTED_DATE_TIME_BUFSIZE 30
44 #ifdef USE_RTC_TIME
46 typedef struct timeConfig_s {
47 int16_t tz_offsetMinutes; // Offset from UTC in minutes, might be positive or negative
48 } timeConfig_t;
50 PG_DECLARE(timeConfig_t, timeConfig);
52 // Milliseconds since Jan 1 1970
53 typedef int64_t rtcTime_t;
55 rtcTime_t rtcTimeMake(int32_t secs, uint16_t millis);
56 int32_t rtcTimeGetSeconds(rtcTime_t *t);
57 uint16_t rtcTimeGetMillis(rtcTime_t *t);
59 typedef struct _dateTime_s {
60 // full year
61 uint16_t year;
62 // 1-12
63 uint8_t month;
64 // 1-31
65 uint8_t day;
66 // 0-23
67 uint8_t hours;
68 // 0-59
69 uint8_t minutes;
70 // 0-59
71 uint8_t seconds;
72 // 0-999
73 uint16_t millis;
74 } dateTime_t;
76 // buf must be at least FORMATTED_DATE_TIME_BUFSIZE
77 bool dateTimeFormatUTC(char *buf, dateTime_t *dt);
78 bool dateTimeFormatLocal(char *buf, dateTime_t *dt);
79 bool dateTimeFormatLocalShort(char *buf, dateTime_t *dt);
81 void dateTimeUTCToLocal(dateTime_t *utcDateTime, dateTime_t *localDateTime);
82 // dateTimeSplitFormatted splits a formatted date into its date
83 // and time parts. Note that the string pointed by formatted will
84 // be modifed and will become invalid after calling this function.
85 bool dateTimeSplitFormatted(char *formatted, char **date, char **time);
87 bool rtcHasTime(void);
89 bool rtcGet(rtcTime_t *t);
90 bool rtcSet(rtcTime_t *t);
92 bool rtcGetDateTime(dateTime_t *dt);
93 bool rtcSetDateTime(dateTime_t *dt);
95 #endif