From f627f5e50695477d0ae3af98e437908401559a99 Mon Sep 17 00:00:00 2001
From: Ferdinand Bachmann <theferdi265@gmail.com>
Date: Fri, 30 Aug 2019 22:45:19 +0200
Subject: [PATCH] pycardium/utime: add set_time_ms and set_unix_time_ms

---
 pycardium/modules/qstrdefs.h |  2 ++
 pycardium/modules/utime.c    | 24 ++++++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/pycardium/modules/qstrdefs.h b/pycardium/modules/qstrdefs.h
index 68c40810e..010681524 100644
--- a/pycardium/modules/qstrdefs.h
+++ b/pycardium/modules/qstrdefs.h
@@ -50,7 +50,9 @@ Q(mktime)
 Q(time)
 Q(time_ms)
 Q(set_time)
+Q(set_time_ms)
 Q(set_unix_time)
+Q(set_unix_time_ms)
 
 /* vibra */
 Q(vibra)
diff --git a/pycardium/modules/utime.c b/pycardium/modules/utime.c
index 4b06a9cd5..585a5d20c 100644
--- a/pycardium/modules/utime.c
+++ b/pycardium/modules/utime.c
@@ -5,6 +5,7 @@
 
 #include "py/mpconfig.h"
 #include "py/obj.h"
+#include "py/objint.h"
 #include "py/runtime.h"
 #include "extmod/utime_mphal.h"
 
@@ -26,6 +27,17 @@ static mp_obj_t time_set_time(mp_obj_t secs)
 }
 static MP_DEFINE_CONST_FUN_OBJ_1(time_set_time_obj, time_set_time);
 
+static mp_obj_t time_set_time_ms(mp_obj_t msecs_obj)
+{
+	uint64_t msecs = 0;
+	mp_obj_int_to_bytes_impl(msecs_obj, false, 8, (byte*)&msecs);
+	uint64_t timestamp = msecs +
+			     EPOCH_OFFSET * 1000ULL - TZONE_OFFSET * 1000ULL;
+	epic_rtc_set_milliseconds(timestamp);
+	return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(time_set_time_ms_obj, time_set_time_ms);
+
 static mp_obj_t time_set_unix_time(mp_obj_t secs)
 {
 	uint64_t timestamp = mp_obj_get_int(secs) * 1000ULL;
@@ -34,6 +46,15 @@ static mp_obj_t time_set_unix_time(mp_obj_t secs)
 }
 static MP_DEFINE_CONST_FUN_OBJ_1(time_set_unix_time_obj, time_set_unix_time);
 
+static mp_obj_t time_set_unix_time_ms(mp_obj_t msecs_obj)
+{
+	uint64_t timestamp = 0;
+	mp_obj_int_to_bytes_impl(msecs_obj, false, 8, (byte*)&timestamp);
+	epic_rtc_set_milliseconds(timestamp);
+	return mp_const_none;
+}
+static MP_DEFINE_CONST_FUN_OBJ_1(time_set_unix_time_ms_obj, time_set_unix_time_ms);
+
 static mp_obj_t time_time(void)
 {
 	mp_int_t seconds;
@@ -131,8 +152,11 @@ static const mp_rom_map_elem_t time_module_globals_table[] = {
 	{ MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_time_ms), MP_ROM_PTR(&time_time_ms_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_set_time), MP_ROM_PTR(&time_set_time_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_set_time_ms), MP_ROM_PTR(&time_set_time_ms_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_set_unix_time),
 	  MP_ROM_PTR(&time_set_unix_time_obj) },
+	{ MP_ROM_QSTR(MP_QSTR_set_unix_time_ms),
+	  MP_ROM_PTR(&time_set_unix_time_ms_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) },
 	{ MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&mp_utime_sleep_obj) },
-- 
GitLab