1/*
2 * This file is part of the MicroPython project, http://micropython.org/
3 *
4 * The MIT License (MIT)
5 *
6 * Copyright (c) 2014-2017 Paul Sokolovsky
7 * Copyright (c) 2014-2017 Damien P. George
8 *
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE SOFTWARE.
26 */
27
28#include "py/mpconfig.h"
29#if MICROPY_PY_UTIME
30
31#include <unistd.h>
32#include <errno.h>
33#include <string.h>
34#include <time.h>
35#include <sys/time.h>
36#include <math.h>
37
38#include "py/runtime.h"
39#include "py/smallint.h"
40#include "py/mphal.h"
41#include "extmod/utime_mphal.h"
42
43#ifdef _WIN32
44static inline int msec_sleep_tv(struct timeval *tv) {
45 msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0);
46 return 0;
47}
48#define sleep_select(a,b,c,d,e) msec_sleep_tv((e))
49#else
50#define sleep_select select
51#endif
52
53// mingw32 defines CLOCKS_PER_SEC as ((clock_t)<somevalue>) but preprocessor does not handle casts
54#if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
55#define MP_REMOVE_BRACKETSA(x)
56#define MP_REMOVE_BRACKETSB(x) MP_REMOVE_BRACKETSA x
57#define MP_REMOVE_BRACKETSC(x) MP_REMOVE_BRACKETSB x
58#define MP_CLOCKS_PER_SEC MP_REMOVE_BRACKETSC(CLOCKS_PER_SEC)
59#else
60#define MP_CLOCKS_PER_SEC CLOCKS_PER_SEC
61#endif
62
63#if defined(MP_CLOCKS_PER_SEC)
64#define CLOCK_DIV (MP_CLOCKS_PER_SEC / MICROPY_FLOAT_CONST(1000.0))
65#else
66#error Unsupported clock() implementation
67#endif
68
69STATIC mp_obj_t mod_time_time(void) {
70 #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
71 struct timeval tv;
72 gettimeofday(&tv, NULL);
73 mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000;
74 return mp_obj_new_float(val);
75 #else
76 return mp_obj_new_int((mp_int_t)time(NULL));
77 #endif
78}
79STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
80
81// Note: this is deprecated since CPy3.3, but pystone still uses it.
82STATIC mp_obj_t mod_time_clock(void) {
83 #if MICROPY_PY_BUILTINS_FLOAT
84 // float cannot represent full range of int32 precisely, so we pre-divide
85 // int to reduce resolution, and then actually do float division hoping
86 // to preserve integer part resolution.
87 return mp_obj_new_float((clock() / 1000) / CLOCK_DIV);
88 #else
89 return mp_obj_new_int((mp_int_t)clock());
90 #endif
91}
92STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
93
94STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
95 #if MICROPY_PY_BUILTINS_FLOAT
96 struct timeval tv;
97 mp_float_t val = mp_obj_get_float(arg);
98 mp_float_t ipart;
99 tv.tv_usec = (time_t)MICROPY_FLOAT_C_FUN(round)(MICROPY_FLOAT_C_FUN(modf)(val, &ipart) * MICROPY_FLOAT_CONST(1000000.));
100 tv.tv_sec = (suseconds_t)ipart;
101 int res;
102 while (1) {
103 MP_THREAD_GIL_EXIT();
104 res = sleep_select(0, NULL, NULL, NULL, &tv);
105 MP_THREAD_GIL_ENTER();
106 #if MICROPY_SELECT_REMAINING_TIME
107 // TODO: This assumes Linux behavior of modifying tv to the remaining
108 // time.
109 if (res != -1 || errno != EINTR) {
110 break;
111 }
112 mp_handle_pending(true);
113 // printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec);
114 #else
115 break;
116 #endif
117 }
118 RAISE_ERRNO(res, errno);
119 #else
120 int seconds = mp_obj_get_int(arg);
121 for (;;) {
122 MP_THREAD_GIL_EXIT();
123 seconds = sleep(seconds);
124 MP_THREAD_GIL_ENTER();
125 if (seconds == 0) {
126 break;
127 }
128 mp_handle_pending(true);
129 }
130 #endif
131 return mp_const_none;
132}
133STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
134
135STATIC mp_obj_t mod_time_gm_local_time(size_t n_args, const mp_obj_t *args, struct tm *(*time_func)(const time_t *timep)) {
136 time_t t;
137 if (n_args == 0) {
138 t = time(NULL);
139 } else {
140 #if MICROPY_PY_BUILTINS_FLOAT && MICROPY_FLOAT_IMPL == MICROPY_FLOAT_IMPL_DOUBLE
141 mp_float_t val = mp_obj_get_float(args[0]);
142 t = (time_t)MICROPY_FLOAT_C_FUN(trunc)(val);
143 #else
144 t = mp_obj_get_int(args[0]);
145 #endif
146 }
147 struct tm *tm = time_func(&t);
148
149 mp_obj_t ret = mp_obj_new_tuple(9, NULL);
150
151 mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(ret);
152 tuple->items[0] = MP_OBJ_NEW_SMALL_INT(tm->tm_year + 1900);
153 tuple->items[1] = MP_OBJ_NEW_SMALL_INT(tm->tm_mon + 1);
154 tuple->items[2] = MP_OBJ_NEW_SMALL_INT(tm->tm_mday);
155 tuple->items[3] = MP_OBJ_NEW_SMALL_INT(tm->tm_hour);
156 tuple->items[4] = MP_OBJ_NEW_SMALL_INT(tm->tm_min);
157 tuple->items[5] = MP_OBJ_NEW_SMALL_INT(tm->tm_sec);
158 int wday = tm->tm_wday - 1;
159 if (wday < 0) {
160 wday = 6;
161 }
162 tuple->items[6] = MP_OBJ_NEW_SMALL_INT(wday);
163 tuple->items[7] = MP_OBJ_NEW_SMALL_INT(tm->tm_yday + 1);
164 tuple->items[8] = MP_OBJ_NEW_SMALL_INT(tm->tm_isdst);
165
166 return ret;
167}
168
169STATIC mp_obj_t mod_time_gmtime(size_t n_args, const mp_obj_t *args) {
170 return mod_time_gm_local_time(n_args, args, gmtime);
171}
172STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_gmtime_obj, 0, 1, mod_time_gmtime);
173
174STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
175 return mod_time_gm_local_time(n_args, args, localtime);
176}
177STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_localtime_obj, 0, 1, mod_time_localtime);
178
179STATIC mp_obj_t mod_time_mktime(mp_obj_t tuple) {
180 size_t len;
181 mp_obj_t *elem;
182 mp_obj_get_array(tuple, &len, &elem);
183
184 // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
185 if (len < 8 || len > 9) {
186 mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
187 }
188
189 struct tm time = {
190 .tm_year = mp_obj_get_int(elem[0]) - 1900,
191 .tm_mon = mp_obj_get_int(elem[1]) - 1,
192 .tm_mday = mp_obj_get_int(elem[2]),
193 .tm_hour = mp_obj_get_int(elem[3]),
194 .tm_min = mp_obj_get_int(elem[4]),
195 .tm_sec = mp_obj_get_int(elem[5]),
196 };
197 if (len == 9) {
198 time.tm_isdst = mp_obj_get_int(elem[8]);
199 } else {
200 time.tm_isdst = -1; // auto-detect
201 }
202 time_t ret = mktime(&time);
203 if (ret == -1) {
204 mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("invalid mktime usage"));
205 }
206 return mp_obj_new_int(ret);
207}
208MP_DEFINE_CONST_FUN_OBJ_1(mod_time_mktime_obj, mod_time_mktime);
209
210STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
211 { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
212 { MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mod_time_clock_obj) },
213 { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mod_time_sleep_obj) },
214 { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
215 { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
216 { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mod_time_time_obj) },
217 { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
218 { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
219 { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
220 { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
221 { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
222 { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
223 { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mod_time_gmtime_obj) },
224 { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) },
225 { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mod_time_mktime_obj) },
226};
227
228STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);
229
230const mp_obj_module_t mp_module_time = {
231 .base = { &mp_type_module },
232 .globals = (mp_obj_dict_t *)&mp_module_time_globals,
233};
234
235#endif // MICROPY_PY_UTIME
236