8000 ports/esp32/modutime.c: add in localtime and mktime · nickzoic/micropython-esp32@30d86f7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 30d86f7

Browse files
committed
ports/esp32/modutime.c: add in localtime and mktime
Uses code copied directly from Josef Gajdusek's contribution to the esp8266 port modifed to work on esp-idf.
1 parent 090b6b8 commit 30d86f7

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

ports/esp32/modutime.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* The MIT License (MIT)
77
*
88
* Copyright (c) 2016 Damien P. George
9+
* Copyright (c) 2015 Josef Gajdusek
910
*
1011
* Permission is hereby granted, free of charge, to any person obtaining a copy
1112
* of this software and associated documentation files (the "Software"), to deal
@@ -29,9 +30,74 @@
2930
#include <stdio.h>
3031
#include <string.h>
3132
#include <sys/time.h>
33+
#include <py/nlr.h>
34+
#include <py/smallint.h>
35+
#include "timeutils.h"
3236

3337
#include "extmod/utime_mphal.h"
3438

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+
35101
STATIC mp_obj_t time_time(void) {
36102
struct timeval tv;
37103
gettimeofday(&tv, NULL);
@@ -42,6 +108,8 @@ MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
42108
STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
43109
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_utime) },
44110

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) },
45113
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
46114
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
47115
{ MP_ROM_QSTR(MP_QSTR_sleep_ms), MP_ROM_PTR(&mp_utime_sleep_ms_obj) },

0 commit comments

Comments
 (0)
0