2016-10-14 18:13:02 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2023-03-10 01:16:00 +00:00
|
|
|
* Copyright (c) 2013-2023 Damien P. George
|
2016-10-14 18:13:02 +01:00
|
|
|
* Copyright (c) 2016 Paul Sokolovsky
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "py/mphal.h"
|
2016-10-29 11:42:36 +01:00
|
|
|
#include "py/runtime.h"
|
2023-03-10 01:16:00 +00:00
|
|
|
#include "py/smallint.h"
|
2022-08-18 06:07:24 +01:00
|
|
|
#include "extmod/modtime.h"
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_TIME
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#ifdef MICROPY_PY_TIME_INCLUDEFILE
|
|
|
|
#include MICROPY_PY_TIME_INCLUDEFILE
|
2023-03-10 01:16:00 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2023-03-10 00:13:31 +00:00
|
|
|
#include "shared/timeutils/timeutils.h"
|
|
|
|
|
2023-03-10 01:16:00 +00:00
|
|
|
// localtime([secs])
|
|
|
|
// Convert a time expressed in seconds since the Epoch into an 8-tuple which
|
|
|
|
// contains: (year, month, mday, hour, minute, second, weekday, yearday)
|
|
|
|
// If secs is not provided or None, then the current time is used.
|
|
|
|
// - year is the full year, eg 2000
|
|
|
|
// - month is 1-12
|
|
|
|
// - mday is 1-31
|
|
|
|
// - hour is 0-23
|
|
|
|
// - minute is 0-59
|
|
|
|
// - second is 0-59
|
|
|
|
// - weekday is 0-6 for Mon-Sun
|
|
|
|
// - yearday is 1-366
|
|
|
|
STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
|
|
|
|
if (n_args == 0 || args[0] == mp_const_none) {
|
|
|
|
// Get current date and time.
|
2022-08-18 05:47:56 +01:00
|
|
|
return mp_time_localtime_get();
|
2023-03-10 01:16:00 +00:00
|
|
|
} else {
|
|
|
|
// Convert given seconds to tuple.
|
|
|
|
mp_int_t seconds = mp_obj_get_int(args[0]);
|
|
|
|
timeutils_struct_time_t tm;
|
|
|
|
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
|
|
|
|
mp_obj_t tuple[8] = {
|
|
|
|
tuple[0] = mp_obj_new_int(tm.tm_year),
|
|
|
|
tuple[1] = mp_obj_new_int(tm.tm_mon),
|
|
|
|
tuple[2] = mp_obj_new_int(tm.tm_mday),
|
|
|
|
tuple[3] = mp_obj_new_int(tm.tm_hour),
|
|
|
|
tuple[4] = mp_obj_new_int(tm.tm_min),
|
|
|
|
tuple[5] = mp_obj_new_int(tm.tm_sec),
|
|
|
|
tuple[6] = mp_obj_new_int(tm.tm_wday),
|
|
|
|
tuple[7] = mp_obj_new_int(tm.tm_yday),
|
|
|
|
};
|
|
|
|
return mp_obj_new_tuple(8, tuple);
|
|
|
|
}
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime);
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2023-03-10 00:13:31 +00:00
|
|
|
// mktime()
|
|
|
|
// This is the inverse function of localtime. Its argument is a full 8-tuple
|
|
|
|
// which expresses a time as per localtime. It returns an integer which is
|
|
|
|
// the number of seconds since the Epoch (eg 1st Jan 1970, or 1st Jan 2000).
|
|
|
|
STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
|
|
|
|
size_t len;
|
|
|
|
mp_obj_t *elem;
|
|
|
|
mp_obj_get_array(tuple, &len, &elem);
|
|
|
|
|
|
|
|
// localtime generates a tuple of len 8. CPython uses 9, so we accept both.
|
|
|
|
if (len < 8 || len > 9) {
|
|
|
|
mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
|
|
|
|
}
|
|
|
|
|
|
|
|
return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
|
|
|
|
mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
|
|
|
|
mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_time_mktime_obj, time_mktime);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_TIME_TIME_TIME_NS
|
2023-03-10 01:16:00 +00:00
|
|
|
|
|
|
|
// time()
|
|
|
|
// Return the number of seconds since the Epoch.
|
|
|
|
STATIC mp_obj_t time_time(void) {
|
2022-08-18 05:47:56 +01:00
|
|
|
return mp_time_time_get();
|
2023-03-10 01:16:00 +00:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
STATIC MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_obj, time_time);
|
2023-03-10 01:16:00 +00:00
|
|
|
|
|
|
|
// time_ns()
|
|
|
|
// Returns the number of nanoseconds since the Epoch, as an integer.
|
|
|
|
STATIC mp_obj_t time_time_ns(void) {
|
|
|
|
return mp_obj_new_int_from_ull(mp_hal_time_ns());
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_ns_obj, time_time_ns);
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_TIME_TIME_TIME_NS
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2016-10-14 18:13:02 +01:00
|
|
|
STATIC mp_obj_t time_sleep(mp_obj_t seconds_o) {
|
2022-08-18 06:01:26 +01:00
|
|
|
#ifdef MICROPY_PY_TIME_CUSTOM_SLEEP
|
2022-08-18 05:47:56 +01:00
|
|
|
mp_time_sleep(seconds_o);
|
2023-03-10 01:16:00 +00:00
|
|
|
#else
|
2016-10-14 18:13:02 +01:00
|
|
|
#if MICROPY_PY_BUILTINS_FLOAT
|
2017-07-01 00:23:29 +01:00
|
|
|
mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
|
2016-10-14 18:13:02 +01:00
|
|
|
#else
|
|
|
|
mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
|
|
|
|
#endif
|
2023-03-10 01:16:00 +00:00
|
|
|
#endif
|
2016-10-14 18:13:02 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_obj, time_sleep);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep_ms(mp_obj_t arg) {
|
2016-10-14 20:19:45 +01:00
|
|
|
mp_int_t ms = mp_obj_get_int(arg);
|
2019-11-20 04:21:35 +00:00
|
|
|
if (ms >= 0) {
|
2016-10-14 20:19:45 +01:00
|
|
|
mp_hal_delay_ms(ms);
|
|
|
|
}
|
2016-10-14 18:13:02 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_ms_obj, time_sleep_ms);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t time_sleep_us(mp_obj_t arg) {
|
2016-10-14 20:19:45 +01:00
|
|
|
mp_int_t us = mp_obj_get_int(arg);
|
|
|
|
if (us > 0) {
|
|
|
|
mp_hal_delay_us(us);
|
|
|
|
}
|
2016-10-14 18:13:02 +01:00
|
|
|
return mp_const_none;
|
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_us_obj, time_sleep_us);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_ms(void) {
|
2022-08-18 06:01:26 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
|
2016-10-14 18:13:02 +01:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_ms_obj, time_ticks_ms);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_us(void) {
|
2022-08-18 06:01:26 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
|
2016-10-14 18:13:02 +01:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_us_obj, time_ticks_us);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
|
|
|
STATIC mp_obj_t time_ticks_cpu(void) {
|
2022-08-18 06:01:26 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
|
2016-10-14 18:13:02 +01:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_cpu_obj, time_ticks_cpu);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
extmod/utime_mphal: ticks_diff(): switch arg order, return signed value.
Based on the earlier discussed RFC. Practice showed that the most natural
order for arguments corresponds to mathematical subtraction:
ticks_diff(x, y) <=> x - y
Also, practice showed that in real life, it's hard to order events by time
of occurance a priori, events tend to miss deadlines, etc. and the expected
order breaks. And then there's a need to detect such cases. And ticks_diff
can be used exactly for this purpose, if it returns a signed, instead of
unsigned, value. E.g. if x is scheduled time for event, and y is the current
time, then if ticks_diff(x, y) < 0 then event has missed a deadline (and e.g.
needs to executed ASAP or skipped). Returning in this case a large unsigned
number (like ticks_diff behaved previously) doesn't make sense, and such
"large unsigned number" can't be reliably detected per our definition of
ticks_* function (we don't expose to user level maximum value, it can be
anything, relatively small or relatively large).
2016-10-29 03:02:24 +01:00
|
|
|
STATIC mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
|
2016-10-14 18:13:02 +01:00
|
|
|
// we assume that the arguments come from ticks_xx so are small ints
|
2016-11-03 20:54:16 +00:00
|
|
|
mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
|
|
|
|
mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
|
2016-11-01 23:50:48 +00:00
|
|
|
// Optimized formula avoiding if conditions. We adjust difference "forward",
|
|
|
|
// wrap it around and adjust back.
|
2022-08-18 06:01:26 +01:00
|
|
|
mp_int_t diff = ((end - start + MICROPY_PY_TIME_TICKS_PERIOD / 2) & (MICROPY_PY_TIME_TICKS_PERIOD - 1))
|
|
|
|
- MICROPY_PY_TIME_TICKS_PERIOD / 2;
|
extmod/utime_mphal: Fix implementation of new semantics of ticks_diff().
Now the function properly uses ring arithmetic to return signed value
in range (inclusive):
[-MICROPY_PY_UTIME_TICKS_PERIOD/2, MICROPY_PY_UTIME_TICKS_PERIOD/2-1].
That means that function can properly process 2 time values away from
each other within MICROPY_PY_UTIME_TICKS_PERIOD/2 ticks, but away in
both directions. For example, if tick value 'a' predates tick value 'b',
ticks_diff(a, b) will return negative value, and positive value otherwise.
But at positive value of MICROPY_PY_UTIME_TICKS_PERIOD/2-1, the result
of the function will wrap around to negative -MICROPY_PY_UTIME_TICKS_PERIOD/2,
in other words, if a follows b in more than MICROPY_PY_UTIME_TICKS_PERIOD/2 - 1
ticks, the function will "consider" a to actually predate b.
2016-10-30 01:07:22 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT(diff);
|
2016-10-14 18:13:02 +01:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_diff_obj, time_ticks_diff);
|
2016-10-14 18:13:02 +01:00
|
|
|
|
2016-10-29 15:30:05 +01:00
|
|
|
STATIC mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
|
|
|
|
// we assume that first argument come from ticks_xx so is small int
|
2016-11-03 20:54:16 +00:00
|
|
|
mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
|
|
|
|
mp_uint_t delta = mp_obj_get_int(delta_in);
|
2022-10-14 05:49:52 +01:00
|
|
|
|
|
|
|
// Check that delta does not overflow the range that ticks_diff can handle.
|
|
|
|
// This ensures the following:
|
|
|
|
// - ticks_diff(ticks_add(T, delta), T) == delta
|
|
|
|
// - ticks_diff(T, ticks_add(T, delta)) == -delta
|
|
|
|
// The latter requires excluding delta=-TICKS_PERIOD/2.
|
|
|
|
//
|
|
|
|
// This unsigned comparison is equivalent to a signed comparison of:
|
2023-02-15 03:16:31 +00:00
|
|
|
// delta <= -TICKS_PERIOD/2 || delta >= TICKS_PERIOD/2
|
2022-08-18 06:01:26 +01:00
|
|
|
if (delta + MICROPY_PY_TIME_TICKS_PERIOD / 2 - 1 >= MICROPY_PY_TIME_TICKS_PERIOD - 1) {
|
2022-10-14 05:49:52 +01:00
|
|
|
mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ticks interval overflow"));
|
|
|
|
}
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
|
2016-10-29 15:30:05 +01:00
|
|
|
}
|
2022-08-18 05:47:56 +01:00
|
|
|
MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_add_obj, time_ticks_add);
|
2016-10-29 15:30:05 +01:00
|
|
|
|
2023-03-10 01:16:00 +00:00
|
|
|
STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
|
2022-08-17 07:18:31 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
|
2022-08-18 05:47:56 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_localtime_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mp_time_localtime_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_time_mktime_obj) },
|
2023-03-10 01:16:00 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#if MICROPY_PY_TIME_TIME_TIME_NS
|
2022-08-18 05:47:56 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_time_time_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_time_time_ns_obj) },
|
2023-03-10 01:16:00 +00:00
|
|
|
#endif
|
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_time_sleep_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_time_sleep_ms_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_time_sleep_us_obj) },
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_time_ticks_ms_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_time_ticks_us_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_time_ticks_cpu_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_time_ticks_add_obj) },
|
|
|
|
{ MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_time_ticks_diff_obj) },
|
2023-03-10 01:16:00 +00:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#ifdef MICROPY_PY_TIME_EXTRA_GLOBALS
|
|
|
|
MICROPY_PY_TIME_EXTRA_GLOBALS
|
2023-03-10 01:16:00 +00:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);
|
|
|
|
|
2022-08-18 05:47:56 +01:00
|
|
|
const mp_obj_module_t mp_module_time = {
|
2023-03-10 01:16:00 +00:00
|
|
|
.base = { &mp_type_module },
|
|
|
|
.globals = (mp_obj_dict_t *)&mp_module_time_globals,
|
|
|
|
};
|
|
|
|
|
2023-06-02 03:33:25 +01:00
|
|
|
MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_time, mp_module_time);
|
2020-09-24 03:37:02 +01:00
|
|
|
|
2022-08-18 06:01:26 +01:00
|
|
|
#endif // MICROPY_PY_TIME
|