blob: fd8fbb36af6e50a98f93fa0eb85982e928c83b6f [file] [log] [blame]
Damien George04b91472014-05-03 23:27:38 +01001/*
2 * This file is part of the Micro Python project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2013, 2014 Damien P. George
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 * THE SOFTWARE.
25 */
26
Dave Hylands66785952014-08-19 18:38:38 -070027#include <stdio.h>
28#include <string.h>
Damien Georged6cbbc52014-05-08 22:25:49 +010029#include "stm32f4xx_hal.h"
Damien Georged3116552014-03-22 15:06:29 +000030
Paul Sokolovsky9b71b162014-05-02 18:03:04 +030031#include "mpconfig.h"
Damien Georged3116552014-03-22 15:06:29 +000032#include "nlr.h"
33#include "misc.h"
Damien Georged3116552014-03-22 15:06:29 +000034#include "qstr.h"
35#include "obj.h"
Damien George04b91472014-05-03 23:27:38 +010036#include "portmodules.h"
Damien Georged6cbbc52014-05-08 22:25:49 +010037#include "rtc.h"
38
Damien Georgeea439e52014-08-08 23:30:01 +010039/// \module time - time related functions
40///
41/// The `time` module provides functions for getting the current time and date,
42/// and for sleeping.
43
Dave Hylands66785952014-08-19 18:38:38 -070044STATIC const uint16_t days_since_jan1[]= { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
Damien Georged6cbbc52014-05-08 22:25:49 +010045
Damien George02bc8822014-07-19 16:39:13 +010046STATIC bool is_leap_year(mp_uint_t year) {
Damien Georged6cbbc52014-05-08 22:25:49 +010047 return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
48}
49
Dave Hylands66785952014-08-19 18:38:38 -070050// Month is one based
Damien George8ba83242014-08-24 17:40:24 +010051STATIC mp_uint_t mod_time_days_in_month(mp_uint_t year, mp_uint_t month) {
Dave Hylands66785952014-08-19 18:38:38 -070052 mp_uint_t mdays = days_since_jan1[month] - days_since_jan1[month - 1];
53 if (month == 2 && is_leap_year(year)) {
54 mdays++;
55 }
56 return mdays;
57}
58
Damien Georged6cbbc52014-05-08 22:25:49 +010059// compute the day of the year, between 1 and 366
60// month should be between 1 and 12, date should start at 1
Damien George8ba83242014-08-24 17:40:24 +010061STATIC mp_uint_t mod_time_year_day(mp_uint_t year, mp_uint_t month, mp_uint_t date) {
Damien George02bc8822014-07-19 16:39:13 +010062 mp_uint_t yday = days_since_jan1[month - 1] + date;
Damien Georged6cbbc52014-05-08 22:25:49 +010063 if (month >= 3 && is_leap_year(year)) {
64 yday += 1;
65 }
66 return yday;
67}
68
Dave Hylands66785952014-08-19 18:38:38 -070069// returns the number of seconds, as an integer, since 2000-01-01
Damien George02bc8822014-07-19 16:39:13 +010070mp_uint_t mod_time_seconds_since_2000(mp_uint_t year, mp_uint_t month, mp_uint_t date, mp_uint_t hour, mp_uint_t minute, mp_uint_t second) {
71 return
72 second
73 + minute * 60
74 + hour * 3600
75 + (mod_time_year_day(year, month, date) - 1
76 + ((year - 2000 + 3) / 4) // add a day each 4 years starting with 2001
77 - ((year - 2000 + 99) / 100) // subtract a day each 100 years starting with 2001
78 + ((year - 2000 + 399) / 400) // add a day each 400 years starting with 2001
79 ) * 86400
80 + (year - 2000) * 31536000;
81}
82
Dave Hylands66785952014-08-19 18:38:38 -070083// LEAPOCH corresponds to 2000-03-01, which is a mod-400 year, immediately
84// after Feb 29. We calculate seconds as a signed integer relative to that.
85//
86// Our timebase is is relative to 2000-01-01.
87
88#define LEAPOCH ((31 + 29) * 86400)
89
Damien George8ba83242014-08-24 17:40:24 +010090#define DAYS_PER_400Y (365*400 + 97)
91#define DAYS_PER_100Y (365*100 + 24)
92#define DAYS_PER_4Y (365*4 + 1)
Dave Hylands66785952014-08-19 18:38:38 -070093
Damien George8ba83242014-08-24 17:40:24 +010094typedef struct {
95 uint16_t tm_year; // i.e. 2014
96 uint8_t tm_mon; // 1..12
97 uint8_t tm_mday; // 1..31
98 uint8_t tm_hour; // 0..23
99 uint8_t tm_min; // 0..59
100 uint8_t tm_sec; // 0..59
101 uint8_t tm_wday; // 0..6 0 = Monday
102 uint16_t tm_yday; // 1..366
103} mod_struct_time;
104
105STATIC void mod_time_seconds_since_2000_to_struct_time(mp_uint_t t, mod_struct_time *tm) {
Dave Hylands66785952014-08-19 18:38:38 -0700106 // The following algorithm was adapted from musl's __secs_to_tm and adapted
Damien George8ba83242014-08-24 17:40:24 +0100107 // for differences in Micro Python's timebase.
Dave Hylands66785952014-08-19 18:38:38 -0700108
109 mp_int_t seconds = t - LEAPOCH;
110
111 mp_int_t days = seconds / 86400;
112 seconds %= 86400;
113 tm->tm_hour = seconds / 3600;
114 tm->tm_min = seconds / 60 % 60;
115 tm->tm_sec = seconds % 60;
116
117 mp_int_t wday = (days + 2) % 7; // Mar 1, 2000 was a Wednesday (2)
118 if (wday < 0) {
119 wday += 7;
120 }
121 tm->tm_wday = wday;
122
123 mp_int_t qc_cycles = days / DAYS_PER_400Y;
124 days %= DAYS_PER_400Y;
125 if (days < 0) {
126 days += DAYS_PER_400Y;
127 qc_cycles--;
128 }
129 mp_int_t c_cycles = days / DAYS_PER_100Y;
130 if (c_cycles == 4) {
131 c_cycles--;
132 }
133 days -= (c_cycles * DAYS_PER_100Y);
134
135 mp_int_t q_cycles = days / DAYS_PER_4Y;
136 if (q_cycles == 25) {
137 q_cycles--;
138 }
139 days -= q_cycles * DAYS_PER_4Y;
140
141 mp_int_t years = days / 365;
142 if (years == 4) {
143 years--;
144 }
145 days -= (years * 365);
146
Damien George8ba83242014-08-24 17:40:24 +0100147 /* We will compute tm_yday at the very end
Dave Hylands66785952014-08-19 18:38:38 -0700148 mp_int_t leap = !years && (q_cycles || !c_cycles);
149
150 tm->tm_yday = days + 31 + 28 + leap;
151 if (tm->tm_yday >= 365 + leap) {
152 tm->tm_yday -= 365 + leap;
153 }
154
Damien George8ba83242014-08-24 17:40:24 +0100155 tm->tm_yday++; // Make one based
156 */
157
Dave Hylands66785952014-08-19 18:38:38 -0700158 tm->tm_year = 2000 + years + 4 * q_cycles + 100 * c_cycles + 400 * qc_cycles;
159
160 // Note: days_in_month[0] corresponds to March
Damien George8ba83242014-08-24 17:40:24 +0100161 STATIC const int8_t days_in_month[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 31, 29};
Dave Hylands66785952014-08-19 18:38:38 -0700162
163 mp_int_t month;
164 for (month = 0; days_in_month[month] <= days; month++) {
165 days -= days_in_month[month];
166 }
167
168 tm->tm_mon = month + 2;
169 if (tm->tm_mon >= 12) {
170 tm->tm_mon -= 12;
171 tm->tm_year++;
172 }
173 tm->tm_mday = days + 1; // Make one based
174 tm->tm_mon++; // Make one based
Dave Hylands66785952014-08-19 18:38:38 -0700175
Damien George8ba83242014-08-24 17:40:24 +0100176 tm->tm_yday = mod_time_year_day(tm->tm_year, tm->tm_mon, tm->tm_mday);
177}
Dave Hylands66785952014-08-19 18:38:38 -0700178
179/// \function localtime([secs])
180/// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
181/// contains: (year, month, mday, hour, minute, second, weekday, yearday)
182/// If secs is not provided or None, then the current time from the RTC is used.
183/// year includes the century (for example 2014)
184/// month is 1-12
185/// mday is 1-31
186/// hour is 0-23
187/// minute is 0-59
188/// second is 0-59
189/// weekday is 0-6 for Mon-Sun.
190/// yearday is 1-366
Damien Georgeecc88e92014-08-30 00:35:11 +0100191STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) {
Dave Hylands66785952014-08-19 18:38:38 -0700192 if (n_args == 0 || args[0] == mp_const_none) {
193 // get current date and time
194 // note: need to call get time then get date to correctly access the registers
195 RTC_DateTypeDef date;
196 RTC_TimeTypeDef time;
197 HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
198 HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
199 mp_obj_t tuple[8] = {
200 mp_obj_new_int(2000 + date.Year),
201 mp_obj_new_int(date.Month),
202 mp_obj_new_int(date.Date),
203 mp_obj_new_int(time.Hours),
204 mp_obj_new_int(time.Minutes),
205 mp_obj_new_int(time.Seconds),
206 mp_obj_new_int(date.WeekDay - 1),
207 mp_obj_new_int(mod_time_year_day(2000 + date.Year, date.Month, date.Date)),
208 };
209 return mp_obj_new_tuple(8, tuple);
Damien George8ba83242014-08-24 17:40:24 +0100210 } else {
211 mp_int_t seconds = mp_obj_get_int(args[0]);
212 mod_struct_time tm;
213 mod_time_seconds_since_2000_to_struct_time(seconds, &tm);
214 mp_obj_t tuple[8] = {
215 tuple[0] = mp_obj_new_int(tm.tm_year),
216 tuple[1] = mp_obj_new_int(tm.tm_mon),
217 tuple[2] = mp_obj_new_int(tm.tm_mday),
218 tuple[3] = mp_obj_new_int(tm.tm_hour),
219 tuple[4] = mp_obj_new_int(tm.tm_min),
220 tuple[5] = mp_obj_new_int(tm.tm_sec),
221 tuple[6] = mp_obj_new_int(tm.tm_wday),
222 tuple[7] = mp_obj_new_int(tm.tm_yday),
223 };
224 return mp_obj_new_tuple(8, tuple);
Dave Hylands66785952014-08-19 18:38:38 -0700225 }
Damien Georged6cbbc52014-05-08 22:25:49 +0100226}
Dave Hylands66785952014-08-19 18:38:38 -0700227MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
228
229
230/// \function mktime()
231/// This is inverse function of localtime. It's argument is a full 8-tuple
232/// which expresses a time as per localtime. It returns an integer which is
233/// the number of seconds since Jan 1, 2000.
234STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
235
Damien Georged03c6812014-10-05 21:51:54 +0100236 mp_uint_t len;
Dave Hylands66785952014-08-19 18:38:38 -0700237 mp_obj_t *elem;
238
239 mp_obj_get_array(tuple, &len, &elem);
240
241 // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
242 if (len < 8 || len > 9) {
243 nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len));
244 }
245
246 mp_int_t year = mp_obj_get_int(elem[0]);
247 mp_int_t month = mp_obj_get_int(elem[1]);
248 mp_int_t mday = mp_obj_get_int(elem[2]);
249 mp_int_t hours = mp_obj_get_int(elem[3]);
250 mp_int_t minutes = mp_obj_get_int(elem[4]);
251 mp_int_t seconds = mp_obj_get_int(elem[5]);
252
Damien George8ba83242014-08-24 17:40:24 +0100253 // Normalise the tuple. This allows things like:
Dave Hylands66785952014-08-19 18:38:38 -0700254 //
255 // tm_tomorrow = list(time.localtime())
256 // tm_tomorrow[2] += 1 # Adds 1 to mday
257 // tomorrow = time.mktime(tm_tommorrow)
Damien George8ba83242014-08-24 17:40:24 +0100258 //
Dave Hylands66785952014-08-19 18:38:38 -0700259 // And not have to worry about all the weird overflows.
260 //
261 // You can subtract dates/times this way as well.
262
263 minutes += seconds / 60;
264 if ((seconds = seconds % 60) < 0) {
265 seconds += 60;
266 minutes--;
267 }
268
269 hours += minutes / 60;
270 if ((minutes = minutes % 60) < 0) {
271 minutes += 60;
272 hours--;
273 }
274
275 mday += hours / 24;
276 if ((hours = hours % 24) < 0) {
277 hours += 24;
278 mday--;
279 }
280
281 month--; // make month zero based
282 year += month / 12;
283 if ((month = month % 12) < 0) {
284 month += 12;
285 year--;
286 }
287 month++; // back to one based
288
289 while (mday < 1) {
290 if (--month == 0) {
291 month = 12;
292 year--;
293 }
Damien George8ba83242014-08-24 17:40:24 +0100294 mday += mod_time_days_in_month(year, month);
Dave Hylands66785952014-08-19 18:38:38 -0700295 }
Damien George8ba83242014-08-24 17:40:24 +0100296 while (mday > mod_time_days_in_month(year, month)) {
297 mday -= mod_time_days_in_month(year, month);
Dave Hylands66785952014-08-19 18:38:38 -0700298 if (++month == 13) {
299 month = 1;
300 year++;
301 }
302 }
303 return mp_obj_new_int_from_uint(mod_time_seconds_since_2000(year, month, mday, hours, minutes, seconds));
304}
305MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
306
Damien Georged3116552014-03-22 15:06:29 +0000307
Damien Georgeea439e52014-08-08 23:30:01 +0100308/// \function sleep(seconds)
309/// Sleep for the given number of seconds. Seconds can be a floating-point number to
310/// sleep for a fractional number of seconds.
Damien Georged3116552014-03-22 15:06:29 +0000311STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
Damien Georgefb510b32014-06-01 13:32:54 +0100312#if MICROPY_PY_BUILTINS_FLOAT
Damien Georged3116552014-03-22 15:06:29 +0000313 if (MP_OBJ_IS_INT(seconds_o)) {
314#endif
315 HAL_Delay(1000 * mp_obj_get_int(seconds_o));
Damien Georgefb510b32014-06-01 13:32:54 +0100316#if MICROPY_PY_BUILTINS_FLOAT
Damien Georged3116552014-03-22 15:06:29 +0000317 } else {
318 HAL_Delay((uint32_t)(1000 * mp_obj_get_float(seconds_o)));
319 }
320#endif
321 return mp_const_none;
322}
Damien Georged3116552014-03-22 15:06:29 +0000323MP_DEFINE_CONST_FUN_OBJ_1(time_sleep_obj, time_sleep);
324
Damien Georgeea439e52014-08-08 23:30:01 +0100325/// \function time()
326/// Returns the number of seconds, as an integer, since 1/1/2000.
Damien Georged6cbbc52014-05-08 22:25:49 +0100327STATIC mp_obj_t time_time(void) {
328 // get date and time
329 // note: need to call get time then get date to correctly access the registers
330 RTC_DateTypeDef date;
331 RTC_TimeTypeDef time;
332 HAL_RTC_GetTime(&RTCHandle, &time, FORMAT_BIN);
333 HAL_RTC_GetDate(&RTCHandle, &date, FORMAT_BIN);
Damien George02bc8822014-07-19 16:39:13 +0100334 return mp_obj_new_int(mod_time_seconds_since_2000(2000 + date.Year, date.Month, date.Date, time.Hours, time.Minutes, time.Seconds));
Damien Georged6cbbc52014-05-08 22:25:49 +0100335}
336MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
337
Damien Georged3116552014-03-22 15:06:29 +0000338STATIC const mp_map_elem_t time_module_globals_table[] = {
Damien George0107e902014-10-12 20:23:47 +0100339 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR_utime) },
Damien Georged3116552014-03-22 15:06:29 +0000340
Damien Georged6cbbc52014-05-08 22:25:49 +0100341 { MP_OBJ_NEW_QSTR(MP_QSTR_localtime), (mp_obj_t)&time_localtime_obj },
Dave Hylands66785952014-08-19 18:38:38 -0700342 { MP_OBJ_NEW_QSTR(MP_QSTR_mktime), (mp_obj_t)&time_mktime_obj },
Damien Georged3116552014-03-22 15:06:29 +0000343 { MP_OBJ_NEW_QSTR(MP_QSTR_sleep), (mp_obj_t)&time_sleep_obj },
Damien Georged6cbbc52014-05-08 22:25:49 +0100344 { MP_OBJ_NEW_QSTR(MP_QSTR_time), (mp_obj_t)&time_time_obj },
Damien Georged3116552014-03-22 15:06:29 +0000345};
346
Damien George8b0535e2014-04-05 21:53:54 +0100347STATIC const mp_obj_dict_t time_module_globals = {
348 .base = {&mp_type_dict},
349 .map = {
350 .all_keys_are_qstrs = 1,
351 .table_is_fixed_array = 1,
Emmanuel Blotf6932d62014-06-19 18:54:34 +0200352 .used = MP_ARRAY_SIZE(time_module_globals_table),
353 .alloc = MP_ARRAY_SIZE(time_module_globals_table),
Damien George8b0535e2014-04-05 21:53:54 +0100354 .table = (mp_map_elem_t*)time_module_globals_table,
355 },
Damien Georged3116552014-03-22 15:06:29 +0000356};
357
Damien George0107e902014-10-12 20:23:47 +0100358const mp_obj_module_t mp_module_utime = {
Damien Georged3116552014-03-22 15:06:29 +0000359 .base = { &mp_type_module },
Damien George0107e902014-10-12 20:23:47 +0100360 .name = MP_QSTR_utime,
Damien George8b0535e2014-04-05 21:53:54 +0100361 .globals = (mp_obj_dict_t*)&time_module_globals,
Damien Georged3116552014-03-22 15:06:29 +0000362};