Use printf for LOG_DEBUG in sitl.
[inav.git] / src / main / common / time.h
blob82f29860777fc19d8a09d50f653985185d663d03
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 "config/parameter_group.h"
27 // time difference, signed 32 bits of microseconds overflows at ~35 minutes
28 // this is worth leaving as int32_t for performance reasons, use timeDeltaLarge_t for deltas that can be big
29 typedef int32_t timeDelta_t;
30 #define TIMEDELTA_MAX INT32_MAX
32 // time difference large, signed 64 bits of microseconds overflows at ~300000 years
33 typedef int64_t timeDeltaLarge_t;
34 #define TIMEDELTALARGE_MAX INT64_MAX
36 // millisecond time
37 typedef uint32_t timeMs_t;
38 #define TIMEMS_MAX UINT32_MAX
40 // microsecond time
41 #ifdef USE_64BIT_TIME
42 typedef uint64_t timeUs_t;
43 #define TIMEUS_MAX UINT64_MAX
44 #else
45 typedef uint32_t timeUs_t;
46 #define TIMEUS_MAX UINT32_MAX
47 #endif
49 // Constants for better readability
50 #define MILLISECS_PER_SEC 1000
51 #define USECS_PER_SEC (1000 * 1000)
53 #define HZ2US(hz) (1000000 / (hz))
54 #define HZ2MS(hz) (1000 / (hz))
55 #define US2S(us) ((us) * 1e-6f)
56 #define US2MS(us) ((us) * 1e-3f)
57 #define MS2US(ms) ((ms) * 1000)
58 #define MS2S(ms) ((ms) * 1e-3f)
59 #define S2MS(s) ((s) * MILLISECS_PER_SEC)
60 #define DS2MS(ds) ((ds) * 100)
61 #define HZ2S(hz) US2S(HZ2US(hz))
63 // Use this function only to get small deltas (difference overflows at ~35 minutes)
64 static inline timeDelta_t cmpTimeUs(timeUs_t a, timeUs_t b) { return (timeDelta_t)(a - b); }
66 typedef enum {
67 TZ_AUTO_DST_OFF,
68 TZ_AUTO_DST_EU,
69 TZ_AUTO_DST_USA,
70 } tz_automatic_dst_e;
72 typedef struct timeConfig_s {
73 int16_t tz_offset; // Offset from UTC in minutes, might be positive or negative
74 uint8_t tz_automatic_dst; // Automatically handle DST or ignore it, values come from tz_automatic_dst_e
75 } timeConfig_t;
77 PG_DECLARE(timeConfig_t, timeConfig);
79 // Milliseconds since Jan 1 1970
80 typedef int64_t rtcTime_t;
82 rtcTime_t rtcTimeMake(int32_t secs, uint16_t millis);
83 int32_t rtcTimeGetSeconds(rtcTime_t *t);
84 uint16_t rtcTimeGetMillis(rtcTime_t *t);
86 typedef struct _dateTime_s {
87 // full year
88 uint16_t year;
89 // 1-12
90 uint8_t month;
91 // 1-31
92 uint8_t day;
93 // 0-23
94 uint8_t hours;
95 // 0-59
96 uint8_t minutes;
97 // 0-59
98 uint8_t seconds;
99 // 0-999
100 uint16_t millis;
101 } dateTime_t;
103 #define FORMATTED_DATE_TIME_BUFSIZE 30
105 // buf must be at least FORMATTED_DATE_TIME_BUFSIZE
106 bool dateTimeFormatUTC(char *buf, dateTime_t *dt);
107 bool dateTimeFormatLocal(char *buf, dateTime_t *dt);
109 void dateTimeUTCToLocal(dateTime_t *localDateTime, const dateTime_t *utcDateTime);
110 // dateTimeSplitFormatted splits a formatted date into its date
111 // and time parts. Note that the string pointed by formatted will
112 // be modifed and will become invalid after calling this function.
113 bool dateTimeSplitFormatted(char *formatted, char **date, char **time);
115 bool rtcHasTime(void);
117 bool rtcGet(rtcTime_t *t);
118 bool rtcSet(rtcTime_t *t);
120 bool rtcGetDateTime(dateTime_t *dt);
121 bool rtcGetDateTimeLocal(dateTime_t *dt);
122 bool rtcSetDateTime(dateTime_t *dt);