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 
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include "py/runtime.h"
31 #include "py/smallint.h"
32 #include "py/obj.h"
33 #include "shared/timeutils/timeutils.h"
34 #include "extmod/utime_mphal.h"
35 #include "time.h"
36 
37 // #include "aos_system.h"
38 #include "amp_platform.h"
39 
40 #if MICROPY_PY_UTIME
41 
time_localtime(size_t n_args,const mp_obj_t * args)42 STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) {
43     timeutils_struct_time_t tm;
44     mp_int_t seconds;
45     if (n_args == 0 || args[0] == mp_const_none) {
46         struct timeval tv;
47         gettimeofday(&tv, NULL);
48         seconds = tv.tv_sec;
49     } else {
50         seconds = mp_obj_get_int(args[0]);
51     }
52     timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
53     mp_obj_t tuple[8] = {
54         tuple[0] = mp_obj_new_int(tm.tm_year),
55         tuple[1] = mp_obj_new_int(tm.tm_mon),
56         tuple[2] = mp_obj_new_int(tm.tm_mday),
57         tuple[3] = mp_obj_new_int(tm.tm_hour),
58         tuple[4] = mp_obj_new_int(tm.tm_min),
59         tuple[5] = mp_obj_new_int(tm.tm_sec),
60         tuple[6] = mp_obj_new_int(tm.tm_wday),
61         tuple[7] = mp_obj_new_int(tm.tm_yday),
62     };
63     return mp_obj_new_tuple(8, tuple);
64 }
65 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(time_localtime_obj, 0, 1, time_localtime);
66 
time_mktime(mp_obj_t tuple)67 STATIC mp_obj_t time_mktime(mp_obj_t tuple) {
68     size_t len;
69     mp_obj_t *elem;
70     mp_obj_get_array(tuple, &len, &elem);
71 
72     // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
73     if (len < 8 || len > 9) {
74         mp_raise_msg_varg(&mp_type_TypeError, MP_ERROR_TEXT("mktime needs a tuple of length 8 or 9 (%d given)"), len);
75     }
76 
77     return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]),
78         mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), mp_obj_get_int(elem[3]),
79         mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])));
80 }
81 STATIC MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime);
82 
time_time(void)83 STATIC mp_obj_t time_time(void) {
84     struct timeval tv;
85     gettimeofday(&tv, NULL);
86     return mp_obj_new_int(tv.tv_sec);
87 }
88 MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
89 
90 STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
91     { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
92 
93     { MP_ROM_QSTR(MP_QSTR_gmtime), MP_ROM_PTR(&time_localtime_obj) },
94     { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
95     { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
96     { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
97     { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
98     { MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },
99     { MP_ROM_QSTR(MP_QSTR_sleep_us), MP_ROM_PTR(&mp_utime_sleep_us_obj) },
100     { MP_ROM_QSTR(MP_QSTR_ticks_ms), MP_ROM_PTR(&mp_utime_ticks_ms_obj) },
101     { MP_ROM_QSTR(MP_QSTR_ticks_us), MP_ROM_PTR(&mp_utime_ticks_us_obj) },
102     { MP_ROM_QSTR(MP_QSTR_ticks_cpu), MP_ROM_PTR(&mp_utime_ticks_cpu_obj) },
103     { MP_ROM_QSTR(MP_QSTR_ticks_add), MP_ROM_PTR(&mp_utime_ticks_add_obj) },
104     { MP_ROM_QSTR(MP_QSTR_ticks_diff), MP_ROM_PTR(&mp_utime_ticks_diff_obj) },
105     { MP_ROM_QSTR(MP_QSTR_time_ns), MP_ROM_PTR(&mp_utime_time_ns_obj) },
106 };
107 
108 STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table);
109 
110 const mp_obj_module_t utime_module = {
111     .base = { &mp_type_module },
112     .globals = (mp_obj_dict_t *)&time_module_globals,
113 };
114 
115 #endif
116