6
6
* The MIT License (MIT)
7
7
*
8
8
* Copyright (c) 2016 Damien P. George
9
+ * Copyright (c) 2015 Josef Gajdusek
9
10
*
10
11
* Permission is hereby granted, free of charge, to any person obtaining a copy
11
12
* of this software and associated documentation files (the "Software"), to deal
29
30
#include <stdio.h>
30
31
#include <string.h>
31
32
#include <sys/time.h>
33
+ #include <py/nlr.h>
34
+ #include <py/smallint.h>
35
+ #include "timeutils.h"
32
36
33
37
#include "extmod/utime_mphal.h"
34
38
39
+ /// \module time - time related functions
40
+ ///
41
+ /// The `time` module provides functions for getting the current time and date,
42
+ /// and for sleeping.
43
+
44
+ /// \function localtime([secs])
45
+ /// Convert a time expressed in seconds since Jan 1, 2000 into an 8-tuple which
46
+ /// contains: (year, month, mday, hour, minute, second, weekday, yearday)
47
+ /// If secs is not provided or None, then the current time from the RTC is used.
48
+ /// year includes the century (for example 2014)
49
+ /// month is 1-12
50
+ /// mday is 1-31
51
+ /// hour is 0-23
52
+ /// minute is 0-59
53
+ /// second is 0-59
54
+ /// weekday is 0-6 for Mon-Sun.
55
+ /// yearday is 1-366
56
+ STATIC mp_obj_t time_localtime (size_t n_args , const mp_obj_t * args ) {
57
+ timeutils_struct_time_t tm ;
58
+ mp_int_t seconds ;
59
+ if (n_args == 0 || args [0 ] == mp_const_none ) {
60
+ struct timeval tv ;
61
+ gettimeofday (& tv , NULL );
62
+ seconds = tv .tv_sec ;
63
+ } else {
64
+ seconds = mp_obj_get_int (args [0 ]);
65
+ }
66
+ timeutils_seconds_since_2000_to_struct_time (seconds , & tm );
67
+ mp_obj_t tuple [8 ] = {
68
+ tuple [0 ] = mp_obj_new_int (tm .tm_year ),
69
+ tuple [1 ] = mp_obj_new_int (tm .tm_mon ),
70
+ tuple [2 ] = mp_obj_new_int (tm .tm_mday ),
71
+ tuple [3 ] = mp_obj_new_int (tm .tm_hour ),
72
+ tuple [4 ] = mp_obj_new_int (tm .tm_min ),
73
+ tuple [5 ] = mp_obj_new_int (tm .tm_sec ),
74
+ tuple [6 ] = mp_obj_new_int (tm .tm_wday ),
75
+ tuple [7 ] = mp_obj_new_int (tm .tm_yday ),
76
+ };
77
+ return mp_obj_new_tuple (8 , tuple );
78
+ }
79
+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (time_localtime_obj , 0 , 1 , time_localtime );
80
+
81
+ /// \function mktime()
82
+ /// This is inverse function of localtime. It's argument is a full 8-tuple
83
+ /// which expresses a time as per localtime. It returns an integer which is
84
+ /// the number of seconds since Jan 1, 2000.
85
+ STATIC mp_obj_t time_mktime (mp_obj_t tuple ) {
86
+ size_t len ;
87
+ mp_obj_t * elem ;
88
+ mp_obj_get_array (tuple , & len , & elem );
89
+
90
+ // localtime generates a tuple of len 8. CPython uses 9, so we accept both.
91
+ if (len < 8 || len > 9 ) {
92
+ nlr_raise (mp_obj_new_exception_msg_varg (& mp_type_TypeError , "mktime needs a tuple of length 8 or 9 (%d given)" , len ));
93
+ }
94
+
95
+ return mp_obj_new_int_from_uint (timeutils_mktime (mp_obj_get_int (elem [0 ]),
96
+ mp_obj_get_int (elem [1 ]), mp_obj_get_int (elem [2 ]), mp_obj_get_int (elem [3 ]),
97
+ mp_obj_get_int (elem [4 ]), mp_obj_get_int (elem [5 ])));
98
+ }
99
+ MP_DEFINE_CONST_FUN_OBJ_1 (time_mktime_obj , time_mktime );
100
+
35
101
STATIC mp_obj_t time_time (void ) {
36
102
struct timeval tv ;
37
103
gettimeofday (& tv , NULL );
@@ -42,6 +108,8 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
42
108
STATIC const mp_rom_map_elem_t time_module_globals_table [] = {
43
109
{ MP_ROM_QSTR (MP_QSTR___name__ ), MP_ROM_QSTR (MP_QSTR_utime ) },
44
110
111
+ { MP_ROM_QSTR (MP_QSTR_localtime ), MP_ROM_PTR (& time_localtime_obj ) },
112
+ { MP_ROM_QSTR (MP_QSTR_mktime ), MP_ROM_PTR (& time_mktime_obj ) },
45
113
{ MP_ROM_QSTR (MP_QSTR_time ), MP_ROM_PTR (& time_time_obj ) },
46
114
{ MP_ROM_QSTR (MP_QSTR_sleep ), MP_ROM_PTR (& mp_utime_sleep_obj ) },
47
115
{ MP_ROM_QSTR (MP_QSTR_sleep_ms ), MP_ROM_PTR (& mp_utime_sleep_ms_obj ) },
0 commit comments