8000 esp32/modutime: add time_us and add us to settime · micropython/micropython@838998b · GitHub
[go: up one dir, main page]

Skip to content

Commit 838998b

Browse files
committed
esp32/modutime: add time_us and add us to settime
1 parent 0aebcec commit 838998b

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

ports/esp32/modutime.c

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ STATIC mp_obj_t time_time(void) {
151151
}
152152
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
153153

154+
STATIC mp_obj_t time_time_us(void) {
155+
struct timeval tv;
156+
if (gettimeofday(&tv, NULL) != 0) {
157+
mp_raise_OSError(errno);
158+
}
159+
long long usec = ((long long)tv.tv_sec - EPOCH_DELTA) * 1000000 + (long long)tv.tv_usec;
160+
return mp_obj_new_int_from_ll(usec);
161+
}
162+
MP_DEFINE_CONST_FUN_OBJ_0(time_time_us_obj, time_time_us);
163+
154164
STATIC mp_obj_t time_tzset(mp_obj_t tz) {
155165
// tz is something like PST+8PDT,M3.2.0/2,M11.1.0/2
156166
const char *zone = mp_obj_str_get_str(tz);
@@ -160,14 +170,16 @@ STATIC mp_obj_t time_tzset(mp_obj_t tz) {
160170
}
161171
MP_DEFINE_CONST_FUN_OBJ_1(time_tzset_obj, time_tzset);
162172

163-
STATIC mp_obj_t time_settime(const mp_obj_t seconds_in) {
164-
struct timeval tv = { mp_obj_get_int(seconds_in) + EPOCH_DELTA, 0 };
173+
STATIC mp_obj_t time_settime(const mp_obj_t secs_in, const mp_obj_t usecs_in) {
174+
mp_int_t secs = mp_obj_get_int(secs_in);
175+
mp_int_t usecs = mp_obj_get_int(usecs_in);
176+
struct timeval tv = { secs + EPOCH_DELTA, usecs };
165177
if (settimeofday(&tv, NULL) != 0) {
166178
mp_raise_OSError(errno);
167179
}
168180
return mp_const_none;
169181
}
170-
MP_DEFINE_CONST_FUN_OBJ_1(time_settime_obj, time_settime);
182+
MP_DEFINE_CONST_FUN_OBJ_2(time_settime_obj, time_settime);
171183

172184
STATIC mp_obj_t time_adjtime(const mp_obj_t microseconds_in) {
173185
// esp-idf is adjtime is broken in that it returns the current adjustment instead of
@@ -203,6 +215,7 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = {
203215
{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
204216
{ MP_ROM_QSTR(MP_QSTR_tzset), MP_ROM_PTR(&time_tzset_obj) },
205217
{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
218+
{ MP_ROM_QSTR(MP_QSTR_time_us), MP_ROM_PTR(&time_time_us_obj) },
206219
{ MP_ROM_QSTR(MP_QSTR_settime), MP_ROM_PTR(&time_settime_obj) },
207220
{ MP_ROM_QSTR(MP_QSTR_adjtime), MP_ROM_PTR(&time_adjtime_obj) },
208221
{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },

tests/esp32/utime_settime.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def ptup(tup):
5050
for adj in [1000, 0, -1000, 1000000, -1000000, 2300000, -2800000]:
5151
print("---", adj)
5252
time.tzset("GMT+0")
53-
time.settime(time.mktime((2020, 4, 1, 10, 23, 45, 0, 0, 0)))
53+
time.settime(time.mktime((2020, 4, 1, 10, 23, 45, 0, 0, 0)), 0)
5454
ptup(time.localtime())
5555
oadj = time.adjtime(adj)
5656
print(oadj)
@@ -67,3 +67,9 @@ def ptup(tup):
6767
print(oadj)
6868
except ValueError as e:
6969
print(e)
70+
71+
# verify that settime and time_us handle sub-second precision
72+
t1 = time.mktime((2020, 4, 1, 10, 23, 45, 0, 0, 0))
73+
time.settime(t1, 500000)
74+
td = time.time_us() - t1 * 1000000 - 500000
75+
print("frac settime", td > 0 and td < 600000)

tests/esp32/utime_settime.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,4 @@ True
4141
True
4242
[2020, 4, 1, 10, 23, 45, 2, 92, 0]
4343
adjustment too big
44+
frac settime True

0 commit comments

Comments
 (0)
0