modtime.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013-2023 Damien P. George
  7. * Copyright (c) 2016 Paul Sokolovsky
  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. #include "py/mphal.h"
  28. #include "py/runtime.h"
  29. #include "py/smallint.h"
  30. #include "extmod/modtime.h"
  31. #if MICROPY_PY_TIME
  32. #ifdef MICROPY_PY_TIME_INCLUDEFILE
  33. #include MICROPY_PY_TIME_INCLUDEFILE
  34. #endif
  35. #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
  36. #include "shared/timeutils/timeutils.h"
  37. // localtime([secs])
  38. // Convert a time expressed in seconds since the Epoch into an 8-tuple which
  39. // contains: (year, month, mday, hour, minute, second, weekday, yearday)
  40. // If secs is not provided or None, then the current time is used.
  41. // - year is the full year, eg 2000
  42. // - month is 1-12
  43. // - mday is 1-31
  44. // - hour is 0-23
  45. // - minute is 0-59
  46. // - second is 0-59
  47. // - weekday is 0-6 for Mon-Sun
  48. // - yearday is 1-366
  49. static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
  50. if (n_args == 0 || args[0] == mp_const_none) {
  51. // Get current date and time.
  52. return mp_time_localtime_get();
  53. } else {
  54. // Convert given seconds to tuple.
  55. mp_int_t seconds = mp_obj_get_int(args[0]);
  56. timeutils_struct_time_t tm;
  57. timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
  58. mp_obj_t tuple[8] = {
  59. tuple[0] = mp_obj_new_int(tm.tm_year),
  60. tuple[1] = mp_obj_new_int(tm.tm_mon),
  61. tuple[2] = mp_obj_new_int(tm.tm_mday),
  62. tuple[3] = mp_obj_new_int(tm.tm_hour),
  63. tuple[4] = mp_obj_new_int(tm.tm_min),
  64. tuple[5] = mp_obj_new_int(tm.tm_sec),
  65. tuple[6] = mp_obj_new_int(tm.tm_wday),
  66. tuple[7] = mp_obj_new_int(tm.tm_yday),
  67. };
  68. return mp_obj_new_tuple(8, tuple);
  69. }
  70. }
  71. MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_time_localtime_obj, 0, 1, time_localtime);
  72. // mktime()
  73. // This is the inverse function of localtime. Its argument is a full 8-tuple
  74. // which expresses a time as per localtime. It returns an integer which is
  75. // the number of seconds since the Epoch (eg 1st Jan 1970, or 1st Jan 2000).
  76. static mp_obj_t time_mktime(mp_obj_t tuple) {
  77. size_t len;
  78. mp_obj_t *elem;
  79. mp_obj_get_array(tuple, &len, &elem);
  80. // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
  81. if (len < 8 || len > 9) {
  82. mp_raise_TypeError(MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9"));
  83. }
  84. return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
  85. mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
  86. mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
  87. }
  88. MP_DEFINE_CONST_FUN_OBJ_1(mp_time_mktime_obj, time_mktime);
  89. #endif // MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
  90. #if MICROPY_PY_TIME_TIME_TIME_NS
  91. // time()
  92. // Return the number of seconds since the Epoch.
  93. static mp_obj_t time_time(void) {
  94. return mp_time_time_get();
  95. }
  96. static MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_obj, time_time);
  97. // time_ns()
  98. // Returns the number of nanoseconds since the Epoch, as an integer.
  99. static mp_obj_t time_time_ns(void) {
  100. return mp_obj_new_int_from_ull(mp_hal_time_ns());
  101. }
  102. MP_DEFINE_CONST_FUN_OBJ_0(mp_time_time_ns_obj, time_time_ns);
  103. #endif // MICROPY_PY_TIME_TIME_TIME_NS
  104. static mp_obj_t time_sleep(mp_obj_t seconds_o) {
  105. #ifdef MICROPY_PY_TIME_CUSTOM_SLEEP
  106. mp_time_sleep(seconds_o);
  107. #else
  108. #if MICROPY_PY_BUILTINS_FLOAT
  109. mp_hal_delay_ms((mp_uint_t)(1000 * mp_obj_get_float(seconds_o)));
  110. #else
  111. mp_hal_delay_ms(1000 * mp_obj_get_int(seconds_o));
  112. #endif
  113. #endif
  114. return mp_const_none;
  115. }
  116. MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_obj, time_sleep);
  117. static mp_obj_t time_sleep_ms(mp_obj_t arg) {
  118. mp_int_t ms = mp_obj_get_int(arg);
  119. if (ms >= 0) {
  120. mp_hal_delay_ms(ms);
  121. }
  122. return mp_const_none;
  123. }
  124. MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_ms_obj, time_sleep_ms);
  125. static mp_obj_t time_sleep_us(mp_obj_t arg) {
  126. mp_int_t us = mp_obj_get_int(arg);
  127. if (us > 0) {
  128. mp_hal_delay_us(us);
  129. }
  130. return mp_const_none;
  131. }
  132. MP_DEFINE_CONST_FUN_OBJ_1(mp_time_sleep_us_obj, time_sleep_us);
  133. static mp_obj_t time_ticks_ms(void) {
  134. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
  135. }
  136. MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_ms_obj, time_ticks_ms);
  137. static mp_obj_t time_ticks_us(void) {
  138. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_us() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
  139. }
  140. MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_us_obj, time_ticks_us);
  141. static mp_obj_t time_ticks_cpu(void) {
  142. return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_cpu() & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
  143. }
  144. MP_DEFINE_CONST_FUN_OBJ_0(mp_time_ticks_cpu_obj, time_ticks_cpu);
  145. static mp_obj_t time_ticks_diff(mp_obj_t end_in, mp_obj_t start_in) {
  146. // we assume that the arguments come from ticks_xx so are small ints
  147. mp_uint_t start = MP_OBJ_SMALL_INT_VALUE(start_in);
  148. mp_uint_t end = MP_OBJ_SMALL_INT_VALUE(end_in);
  149. // Optimized formula avoiding if conditions. We adjust difference "forward",
  150. // wrap it around and adjust back.
  151. mp_int_t diff = ((end - start + MICROPY_PY_TIME_TICKS_PERIOD / 2) & (MICROPY_PY_TIME_TICKS_PERIOD - 1))
  152. - MICROPY_PY_TIME_TICKS_PERIOD / 2;
  153. return MP_OBJ_NEW_SMALL_INT(diff);
  154. }
  155. MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_diff_obj, time_ticks_diff);
  156. static mp_obj_t time_ticks_add(mp_obj_t ticks_in, mp_obj_t delta_in) {
  157. // we assume that first argument come from ticks_xx so is small int
  158. mp_uint_t ticks = MP_OBJ_SMALL_INT_VALUE(ticks_in);
  159. mp_uint_t delta = mp_obj_get_int(delta_in);
  160. // Check that delta does not overflow the range that ticks_diff can handle.
  161. // This ensures the following:
  162. // - ticks_diff(ticks_add(T, delta), T) == delta
  163. // - ticks_diff(T, ticks_add(T, delta)) == -delta
  164. // The latter requires excluding delta=-TICKS_PERIOD/2.
  165. //
  166. // This unsigned comparison is equivalent to a signed comparison of:
  167. // delta <= -TICKS_PERIOD/2 || delta >= TICKS_PERIOD/2
  168. if (delta + MICROPY_PY_TIME_TICKS_PERIOD / 2 - 1 >= MICROPY_PY_TIME_TICKS_PERIOD - 1) {
  169. mp_raise_msg(&mp_type_OverflowError, MP_ERROR_TEXT("ticks interval overflow"));
  170. }
  171. return MP_OBJ_NEW_SMALL_INT((ticks + delta) & (MICROPY_PY_TIME_TICKS_PERIOD - 1));
  172. }
  173. MP_DEFINE_CONST_FUN_OBJ_2(mp_time_ticks_add_obj, time_ticks_add);
  174. static const mp_rom_map_elem_t mp_module_time_globals_table[] = {
  175. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) },
  176. #if MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME
  177. { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&mp_time_localtime_obj) },
  178. { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mp_time_localtime_obj) },
  179. { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&mp_time_mktime_obj) },
  180. #endif
  181. #if MICROPY_PY_TIME_TIME_TIME_NS
  182. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mp_time_time_obj) },
  183. { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_time_time_ns_obj) },
  184. #endif
  185. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_time_sleep_obj) },
  186. { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_time_sleep_ms_obj) },
  187. { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_time_sleep_us_obj) },
  188. { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_time_ticks_ms_obj) },
  189. { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_time_ticks_us_obj) },
  190. { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_time_ticks_cpu_obj) },
  191. { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_time_ticks_add_obj) },
  192. { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_time_ticks_diff_obj) },
  193. #ifdef MICROPY_PY_TIME_EXTRA_GLOBALS
  194. MICROPY_PY_TIME_EXTRA_GLOBALS
  195. #endif
  196. };
  197. static MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);
  198. const mp_obj_module_t mp_module_time = {
  199. .base = { &mp_type_module },
  200. .globals = (mp_obj_dict_t *)&mp_module_time_globals,
  201. };
  202. MP_REGISTER_EXTENSIBLE_MODULE(MP_QSTR_time, mp_module_time);
  203. #endif // MICROPY_PY_TIME