diff options
| author | Scott Shawcroft <scott@tannewt.org> | 2018-05-19 12:30:34 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2018-05-19 12:30:34 -0500 |
| commit | be12e07d7437b9f2a75659bc944c15cccc8e93c1 (patch) | |
| tree | c77237634500b7d093b29af7ee86b512bb906b39 | |
| parent | 14ca3dc42f68f778952ca36bc0e54df52d64a316 (diff) | |
| parent | 95e70cd0ea6d91a235633832b03a7a2137f4b3d6 (diff) | |
Merge pull request #846 from notro/epoch1970
time: Use 1970 epoch
11 files changed, 49 insertions, 9 deletions
diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h index 3d97e2cb1..44da28eae 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit CircuitPlayground Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + // Don't allow touch on A0 (PA02), because it's connected to the speaker. #define PA02_NO_TOUCH (true) diff --git a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk index 4d6e32eb7..3b341c32c 100644 --- a/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/circuitplayground_express/mpconfigboard.mk @@ -9,6 +9,8 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz + # Include these Python libraries in firmware. FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_CircuitPlayground diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h index bfa8c642a..44141773d 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Feather M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define MICROPY_HW_NEOPIXEL (&pin_PA06) // Clock rates are off: Salae reads 12MHz which is the limit even though we set it to the safer 8MHz. diff --git a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk index 909922dbe..202d8a626 100644 --- a/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/feather_m0_express/mpconfigboard.mk @@ -8,3 +8,5 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 + +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h index be59bc5d4..fea2d290b 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Itsy Bitsy M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define CIRCUITPY_BITBANG_APA102 #define MICROPY_HW_APA102_MOSI (&pin_PA01) #define MICROPY_HW_APA102_SCK (&pin_PA00) diff --git a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk index 0fb0e38ed..b3e5a636d 100644 --- a/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/itsybitsy_m0_express/mpconfigboard.mk @@ -9,3 +9,4 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h index f798c5d36..4825b6663 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.h @@ -1,6 +1,9 @@ #define MICROPY_HW_BOARD_NAME "Adafruit Metro M0 Express" #define MICROPY_HW_MCU_NAME "samd21g18" +#define MICROPY_LONGINT_IMPL MICROPY_LONGINT_IMPL_MPZ +#define MP_SSIZE_MAX 0x7fffffff + #define MICROPY_HW_LED_TX PIN_PA27 //#define MICROPY_HW_LED_RX PIN_PA31 diff --git a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk index 79d60380d..ee1335c9a 100644 --- a/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk +++ b/ports/atmel-samd/boards/metro_m0_express/mpconfigboard.mk @@ -8,3 +8,5 @@ SPI_FLASH_FILESYSTEM = 1 CHIP_VARIANT = SAMD21G18A CHIP_FAMILY = samd21 + +MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/ports/atmel-samd/mpconfigport.h b/ports/atmel-samd/mpconfigport.h index 27c248001..8e726f9b7 100644 --- a/ports/atmel-samd/mpconfigport.h +++ b/ports/atmel-samd/mpconfigport.h @@ -141,8 +141,10 @@ typedef long mp_off_t; #define MICROPY_PY_SYS_PLATFORM "Atmel SAMD21" #define PORT_HEAP_SIZE (16384 + 4096) // If you change MICROPY_LONGINT_IMPL, also change MPY_TOOL_LONGINT_IMPL in mpconfigport.mk. +#ifndef MICROPY_LONGINT_IMPL #define MICROPY_LONGINT_IMPL (MICROPY_LONGINT_IMPL_NONE) #endif +#endif #ifdef SAMD51 #define CIRCUITPY_MCU_FAMILY samd51 diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 0f3f399e2..80ec9d978 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -2,8 +2,10 @@ # $(MPY-TOOL) needs to know what kind of longint to use (if any) to freeze long integers. # This should correspond to the MICROPY_LONGINT_IMPL definition in mpconfigport.h. ifeq ($(CHIP_FAMILY), samd21) +ifndef MPY_TOOL_LONGINT_IMPL MPY_TOOL_LONGINT_IMPL = -mlongint-impl=none endif +endif ifeq ($(CHIP_FAMILY), samd51) MPY_TOOL_LONGINT_IMPL = -mlongint-impl=mpz diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c index b4137706e..a105941c0 100644 --- a/shared-bindings/time/__init__.c +++ b/shared-bindings/time/__init__.c @@ -34,6 +34,8 @@ #include "shared-bindings/rtc/__init__.h" #include "shared-bindings/time/__init__.h" +#define EPOCH1970_EPOCH2000_DIFF_SECS 946684800 + //| :mod:`time` --- time and timing related functions //| ======================================================== //| @@ -183,13 +185,14 @@ void struct_time_to_tm(mp_obj_t t, timeutils_struct_time_t *tm) { // elems[8] tm_isdst is not supported } +#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE mp_obj_t MP_WEAK rtc_get_time_source_time(void) { mp_raise_RuntimeError("RTC is not supported on this board"); } //| .. method:: time() //| -//| Return the current time in seconds since since Jan 1, 2000. +//| Return the current time in seconds since since Jan 1, 1970. //| //| :return: the current time //| :rtype: int @@ -197,17 +200,18 @@ mp_obj_t MP_WEAK rtc_get_time_source_time(void) { STATIC mp_obj_t time_time(void) { timeutils_struct_time_t tm; struct_time_to_tm(rtc_get_time_source_time(), &tm); - mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, // mp_uint_t date + mp_uint_t secs = timeutils_seconds_since_2000(tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); - return mp_obj_new_int_from_uint(secs); + return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS); } MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time); //| .. method:: localtime([secs]) //| -//| Convert a time expressed in seconds since Jan 1, 2000 to a struct_time in +//| Convert a time expressed in seconds since Jan 1, 1970 to a struct_time in //| local time. If secs is not provided or None, the current time as returned //| by time() is used. +//| The earliest date for which it can generate a time is Jan 1, 2000. //| //| :return: the current time //| :rtype: time.struct_time @@ -217,8 +221,12 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { return rtc_get_time_source_time(); } + mp_int_t secs = mp_obj_int_get_checked(args[0]); + if (secs < EPOCH1970_EPOCH2000_DIFF_SECS) + mp_raise_msg(&mp_type_OverflowError, "timestamp out of range for platform time_t"); + timeutils_struct_time_t tm; - timeutils_seconds_since_2000_to_struct_time(mp_obj_get_int(args[0]), &tm); + timeutils_seconds_since_2000_to_struct_time(secs - EPOCH1970_EPOCH2000_DIFF_SECS, &tm); return struct_time_from_tm(&tm); } @@ -247,11 +255,16 @@ STATIC mp_obj_t time_mktime(mp_obj_t t) { mp_raise_TypeError("function takes exactly 9 arguments"); } - return mp_obj_new_int_from_uint(timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), - mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5]))); + if (mp_obj_get_int(elem[0]) < 2000) + mp_raise_msg(&mp_type_OverflowError, "timestamp out of range for platform time_t"); + + mp_uint_t secs = timeutils_mktime(mp_obj_get_int(elem[0]), mp_obj_get_int(elem[1]), mp_obj_get_int(elem[2]), + mp_obj_get_int(elem[3]), mp_obj_get_int(elem[4]), mp_obj_get_int(elem[5])); + return mp_obj_new_int_from_uint(secs + EPOCH1970_EPOCH2000_DIFF_SECS); } MP_DEFINE_CONST_FUN_OBJ_1(time_mktime_obj, time_mktime); -#endif +#endif // MICROPY_LONGINT_IMPL +#endif // MICROPY_PY_COLLECTIONS STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_time) }, @@ -260,10 +273,14 @@ STATIC const mp_rom_map_elem_t time_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_sleep), MP_ROM_PTR(&time_sleep_obj) }, #if MICROPY_PY_COLLECTIONS { MP_ROM_QSTR(MP_QSTR_struct_time), MP_ROM_PTR(&struct_time_type_obj) }, + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE { MP_ROM_QSTR(MP_QSTR_localtime), MP_ROM_PTR(&time_localtime_obj) }, { MP_ROM_QSTR(MP_QSTR_mktime), MP_ROM_PTR(&time_mktime_obj) }, - #endif + #endif // MICROPY_LONGINT_IMPL + #endif // MICROPY_PY_COLLECTIONS + #if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE { MP_ROM_QSTR(MP_QSTR_time), MP_ROM_PTR(&time_time_obj) }, + #endif }; STATIC MP_DEFINE_CONST_DICT(time_module_globals, time_module_globals_table); |
