Skip to content

Commit 8e3b701

Browse files
committed
webassembly: Enable time localtime, gmtime, time, time_ns.
Signed-off-by: Damien George <[email protected]>
1 parent 76898cb commit 8e3b701

File tree

7 files changed

+69
-0
lines changed

7 files changed

+69
-0
lines changed

ports/webassembly/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ SRC_SHARED = $(addprefix shared/,\
2323
runtime/stdout_helpers.c \
2424
runtime/pyexec.c \
2525
readline/readline.c \
26+
timeutils/timeutils.c \
2627
)
2728

2829
SRC_C = \

ports/webassembly/library.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@
2929
extern void mp_js_write(const char *str, mp_uint_t len);
3030
extern int mp_js_ticks_ms(void);
3131
extern void mp_js_hook(void);
32+
extern double mp_js_time_ms(void);
3233
extern uint32_t mp_js_random_u32(void);

ports/webassembly/library.js

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ mergeInto(LibraryManager.library, {
6464
}
6565
},
6666

67+
mp_js_time_ms: () => Date.now(),
68+
6769
// Node prior to v19 did not expose "crypto" as a global, so make sure it exists.
6870
mp_js_random_u32__postset:
6971
"if (globalThis.crypto === undefined) { globalThis.crypto = require('crypto'); }",

ports/webassembly/modtime.c

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2023 Damien P. George
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/obj.h"
28+
#include "shared/timeutils/timeutils.h"
29+
#include "library.h"
30+
31+
// Return the localtime as an 8-tuple.
32+
static mp_obj_t mp_time_localtime_get(void) {
33+
timeutils_struct_time_t tm;
34+
timeutils_seconds_since_epoch_to_struct_time(mp_hal_time_ms() / 1000, &tm);
35+
mp_obj_t tuple[8] = {
36+
mp_obj_new_int(tm.tm_year),
37+
mp_obj_new_int(tm.tm_mon),
38+
mp_obj_new_int(tm.tm_mday),
39+
mp_obj_new_int(tm.tm_hour),
40+
mp_obj_new_int(tm.tm_min),
41+
mp_obj_new_int(tm.tm_sec),
42+
mp_obj_new_int(tm.tm_wday),
43+
mp_obj_new_int(tm.tm_yday),
44+
};
45+
return mp_obj_new_tuple(8, tuple);
46+
}
47+
48+
// Returns the number of seconds, as a float, since the Epoch.
49+
static mp_obj_t mp_time_time_get(void) {
50+
return mp_obj_new_float((mp_float_t)mp_hal_time_ms() / 1000);
51+
}

ports/webassembly/mpconfigport.h

+4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@
5050
#define MICROPY_USE_INTERNAL_ERRNO (1)
5151
#define MICROPY_USE_INTERNAL_PRINTF (0)
5252

53+
#define MICROPY_EPOCH_IS_1970 (1)
5354
#define MICROPY_PY_RANDOM_SEED_INIT_FUNC (mp_js_random_u32())
55+
#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
56+
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
57+
#define MICROPY_PY_TIME_INCLUDEFILE "ports/webassembly/modtime.c"
5458
#ifndef MICROPY_VFS
5559
#define MICROPY_VFS (1)
5660
#endif

ports/webassembly/mphalport.c

+9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ mp_uint_t mp_hal_ticks_cpu(void) {
5656
return 0;
5757
}
5858

59+
uint64_t mp_hal_time_ms(void) {
60+
double mm = mp_js_time_ms();
61+
return (uint64_t)mm;
62+
}
63+
64+
uint64_t mp_hal_time_ns(void) {
65+
return mp_hal_time_ms() * 1000000ULL;
66+
}
67+
5968
extern int mp_interrupt_char;
6069

6170
int mp_hal_get_interrupt_char(void) {

ports/webassembly/mphalport.h

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ void mp_hal_delay_us(mp_uint_t us);
3535
mp_uint_t mp_hal_ticks_ms(void);
3636
mp_uint_t mp_hal_ticks_us(void);
3737
mp_uint_t mp_hal_ticks_cpu(void);
38+
uint64_t mp_hal_time_ms(void);
3839

3940
int mp_hal_get_interrupt_char(void);
4041

0 commit comments

Comments
 (0)