modtime.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * This file is part of the MicroPython 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. #include "py/mpconfig.h"
  27. #if MICROPY_PY_UTIME
  28. #include <unistd.h>
  29. #include <errno.h>
  30. #include <string.h>
  31. #include <time.h>
  32. #include <sys/time.h>
  33. #include <math.h>
  34. #include "py/runtime.h"
  35. #include "py/smallint.h"
  36. #include "py/mphal.h"
  37. #include "extmod/utime_mphal.h"
  38. #ifdef _WIN32
  39. static inline int msec_sleep_tv(struct timeval *tv) {
  40. msec_sleep(tv->tv_sec * 1000.0 + tv->tv_usec / 1000.0);
  41. return 0;
  42. }
  43. #define sleep_select(a,b,c,d,e) msec_sleep_tv((e))
  44. #else
  45. #define sleep_select select
  46. #endif
  47. // mingw32 defines CLOCKS_PER_SEC as ((clock_t)<somevalue>) but preprocessor does not handle casts
  48. #if defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR)
  49. #define MP_REMOVE_BRACKETSA(x)
  50. #define MP_REMOVE_BRACKETSB(x) MP_REMOVE_BRACKETSA x
  51. #define MP_REMOVE_BRACKETSC(x) MP_REMOVE_BRACKETSB x
  52. #define MP_CLOCKS_PER_SEC MP_REMOVE_BRACKETSC(CLOCKS_PER_SEC)
  53. #else
  54. #define MP_CLOCKS_PER_SEC CLOCKS_PER_SEC
  55. #endif
  56. #if defined(MP_CLOCKS_PER_SEC)
  57. #define CLOCK_DIV (MP_CLOCKS_PER_SEC / 1000.0F)
  58. #else
  59. #error Unsupported clock() implementation
  60. #endif
  61. STATIC mp_obj_t mod_time_time(void) {
  62. #if MICROPY_PY_BUILTINS_FLOAT
  63. struct timeval tv;
  64. gettimeofday(&tv, NULL);
  65. mp_float_t val = tv.tv_sec + (mp_float_t)tv.tv_usec / 1000000;
  66. return mp_obj_new_float(val);
  67. #else
  68. return mp_obj_new_int((mp_int_t)time(NULL));
  69. #endif
  70. }
  71. STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_time_obj, mod_time_time);
  72. // Note: this is deprecated since CPy3.3, but pystone still uses it.
  73. STATIC mp_obj_t mod_time_clock(void) {
  74. #if MICROPY_PY_BUILTINS_FLOAT
  75. // float cannot represent full range of int32 precisely, so we pre-divide
  76. // int to reduce resolution, and then actually do float division hoping
  77. // to preserve integer part resolution.
  78. return mp_obj_new_float((float)(clock() / 1000) / CLOCK_DIV);
  79. #else
  80. return mp_obj_new_int((mp_int_t)clock());
  81. #endif
  82. }
  83. STATIC MP_DEFINE_CONST_FUN_OBJ_0(mod_time_clock_obj, mod_time_clock);
  84. STATIC mp_obj_t mod_time_sleep(mp_obj_t arg) {
  85. #if MICROPY_PY_BUILTINS_FLOAT
  86. struct timeval tv;
  87. mp_float_t val = mp_obj_get_float(arg);
  88. double ipart;
  89. tv.tv_usec = round(modf(val, &ipart) * 1000000);
  90. tv.tv_sec = ipart;
  91. int res;
  92. while (1) {
  93. MP_THREAD_GIL_EXIT();
  94. res = sleep_select(0, NULL, NULL, NULL, &tv);
  95. MP_THREAD_GIL_ENTER();
  96. #if MICROPY_SELECT_REMAINING_TIME
  97. // TODO: This assumes Linux behavior of modifying tv to the remaining
  98. // time.
  99. if (res != -1 || errno != EINTR) {
  100. break;
  101. }
  102. mp_handle_pending();
  103. //printf("select: EINTR: %ld:%ld\n", tv.tv_sec, tv.tv_usec);
  104. #else
  105. break;
  106. #endif
  107. }
  108. RAISE_ERRNO(res, errno);
  109. #else
  110. // TODO: Handle EINTR
  111. MP_THREAD_GIL_EXIT();
  112. sleep(mp_obj_get_int(arg));
  113. MP_THREAD_GIL_ENTER();
  114. #endif
  115. return mp_const_none;
  116. }
  117. STATIC MP_DEFINE_CONST_FUN_OBJ_1(mod_time_sleep_obj, mod_time_sleep);
  118. STATIC mp_obj_t mod_time_localtime(size_t n_args, const mp_obj_t *args) {
  119. time_t t;
  120. if (n_args == 0) {
  121. t = time(NULL);
  122. } else {
  123. #if MICROPY_PY_BUILTINS_FLOAT
  124. mp_float_t val = mp_obj_get_float(args[0]);
  125. t = (time_t)MICROPY_FLOAT_C_FUN(trunc)(val);
  126. #else
  127. t = mp_obj_get_int(args[0]);
  128. #endif
  129. }
  130. struct tm *tm = localtime(&t);
  131. mp_obj_t ret = mp_obj_new_tuple(9, NULL);
  132. mp_obj_tuple_t *tuple = MP_OBJ_TO_PTR(ret);
  133. tuple->items[0] = MP_OBJ_NEW_SMALL_INT(tm->tm_year + 1900);
  134. tuple->items[1] = MP_OBJ_NEW_SMALL_INT(tm->tm_mon + 1);
  135. tuple->items[2] = MP_OBJ_NEW_SMALL_INT(tm->tm_mday);
  136. tuple->items[3] = MP_OBJ_NEW_SMALL_INT(tm->tm_hour);
  137. tuple->items[4] = MP_OBJ_NEW_SMALL_INT(tm->tm_min);
  138. tuple->items[5] = MP_OBJ_NEW_SMALL_INT(tm->tm_sec);
  139. int wday = tm->tm_wday - 1;
  140. if (wday < 0) {
  141. wday = 6;
  142. }
  143. tuple->items[6] = MP_OBJ_NEW_SMALL_INT(wday);
  144. tuple->items[7] = MP_OBJ_NEW_SMALL_INT(tm->tm_yday + 1);
  145. tuple->items[8] = MP_OBJ_NEW_SMALL_INT(tm->tm_isdst);
  146. return ret;
  147. }
  148. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_time_localtime_obj, 0, 1, mod_time_localtime);
  149. STATIC const mp_rom_map_elem_t mp_module_time_globals_table[] = {
  150. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
  151. { MP_ROM_QSTR(MP_QSTR_clock), MP_ROM_PTR(&mod_time_clock_obj) },
  152. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mod_time_sleep_obj) },
  153. { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  154. { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  155. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&mod_time_time_obj) },
  156. { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  157. { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  158. { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
  159. { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
  160. { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
  161. { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&mod_time_localtime_obj) },
  162. };
  163. STATIC MP_DEFINE_CONST_DICT(mp_module_time_globals, mp_module_time_globals_table);
  164. const mp_obj_module_t mp_module_time = {
  165. .base = { &mp_type_module },
  166. .globals = (mp_obj_dict_t*)&mp_module_time_globals,
  167. };
  168. #endif // MICROPY_PY_UTIME