summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ports/webassembly/Makefile1
-rw-r--r--ports/webassembly/library.h1
-rw-r--r--ports/webassembly/library.js2
-rw-r--r--ports/webassembly/modtime.c51
-rw-r--r--ports/webassembly/mpconfigport.h4
-rw-r--r--ports/webassembly/mphalport.c9
-rw-r--r--ports/webassembly/mphalport.h1
7 files changed, 69 insertions, 0 deletions
diff --git a/ports/webassembly/Makefile b/ports/webassembly/Makefile
index c5ee80fe0..4e1d53b0c 100644
--- a/ports/webassembly/Makefile
+++ b/ports/webassembly/Makefile
@@ -23,6 +23,7 @@ SRC_SHARED = $(addprefix shared/,\
runtime/stdout_helpers.c \
runtime/pyexec.c \
readline/readline.c \
+ timeutils/timeutils.c \
)
SRC_C = \
diff --git a/ports/webassembly/library.h b/ports/webassembly/library.h
index 21027c137..04b408d71 100644
--- a/ports/webassembly/library.h
+++ b/ports/webassembly/library.h
@@ -29,4 +29,5 @@
extern void mp_js_write(const char *str, mp_uint_t len);
extern int mp_js_ticks_ms(void);
extern void mp_js_hook(void);
+extern double mp_js_time_ms(void);
extern uint32_t mp_js_random_u32(void);
diff --git a/ports/webassembly/library.js b/ports/webassembly/library.js
index 4a17942bb..d1266598d 100644
--- a/ports/webassembly/library.js
+++ b/ports/webassembly/library.js
@@ -64,6 +64,8 @@ mergeInto(LibraryManager.library, {
}
},
+ mp_js_time_ms: () => Date.now(),
+
// Node prior to v19 did not expose "crypto" as a global, so make sure it exists.
mp_js_random_u32__postset:
"if (globalThis.crypto === undefined) { globalThis.crypto = require('crypto'); }",
diff --git a/ports/webassembly/modtime.c b/ports/webassembly/modtime.c
new file mode 100644
index 000000000..1b1e63d4d
--- /dev/null
+++ b/ports/webassembly/modtime.c
@@ -0,0 +1,51 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2023 Damien P. George
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/obj.h"
+#include "shared/timeutils/timeutils.h"
+#include "library.h"
+
+// Return the localtime as an 8-tuple.
+static mp_obj_t mp_time_localtime_get(void) {
+ timeutils_struct_time_t tm;
+ timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, &tm);
+ mp_obj_t tuple[8] = {
+ mp_obj_new_int(tm.tm_year),
+ mp_obj_new_int(tm.tm_mon),
+ mp_obj_new_int(tm.tm_mday),
+ mp_obj_new_int(tm.tm_hour),
+ mp_obj_new_int(tm.tm_min),
+ mp_obj_new_int(tm.tm_sec),
+ mp_obj_new_int(tm.tm_wday),
+ mp_obj_new_int(tm.tm_yday),
+ };
+ return mp_obj_new_tuple(8, tuple);
+}
+
+// Returns the number of seconds, as a float, since the Epoch.
+static mp_obj_t mp_time_time_get(void) {
+ return mp_obj_new_float((mp_float_t)mp_hal_time_ms() / 1000);
+}
diff --git a/ports/webassembly/mpconfigport.h b/ports/webassembly/mpconfigport.h
index 43a029c53..79537dd72 100644
--- a/ports/webassembly/mpconfigport.h
+++ b/ports/webassembly/mpconfigport.h
@@ -50,7 +50,11 @@
#define MICROPY_USE_INTERNAL_ERRNO (1)
#define MICROPY_USE_INTERNAL_PRINTF (0)
+#define MICROPY_EPOCH_IS_1970 (1)
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (mp_js_random_u32())
+#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
+#define MICROPY_PY_TIME_TIME_TIME_NS (1)
+#define MICROPY_PY_TIME_INCLUDEFILE "ports/webassembly/modtime.c"
#ifndef MICROPY_VFS
#define MICROPY_VFS (1)
#endif
diff --git a/ports/webassembly/mphalport.c b/ports/webassembly/mphalport.c
index f91a50901..72a326e30 100644
--- a/ports/webassembly/mphalport.c
+++ b/ports/webassembly/mphalport.c
@@ -56,6 +56,15 @@ mp_uint_t mp_hal_ticks_cpu(void) {
return 0;
}
+uint64_t mp_hal_time_ms(void) {
+ double mm = mp_js_time_ms();
+ return (uint64_t)mm;
+}
+
+uint64_t mp_hal_time_ns(void) {
+ return mp_hal_time_ms() * 1000000ULL;
+}
+
extern int mp_interrupt_char;
int mp_hal_get_interrupt_char(void) {
diff --git a/ports/webassembly/mphalport.h b/ports/webassembly/mphalport.h
index 1b3179698..a90de8ec5 100644
--- a/ports/webassembly/mphalport.h
+++ b/ports/webassembly/mphalport.h
@@ -35,6 +35,7 @@ void mp_hal_delay_us(mp_uint_t us);
mp_uint_t mp_hal_ticks_ms(void);
mp_uint_t mp_hal_ticks_us(void);
mp_uint_t mp_hal_ticks_cpu(void);
+uint64_t mp_hal_time_ms(void);
int mp_hal_get_interrupt_char(void);