Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 1 | /* |
Alexander Steffen | 55f3324 | 2017-06-30 09:22:17 +0200 | [diff] [blame^] | 2 | * This file is part of the MicroPython project, http://micropython.org/ |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2013, 2014 Damien P. George |
| 7 | * Copyright (c) 2015 Josef Gajdusek |
| 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 <stdio.h> |
| 29 | #include <string.h> |
| 30 | |
| 31 | #include "py/nlr.h" |
| 32 | #include "py/obj.h" |
| 33 | #include "py/gc.h" |
| 34 | #include "py/runtime.h" |
Damien George | 731f359 | 2015-10-30 23:03:58 +0000 | [diff] [blame] | 35 | #include "py/mphal.h" |
Damien George | 5788499 | 2015-12-28 17:28:16 +0000 | [diff] [blame] | 36 | #include "py/smallint.h" |
Damien George | b6c7e4b | 2017-03-31 22:29:39 +1100 | [diff] [blame] | 37 | #include "lib/timeutils/timeutils.h" |
Paul Sokolovsky | 8bc3fc2 | 2016-11-06 01:30:19 +0300 | [diff] [blame] | 38 | #include "modmachine.h" |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 39 | #include "user_interface.h" |
Paul Sokolovsky | a972844 | 2016-10-14 20:13:02 +0300 | [diff] [blame] | 40 | #include "extmod/utime_mphal.h" |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 41 | |
| 42 | /// \module time - time related functions |
| 43 | /// |
| 44 | /// The `time` module provides functions for getting the current time and date, |
| 45 | /// and for sleeping. |
| 46 | |
| 47 | /// \function localtime([secs]) |
| 48 | /// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which |
| 49 | /// contains: (year, month, mday, hour, minute, second, weekday, yearday) |
| 50 | /// If secs is not provided or None, then the current time from the RTC is used. |
| 51 | /// year includes the century (for example 2014) |
| 52 | /// month is 1-12 |
| 53 | /// mday is 1-31 |
| 54 | /// hour is 0-23 |
| 55 | /// minute is 0-59 |
| 56 | /// second is 0-59 |
| 57 | /// weekday is 0-6 for Mon-Sun. |
| 58 | /// yearday is 1-366 |
| 59 | STATIC mp_obj_t time_localtime(mp_uint_t n_args, const mp_obj_t *args) { |
| 60 | timeutils_struct_time_t tm; |
| 61 | mp_int_t seconds; |
| 62 | if (n_args == 0 || args[0] == mp_const_none) { |
| 63 | seconds = pyb_rtc_get_us_since_2000() / 1000 / 1000; |
| 64 | } else { |
| 65 | seconds = mp_obj_get_int(args[0]); |
| 66 | } |
| 67 | timeutils_seconds_since_2000_to_struct_time(seconds, &tm); |
| 68 | mp_obj_t tuple[8] = { |
| 69 | tuple[0] = mp_obj_new_int(tm.tm_year), |
| 70 | tuple[1] = mp_obj_new_int(tm.tm_mon), |
| 71 | tuple[2] = mp_obj_new_int(tm.tm_mday), |
| 72 | tuple[3] = mp_obj_new_int(tm.tm_hour), |
| 73 | tuple[4] = mp_obj_new_int(tm.tm_min), |
| 74 | tuple[5] = mp_obj_new_int(tm.tm_sec), |
| 75 | tuple[6] = mp_obj_new_int(tm.tm_wday), |
| 76 | tuple[7] = mp_obj_new_int(tm.tm_yday), |
| 77 | }; |
| 78 | return mp_obj_new_tuple(8, tuple); |
| 79 | } |
| 80 | MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime); |
| 81 | |
| 82 | /// \function mktime() |
| 83 | /// This is inverse function of localtime. It's argument is a full 8-tuple |
| 84 | /// which expresses a time as per localtime. It returns an integer which is |
| 85 | /// the number of seconds since Jan 1, 2000. |
| 86 | STATIC mp_obj_t time_mktime(mp_obj_t tuple) { |
Damien George | 1d7e311 | 2017-03-26 17:18:25 +1100 | [diff] [blame] | 87 | size_t len; |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 88 | mp_obj_t *elem; |
| 89 | mp_obj_get_array(tuple, &len, &elem); |
| 90 | |
| 91 | // localtime generates a tuple of len 8. CPython uses 9, so we accept both. |
| 92 | if (len < 8 || len > 9) { |
| 93 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "mktime needs a tuple of length 8 or 9 (%d given)", len)); |
| 94 | } |
| 95 | |
| 96 | return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), |
| 97 | mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]), |
| 98 | mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); |
| 99 | } |
| 100 | MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); |
| 101 | |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 102 | /// \function time() |
| 103 | /// Returns the number of seconds, as an integer, since 1/1/2000. |
| 104 | STATIC mp_obj_t time_time(void) { |
| 105 | // get date and time |
| 106 | return mp_obj_new_int(pyb_rtc_get_us_since_2000() / 1000 / 1000); |
| 107 | } |
| 108 | MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); |
| 109 | |
Paul Sokolovsky | 64c6bdb | 2016-10-29 18:48:04 +0300 | [diff] [blame] | 110 | STATIC const mp_rom_map_elem_t time_module_globals_table[] = { |
| 111 | { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) }, |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 112 | |
Paul Sokolovsky | 64c6bdb | 2016-10-29 18:48:04 +0300 | [diff] [blame] | 113 | { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, |
| 114 | { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, |
| 115 | { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) }, |
| 116 | { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) }, |
| 117 | { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) }, |
| 118 | { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) }, |
| 119 | { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) }, |
| 120 | { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) }, |
Paul Sokolovsky | d86cac4 | 2016-10-29 17:30:05 +0300 | [diff] [blame] | 121 | { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) }, |
Paul Sokolovsky | 64c6bdb | 2016-10-29 18:48:04 +0300 | [diff] [blame] | 122 | { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) }, |
| 123 | { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 124 | }; |
| 125 | |
| 126 | STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); |
| 127 | |
| 128 | const mp_obj_module_t utime_module = { |
| 129 | .base = { &mp_type_module }, |
Josef Gajdusek | 103d12a | 2015-05-11 21:11:37 +0200 | [diff] [blame] | 130 | .globals = (mp_obj_dict_t*)&time_module_globals, |
| 131 | }; |