optimize math (#5287)
[betaflight.git] / src / main / common / time.c
blob5940c4225f2028c7b828e3548cb6c6871891f4c4
1 /*
2 * This file is part of 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 "common/maths.h"
30 #include "common/printf.h"
31 #include "common/time.h"
33 #include "pg/pg_ids.h"
35 #include "drivers/time.h"
37 #ifdef USE_RTC_TIME
39 #define UNIX_REFERENCE_YEAR 1970
40 #define MILLIS_PER_SECOND 1000
42 // rtcTime_t when the system was started.
43 // Calculated in rtcSet().
44 static rtcTime_t started = 0;
46 static const uint16_t days[4][12] =
48 { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335},
49 { 366, 397, 425, 456, 486, 517, 547, 578, 609, 639, 670, 700},
50 { 731, 762, 790, 821, 851, 882, 912, 943, 974, 1004, 1035, 1065},
51 {1096, 1127, 1155, 1186, 1216, 1247, 1277, 1308, 1339, 1369, 1400, 1430},
54 PG_REGISTER_WITH_RESET_TEMPLATE(timeConfig_t, timeConfig, PG_TIME_CONFIG, 0);
56 PG_RESET_TEMPLATE(timeConfig_t, timeConfig,
57 .tz_offsetMinutes = 0,
60 static rtcTime_t dateTimeToRtcTime(dateTime_t *dt)
62 unsigned int second = dt->seconds; // 0-59
63 unsigned int minute = dt->minutes; // 0-59
64 unsigned int hour = dt->hours; // 0-23
65 unsigned int day = dt->day - 1; // 0-30
66 unsigned int month = dt->month - 1; // 0-11
67 unsigned int year = dt->year - UNIX_REFERENCE_YEAR; // 0-99
68 int32_t unixTime = (((year / 4 * (365 * 4 + 1) + days[year % 4][month] + day) * 24 + hour) * 60 + minute) * 60 + second;
69 return rtcTimeMake(unixTime, dt->millis);
72 static void rtcTimeToDateTime(dateTime_t *dt, rtcTime_t t)
74 int32_t unixTime = t / MILLIS_PER_SECOND;
75 dt->seconds = unixTime % 60;
76 unixTime /= 60;
77 dt->minutes = unixTime % 60;
78 unixTime /= 60;
79 dt->hours = unixTime % 24;
80 unixTime /= 24;
82 unsigned int years = unixTime / (365 * 4 + 1) * 4;
83 unixTime %= 365 * 4 + 1;
85 unsigned int year;
86 for (year = 3; year > 0; year--) {
87 if (unixTime >= days[year][0]) {
88 break;
92 unsigned int month;
93 for (month = 11; month > 0; month--) {
94 if (unixTime >= days[year][month]) {
95 break;
99 dt->year = years + year + UNIX_REFERENCE_YEAR;
100 dt->month = month + 1;
101 dt->day = unixTime - days[year][month] + 1;
102 dt->millis = t % MILLIS_PER_SECOND;
105 static void rtcGetDefaultDateTime(dateTime_t *dateTime)
107 dateTime->year = 0;
108 dateTime->month = 1;
109 dateTime->day = 1;
110 dateTime->hours = 0;
111 dateTime->minutes = 0;
112 dateTime->seconds = 0;
113 dateTime->millis = 0;
116 static bool rtcIsDateTimeValid(dateTime_t *dateTime)
118 return (dateTime->year >= UNIX_REFERENCE_YEAR) &&
119 (dateTime->month >= 1 && dateTime->month <= 12) &&
120 (dateTime->day >= 1 && dateTime->day <= 31) &&
121 (dateTime->hours <= 23) &&
122 (dateTime->minutes <= 59) &&
123 (dateTime->seconds <= 59) &&
124 (dateTime->millis <= 999);
127 static void dateTimeWithOffset(dateTime_t *dateTimeOffset, dateTime_t *dateTimeInitial, int16_t minutes)
129 rtcTime_t initialTime = dateTimeToRtcTime(dateTimeInitial);
130 rtcTime_t offsetTime = rtcTimeMake(rtcTimeGetSeconds(&initialTime) + minutes * 60, rtcTimeGetMillis(&initialTime));
131 rtcTimeToDateTime(dateTimeOffset, offsetTime);
134 static bool dateTimeFormat(char *buf, dateTime_t *dateTime, int16_t offsetMinutes, bool shortVersion)
136 dateTime_t local;
138 int tz_hours = 0;
139 int tz_minutes = 0;
140 bool retVal = true;
142 // Apply offset if necessary
143 if (offsetMinutes != 0) {
144 tz_hours = offsetMinutes / 60;
145 tz_minutes = ABS(offsetMinutes % 60);
146 dateTimeWithOffset(&local, dateTime, offsetMinutes);
147 dateTime = &local;
150 if (!rtcIsDateTimeValid(dateTime)) {
151 rtcGetDefaultDateTime(&local);
152 dateTime = &local;
153 retVal = false;
156 if (shortVersion) {
157 tfp_sprintf(buf, "%04u-%02u-%02u %02u:%02u:%02u",
158 dateTime->year, dateTime->month, dateTime->day,
159 dateTime->hours, dateTime->minutes, dateTime->seconds);
160 } else {
161 // Changes to this format might require updates in
162 // dateTimeSplitFormatted()
163 // Datetime is in ISO_8601 format, https://en.wikipedia.org/wiki/ISO_8601
164 tfp_sprintf(buf, "%04u-%02u-%02uT%02u:%02u:%02u.%03u%c%02d:%02d",
165 dateTime->year, dateTime->month, dateTime->day,
166 dateTime->hours, dateTime->minutes, dateTime->seconds, dateTime->millis,
167 tz_hours >= 0 ? '+' : '-', ABS(tz_hours), tz_minutes);
170 return retVal;
173 rtcTime_t rtcTimeMake(int32_t secs, uint16_t millis)
175 return ((rtcTime_t)secs) * MILLIS_PER_SECOND + millis;
178 int32_t rtcTimeGetSeconds(rtcTime_t *t)
180 return *t / MILLIS_PER_SECOND;
183 uint16_t rtcTimeGetMillis(rtcTime_t *t)
185 return *t % MILLIS_PER_SECOND;
188 bool dateTimeFormatUTC(char *buf, dateTime_t *dt)
190 return dateTimeFormat(buf, dt, 0, false);
193 bool dateTimeFormatLocal(char *buf, dateTime_t *dt)
195 return dateTimeFormat(buf, dt, timeConfig()->tz_offsetMinutes, false);
198 bool dateTimeFormatLocalShort(char *buf, dateTime_t *dt)
200 return dateTimeFormat(buf, dt, timeConfig()->tz_offsetMinutes, true);
203 void dateTimeUTCToLocal(dateTime_t *utcDateTime, dateTime_t *localDateTime)
205 dateTimeWithOffset(localDateTime, utcDateTime, timeConfig()->tz_offsetMinutes);
208 bool dateTimeSplitFormatted(char *formatted, char **date, char **time)
210 // Just look for the T and replace it with a zero
211 // XXX: Keep in sync with dateTimeFormat()
212 for (char *p = formatted; *p; p++) {
213 if (*p == 'T') {
214 *date = formatted;
215 *time = (p+1);
216 *p = '\0';
217 return true;
220 return false;
223 bool rtcHasTime(void)
225 return started != 0;
228 bool rtcGet(rtcTime_t *t)
230 if (!rtcHasTime()) {
231 return false;
233 *t = started + millis();
234 return true;
237 bool rtcSet(rtcTime_t *t)
239 started = *t - millis();
240 return true;
243 bool rtcGetDateTime(dateTime_t *dt)
245 rtcTime_t t;
246 if (rtcGet(&t)) {
247 rtcTimeToDateTime(dt, t);
248 return true;
250 // No time stored, fill dt with 0000-01-01T00:00:00.000
251 rtcGetDefaultDateTime(dt);
252 return false;
255 bool rtcSetDateTime(dateTime_t *dt)
257 rtcTime_t t = dateTimeToRtcTime(dt);
258 return rtcSet(&t);
261 #endif