Select Git revision
Forked from
card10 / firmware
Source project has a limited visibility.
utime.c 4.62 KiB
#include "interrupt.h"
#include "epicardium.h"
#include "mxc_delay.h"
#include "py/mpconfig.h"
#include "py/obj.h"
#include "py/runtime.h"
#include "extmod/utime_mphal.h"
#include <stdint.h>
// Needs to be after the stdint include ...
#include "lib/timeutils/timeutils.h"
/* MicroPython has its epoch at 2000-01-01. Our RTC is in UTC */
#define EPOCH_OFFSET 946684800UL
#define TZONE_OFFSET 7600UL
static mp_obj_t time_set_time(mp_obj_t secs)
{
uint64_t timestamp =
mp_obj_get_int(secs) * 1000ULL + EPOCH_OFFSET * 1000ULL;
epic_rtc_set_milliseconds(timestamp);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(time_set_time_obj, time_set_time);
static mp_obj_t time_set_unix_time(mp_obj_t secs)
{
uint64_t timestamp = mp_obj_get_int(secs) * 1000ULL;
epic_rtc_set_milliseconds(timestamp);
return mp_const_none;
}
static MP_DEFINE_CONST_FUN_OBJ_1(time_set_unix_time_obj, time_set_unix_time);
static mp_obj_t time_time(void)
{
mp_int_t seconds;
seconds = epic_rtc_get_seconds() - EPOCH_OFFSET + TZONE_OFFSET;
return mp_obj_new_int(seconds);
}
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
static mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args)
{
mp_int_t seconds;
if (n_args == 0 || args[0] == mp_const_none) {
seconds = epic_rtc_get_seconds() - EPOCH_OFFSET + TZONE_OFFSET;
} else {
seconds = mp_obj_get_int(args[0]);
}
timeutils_struct_time_t tm;
timeutils_seconds_since_2000_to_struct_time(seconds, &tm);
mp_obj_t tuple[8] = {
tuple[0] = mp_obj_new_int(tm.tm_year),
tuple[1] = mp_obj_new_int(tm.tm_mon),
tuple[2] = mp_obj_new_int(tm.tm_mday),
tuple[3] = mp_obj_new_int(tm.tm_hour),
tuple[4] = mp_obj_new_int(tm.tm_min),
tuple[5] = mp_obj_new_int(tm.tm_sec),
tuple[6] = mp_obj_new_int(tm.tm_wday),
tuple[7] = mp_obj_new_int(tm.tm_yday),
};
return mp_obj_new_tuple(8, tuple);
}
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(
time_localtime_obj, 0, 1, time_localtime