Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / common / time.c
blob67d46e3060604c0007d80b85cf98f9f00ba5549b
1 /*
2 * This file is part of Cleanflight, Betaflight and INAV.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
6 * You can obtain one at http://mozilla.org/MPL/2.0/.
8 * Alternatively, the contents of this file may be used under the terms
9 * of the GNU General Public License Version 3, as described below:
11 * This file is free software: you may copy, redistribute and/or modify
12 * it under the terms of the GNU General Public License as published by the
13 * Free Software Foundation, either version 3 of the License, or (at your
14 * option) any later version.
16 * This file is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program. If not, see http://www.gnu.org/licenses/.
24 * @author Alberto Garcia Hierro <alberto@garciahierro.com>
27 #include <stdint.h>
29 #include "platform.h"
31 #include "common/maths.h"
32 #include "common/printf.h"
33 #include "common/time.h"
35 #include "drivers/persistent.h"
37 #include "pg/pg_ids.h"
39 #include "drivers/time.h"
41 #ifdef USE_RTC_TIME
43 // For the "modulo 4" arithmetic to work, we need a leap base year
44 #define REFERENCE_YEAR 2000
45 // Offset (seconds) from the UNIX epoch (1970-01-01) to 2000-01-01
46 #define EPOCH_2000_OFFSET 946684800
48 #define MILLIS_PER_SECOND 1000
50 // rtcTime_t when the system was started.
51 // Calculated in rtcSet().
52 static rtcTime_t started = 0;
54 static const uint16_t days[4][12] =
56 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
57 { 366, 397, 425, 456, 486, 517, 547, 578, 609, 639, 670, 700},
58 { 731, 762, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065},
59 {1096, 1127, 1155, 1186, 1216, 1247, 1277, 1308, 1339, 1369, 1400, 1430},
62 PG_REGISTER_WITH_RESET_TEMPLATE(timeConfig_t, timeConfig, PG_TIME_CONFIG, 0);
64 PG_RESET_TEMPLATE(timeConfig_t, timeConfig,
65 .tz_offsetMinutes = 0,
68 static rtcTime_t dateTimeToRtcTime(dateTime_t *dt)
70 unsigned int second = dt->seconds; // 0-59
71 unsigned int minute = dt->minutes; // 0-59
72 unsigned int hour = dt->hours; // 0-23
73 unsigned int day = dt->day - 1; // 0-30
74 unsigned int month = dt->month - 1; // 0-11
75 unsigned int year = dt->year - REFERENCE_YEAR; // 0-99
76 int32_t unixTime = (((year / 4 * (365 * 4 + 1) + days[year % 4][month] + day) * 24 + hour) * 60 + minute) * 60 + second + EPOCH_2000_OFFSET;
77 return rtcTimeMake(unixTime, dt->millis);
80 static void rtcTimeToDateTime(dateTime_t *dt, rtcTime_t t)
82 int32_t unixTime = t / MILLIS_PER_SECOND - EPOCH_2000_OFFSET;
83 dt->seconds = unixTime % 60;
84 unixTime /= 60;
85 dt->minutes = unixTime % 60;
86 unixTime /= 60;
87 dt->hours = unixTime % 24;
88 unixTime /= 24;
90 unsigned int years = unixTime / (365 * 4 + 1) * 4;
91 unixTime %= 365 * 4 + 1;
93 unsigned int year;
94 for (year = 3; year > 0; year--) {
95 if (unixTime >= days[year][0]) {
96 break;
100 unsigned int month;
101 for (month = 11; month > 0; month--) {
102 if (unixTime >= days[year][month]) {
103 break;
107 dt->year = years + year + REFERENCE_YEAR;
108 dt->month = month + 1;
109 dt->day = unixTime - days[year][month] + 1;
110 dt->millis = t % MILLIS_PER_SECOND;
113 static void rtcGetDefaultDateTime(dateTime_t *dateTime)
115 dateTime->year = 0;
116 dateTime->month = 1;
117 dateTime->day = 1;
118 dateTime->hours = 0;
119 dateTime->minutes = 0;
120 dateTime->seconds = 0;
121 dateTime->millis = 0;
124 static bool rtcIsDateTimeValid(dateTime_t *dateTime)
126 return (dateTime->year >= REFERENCE_YEAR) &&
127 (dateTime->month >= 1 && dateTime->month <= 12) &&
128 (dateTime->day >= 1 && dateTime->day <= 31) &&
129 (dateTime->hours <= 23) &&
130 (dateTime->minutes <= 59) &&
131 (dateTime->seconds <= 59) &&
132 (dateTime->millis <= 999);
135 static void dateTimeWithOffset(dateTime_t *dateTimeOffset, dateTime_t *dateTimeInitial, int16_t minutes)
137 rtcTime_t initialTime = dateTimeToRtcTime(dateTimeInitial);
138 rtcTime_t offsetTime = rtcTimeMake(rtcTimeGetSeconds(&initialTime) + minutes * 60, rtcTimeGetMillis(&initialTime));
139 rtcTimeToDateTime(dateTimeOffset, offsetTime);
142 static bool dateTimeFormat(char *buf, dateTime_t *dateTime, int16_t offsetMinutes, bool shortVersion)
144 dateTime_t local;
146 int tz_hours = 0;
147 int tz_minutes = 0;
148 bool retVal = true;
150 // Apply offset if necessary
151 if (offsetMinutes != 0) {
152 tz_hours = offsetMinutes / 60;
153 tz_minutes = ABS(offsetMinutes % 60);
154 dateTimeWithOffset(&local, dateTime, offsetMinutes);
155 dateTime = &local;
158 if (!rtcIsDateTimeValid(dateTime)) {
159 rtcGetDefaultDateTime(&local);
160 dateTime = &local;
161 retVal = false;
164 if (shortVersion) {
165 tfp_sprintf(buf, "%04u-%02u-%02u %02u:%02u:%02u",
166 dateTime->year, dateTime->month, dateTime->day,
167 dateTime->hours, dateTime->minutes, dateTime->seconds);
168 } else {
169 // Changes to this format might require updates in
170 // dateTimeSplitFormatted()
171 // Datetime is in ISO_8601 format, https://en.wikipedia.org/wiki/ISO_8601
172 tfp_sprintf(buf, "%04u-%02u-%02uT%02u:%02u:%02u.%03u%c%02d:%02d",
173 dateTime->year, dateTime->month, dateTime->day,
174 dateTime->hours, dateTime->minutes, dateTime->seconds, dateTime->millis,
175 tz_hours >= 0 ? '+' : '-', ABS(tz_hours), tz_minutes);
178 return retVal;
181 rtcTime_t rtcTimeMake(int32_t secs, uint16_t millis)
183 return ((rtcTime_t)secs) * MILLIS_PER_SECOND + millis;
186 int32_t rtcTimeGetSeconds(rtcTime_t *t)
188 return *t / MILLIS_PER_SECOND;
191 uint16_t rtcTimeGetMillis(rtcTime_t *t)
193 return *t % MILLIS_PER_SECOND;
196 bool dateTimeFormatUTC(char *buf, dateTime_t *dt)
198 return dateTimeFormat(buf, dt, 0, false);
201 bool dateTimeFormatLocal(char *buf, dateTime_t *dt)
203 const int16_t timezoneOffset = rtcIsDateTimeValid(dt) ? timeConfig()->tz_offsetMinutes : 0;
204 return dateTimeFormat(buf, dt, timezoneOffset, false);
207 bool dateTimeFormatLocalShort(char *buf, dateTime_t *dt)
209 return dateTimeFormat(buf, dt, timeConfig()->tz_offsetMinutes, true);
212 void dateTimeUTCToLocal(dateTime_t *utcDateTime, dateTime_t *localDateTime)
214 dateTimeWithOffset(localDateTime, utcDateTime, timeConfig()->tz_offsetMinutes);
217 bool dateTimeSplitFormatted(char *formatted, char **date, char **time)
219 // Just look for the T and replace it with a zero
220 // XXX: Keep in sync with dateTimeFormat()
221 for (char *p = formatted; *p; p++) {
222 if (*p == 'T') {
223 *date = formatted;
224 *time = (p+1);
225 *p = '\0';
226 return true;
229 return false;
232 bool rtcHasTime(void)
234 return started != 0;
237 bool rtcGet(rtcTime_t *t)
239 if (!rtcHasTime()) {
240 return false;
242 *t = started + millis();
243 return true;
246 bool rtcSet(rtcTime_t *t)
248 started = *t - millis();
249 return true;
252 bool rtcGetDateTime(dateTime_t *dt)
254 rtcTime_t t;
255 if (rtcGet(&t)) {
256 rtcTimeToDateTime(dt, t);
257 return true;
259 // No time stored, fill dt with 0000-01-01T00:00:00.000
260 rtcGetDefaultDateTime(dt);
261 return false;
264 bool rtcSetDateTime(dateTime_t *dt)
266 rtcTime_t t = dateTimeToRtcTime(dt);
267 return rtcSet(&t);
270 #if defined(USE_PERSISTENT_OBJECTS)
271 void rtcPersistWrite(int16_t offsetMinutes)
273 rtcTime_t workTime;
274 uint32_t highLongWord = 0;
275 uint32_t lowLongWord = 0;
276 if (rtcGet(&workTime)) {
277 workTime += (offsetMinutes * 60 * MILLIS_PER_SECOND);
278 highLongWord = (uint32_t)(workTime >> 32);
279 lowLongWord = (uint32_t)(workTime & 0xffffffff);
281 persistentObjectWrite(PERSISTENT_OBJECT_RTC_HIGH, highLongWord);
282 persistentObjectWrite(PERSISTENT_OBJECT_RTC_LOW, lowLongWord);
285 bool rtcPersistRead(rtcTime_t *t)
287 const uint32_t highLongWord = persistentObjectRead(PERSISTENT_OBJECT_RTC_HIGH);
288 const uint32_t lowLongWord = persistentObjectRead(PERSISTENT_OBJECT_RTC_LOW);
290 if ((highLongWord != 0) || (lowLongWord != 0)) {
291 *t = ((uint64_t)highLongWord << 32) + lowLongWord;
292 return true;
293 } else {
294 return false;
297 #endif
298 #endif // USE_RTC_TIME