modutime.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * Development of the code in this file was sponsored by Microbric Pty Ltd
  5. *
  6. * The MIT License (MIT)
  7. *
  8. * Copyright (c) 2016 Damien P. George
  9. *
  10. * Permission is hereby granted, free of charge, to any person obtaining a copy
  11. * of this software and associated documentation files (the "Software"), to deal
  12. * in the Software without restriction, including without limitation the rights
  13. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. * copies of the Software, and to permit persons to whom the Software is
  15. * furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in
  18. * all copies or substantial portions of the Software.
  19. *
  20. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  26. * THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <sys/time.h>
  31. #include "py/runtime.h"
  32. #include "lib/timeutils/timeutils.h"
  33. #include "extmod/utime_mphal.h"
  34. STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
  35. timeutils_struct_time_t tm;
  36. mp_int_t seconds;
  37. if (n_args == 0 || args[0] == mp_const_none) {
  38. struct timeval tv;
  39. gettimeofday(&tv, NULL);
  40. seconds = tv.tv_sec;
  41. } else {
  42. seconds = mp_obj_get_int(args[0]);
  43. }
  44. timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
  45. mp_obj_t tuple[8] = {
  46. tuple[0] = mp_obj_new_int(tm.tm_year),
  47. tuple[1] = mp_obj_new_int(tm.tm_mon),
  48. tuple[2] = mp_obj_new_int(tm.tm_mday),
  49. tuple[3] = mp_obj_new_int(tm.tm_hour),
  50. tuple[4] = mp_obj_new_int(tm.tm_min),
  51. tuple[5] = mp_obj_new_int(tm.tm_sec),
  52. tuple[6] = mp_obj_new_int(tm.tm_wday),
  53. tuple[7] = mp_obj_new_int(tm.tm_yday),
  54. };
  55. return mp_obj_new_tuple(8, tuple);
  56. }
  57. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
  58. STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
  59. size_t len;
  60. mp_obj_t *elem;
  61. mp_obj_get_array(tuple, &len, &elem);
  62. // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
  63. if (len < 8 || len > 9) {
  64. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len));
  65. }
  66. return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
  67. mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
  68. mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
  69. }
  70. STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
  71. STATIC mp_obj_t time_time(void) {
  72. struct timeval tv;
  73. gettimeofday(&tv, NULL);
  74. return mp_obj_new_int(tv.tv_sec);
  75. }
  76. MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
  77. STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
  78. { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
  79. { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
  80. { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
  81. { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
  82. { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
  83. { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
  84. { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
  85. { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
  86. { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
  87. { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
  88. { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
  89. { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
  90. };
  91. STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
  92. const mp_obj_module_t utime_module = {
  93. .base = { &mp_type_module },
  94. .globals = (mp_obj_dict_t*)&time_module_globals,
  95. };