10000 ports/embed: Implement additional time functions. · micropython/micropython@82b8679 · GitHub
[go: up one dir, main page]

Skip to content

Commit 82b8679

Browse files
committed
ports/embed: Implement additional time functions.
Optionally adds gmtime, localtime, mktime, time, time_ns to the time module, implemented using mp_hal_time_ns(). This could also be used by other ports. I'm unsure where to put modtime_mphal.h, it could also be in extmod. The important thing is that for MICROPY_PY_TIME_INCLUDEFILE to work it must be at the same path in both the port build (original source tree) and the application build (micropython_embed distribution), therefore not in ports/embed/port. It is named .h, mismatching the corresponding ports/*/modtime.c, because it must not be compiled separately, which naming it .c would make harder for users of the embed port - they would need to explicitly exclude it, whereas this way they can continue to just compile all the .c files found in the micropython_embed distribution except those in lib. Signed-off-by: Christian Walther <cwalther@gmx.ch>
1 parent 56392f9 commit 82b8679

File tree

6 files changed

+84
-0
lines changed

6 files changed

+84
-0
lines changed

examples/embedding-full/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ static const char *example_2 =
3535
"help(random)\n"
3636
"import time\n"
3737
"help(time)\n"
38+
"print('time.gmtime(736622952) = 2023-05-05T17:29:12Z:', time.gmtime(736622952))\n"
3839
"import math\n"
3940
"help(math)\n"
4041
"import frozenhello\n"

examples/embedding-full/micropython_embed.mk

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ MICROPYTHON_TOP = ../..
88
# Include modules from extmod in the output.
99
EMBED_EXTRA = extmod
1010

11+
# Include helper sources for the time module in the output.
12+
EMBED_EXTRA += \
13+
shared/timeutils/timeutils.c \
14+
shared/timeutils/timeutils.h \
15+
shared/timeutils/modtime_mphal.h
16+
1117
# Freeze Python modules.
1218
FROZEN_MANIFEST ?= manifest.py
1319

examples/embedding-full/mpconfigport.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
// Enable floating point numbers and the math module.
2323
#define MICROPY_FLOAT_IMPL (MICROPY_FLOAT_IMPL_FLOAT)
2424

25+
// Enable more functions in the time module. Requires additions to EMBED_EXTRA,
26+
// see micropython_embed.mk.
27+
#define MICROPY_PY_TIME_GMTIME_LOCALTIME_MKTIME (1)
28+
#define MICROPY_PY_TIME_TIME_TIME_NS (1)
29+
#define MICROPY_PY_TIME_INCLUDEFILE "shared/timeutils/modtime_mphal.h"
30+
2531
// Requires shared/readline/readline.h, don't bother as we have no input.
2632
#define MICROPY_PY_BUILTINS_INPUT (0)
2733

examples/embedding-full/mphal.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ mp_uint_t mp_hal_ticks_cpu(void) {
5252

5353
#endif
5454

55+
#if MICROPY_PY_TIME_TIME_TIME_NS
56+
57+
uint64_t mp_hal_time_ns(void) {
58+
// Nanoseconds since the Epoch.
59+
return 0;
60+
}
61+
62+
#endif
63+
5564
// Text-mode standard output
5665
void mp_hal_stdout_tx_strn_cooked(const char *str, size_t len) {
5766
// This is a simplistic implementation for demonstration purposes. A real

ports/embed/embed.mk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ ifeq ($(filter extmod,$(EMBED_EXTRA)),extmod)
1616
include $(TOP)/extmod/extmod.mk
1717
endif
1818

19+
# The parts of EMBED_EXTRA that name literal files and don't need special handling.
20+
EMBED_EXTRA_FILES = $(filter-out extmod,$(EMBED_EXTRA))
21+
1922
# Set the location of the MicroPython embed port.
2023
MICROPYTHON_EMBED_PORT = $(MICROPYTHON_TOP)/ports/embed
2124

@@ -57,6 +60,9 @@ endif
5760
ifneq ($(FROZEN_MANIFEST),)
5861
PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,frozen)
5962
endif
63+
ifneq ($(EMBED_EXTRA_FILES),)
64+
PACKAGE_DIR_LIST += $(addprefix $(PACKAGE_DIR)/,$(filter-out ./,$(sort $(dir $(EMBED_EXTRA_FILES)))))
65+
endif
6066

6167
.PHONY: micropython-embed-package
6268
micropython-embed-package: $(GENHDR_OUTPUT) $(FROZEN_OUTPUT)
@@ -86,6 +92,10 @@ ifneq ($(FROZEN_MANIFEST),)
8692
endif
8793
$(ECHO) "- port"
8894
$(Q)$(CP) $(MICROPYTHON_EMBED_PORT)/port/*.[ch] $(PACKAGE_DIR)/port
95+
ifneq ($(EMBED_EXTRA_FILES),)
96+
$(ECHO) "- extra"
97+
$(Q)$(foreach FILE,$(EMBED_EXTRA_FILES),$(CP) $(TOP)/$(FILE) $(dir $(PACKAGE_DIR)/$(FILE)) &&) true
98+
endif
8999

90100
# Include remaining core make rules.
91101
include $(TOP)/py/mkrules.mk

shared/timeutils/modtime_mphal.h

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2013-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 "py/mphal.h"
29+
#include "shared/timeutils/timeutils.h"
30+
31+
// Return the localtime as an 8-tuple.
32+
STATIC mp_obj_t mp_time_localtime_get(void) {
33+
mp_int_t seconds = mp_hal_time_ns() / 1000000000;
34+
timeutils_struct_time_t tm;
35+
timeutils_seconds_since_epoch_to_struct_time(seconds, &tm);
36+
mp_obj_t tuple[8] = {
37+
tuple[0] = mp_obj_new_int(tm.tm_year),
38+
tuple[1] = mp_obj_new_int(tm.tm_mon),
39+
tuple[2] = mp_obj_new_int(tm.tm_mday),
40+
tuple[3] = mp_obj_new_int(tm.tm_hour),
41+
tuple[4] = mp_obj_new_int(tm.tm_min),
42+
tuple[5] = mp_obj_new_int(tm.tm_sec),
43+
tuple[6] = mp_obj_new_int(tm.tm_wday),
44+
tuple[7] = mp_obj_new_int(tm.tm_yday),
45+
};
46+
return mp_obj_new_tuple(8, tuple);
47+
}
48+
49+
// Returns the number of seconds, as an integer, since the Epoch.
50+
STATIC mp_obj_t mp_time_time_get(void) {
51+
return mp_obj_new_int(mp_hal_time_ns() / 1000000000);
52+
}

0 commit comments

Comments
 (0)
0