webassembly: Enable time localtime, gmtime, time, time_ns.

Signed-off-by: Damien George <damien@micropython.org>
pull/13583/head
Damien George 2023-12-13 13:48:32 +11:00
rodzic 76898cbfa1
commit 8e3b701dee
7 zmienionych plików z 69 dodań i 0 usunięć

Wyświetl plik

@ -23,6 +23,7 @@ SRC_SHARED = $(addprefix shared/,\
runtime/stdout_helpers.c \
runtime/pyexec.c \
readline/readline.c \
timeutils/timeutils.c \
)
SRC_C = \

Wyświetl plik

@ -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);

Wyświetl plik

@ -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'); }",

Wyświetl plik

@ -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);
}

Wyświetl plik

@ -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

Wyświetl plik

@ -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) {

Wyświetl plik

@ -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);