diff --git a/docs/rp2/quickref.rst b/docs/rp2/quickref.rst index 6bbe1795415f0..4c8c02dc0cc5b 100644 --- a/docs/rp2/quickref.rst +++ b/docs/rp2/quickref.rst @@ -202,7 +202,7 @@ See :ref:`machine.RTC ` :: from machine import RTC rtc = RTC() - rtc.datetime((2017, 8, 23, 1, 12, 48, 0, 0)) # set a specific date and time + rtc.datetime((2017, 8, 23, 2, 12, 48, 0, 0)) # set a specific date and time rtc.datetime() # get date and time WDT (Watchdog timer) diff --git a/lib/timeutils/timeutils.c b/lib/timeutils/timeutils.c index af210d9943bee..7c74f5fc37715 100644 --- a/lib/timeutils/timeutils.c +++ b/lib/timeutils/timeutils.c @@ -213,3 +213,10 @@ mp_uint_t timeutils_mktime_2000(mp_uint_t year, mp_int_t month, mp_int_t mday, } return timeutils_seconds_since_2000(year, month, mday, hours, minutes, seconds); } + +// Calculate the weekday from the date. +// The result is zero based with 0 = Monday. +// by Michael Keith and Tom Craver, 1990. +int timeutils_calc_weekday(int y, int m, int d) { + return ((d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) + 6) % 7; +} diff --git a/lib/timeutils/timeutils.h b/lib/timeutils/timeutils.h index 2d40f773cc53e..66e2a77f139ed 100644 --- a/lib/timeutils/timeutils.h +++ b/lib/timeutils/timeutils.h @@ -100,4 +100,6 @@ static inline int64_t timeutils_nanoseconds_since_epoch_to_nanoseconds_since_197 #endif +int timeutils_calc_weekday(int y, int m, int d); + #endif // MICROPY_INCLUDED_LIB_TIMEUTILS_TIMEUTILS_H diff --git a/ports/mimxrt/machine_rtc.c b/ports/mimxrt/machine_rtc.c index 76f414925eb5d..d00d139f79078 100644 --- a/ports/mimxrt/machine_rtc.c +++ b/ports/mimxrt/machine_rtc.c @@ -26,6 +26,7 @@ */ #include "py/runtime.h" +#include "lib/timeutils/timeutils.h" #include "modmachine.h" #include "ticks.h" #include "fsl_snvs_lp.h" @@ -39,13 +40,6 @@ typedef struct _machine_rtc_obj_t { STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}}; uint32_t us_offset = 0; -// Calculate the weekday from the date. -// The result is zero based with 0 = Monday. -// by Michael Keith and Tom Craver, 1990. -int calc_weekday(int y, int m, int d) { - return ((d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) + 6) % 7; -} - STATIC mp_obj_t machine_rtc_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { // Check arguments. mp_arg_check_num(n_args, n_kw, 0, 0, false); @@ -67,7 +61,7 @@ STATIC mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args) mp_obj_new_int(srtc_date.year), mp_obj_new_int(srtc_date.month), mp_obj_new_int(srtc_date.day), - mp_obj_new_int(calc_weekday(srtc_date.year, srtc_date.month, srtc_date.day)), + mp_obj_new_int(timeutils_calc_weekday(srtc_date.year, srtc_date.month, srtc_date.day)), mp_obj_new_int(srtc_date.hour), mp_obj_new_int(srtc_date.minute), mp_obj_new_int(srtc_date.second), diff --git a/ports/mimxrt/modutime.c b/ports/mimxrt/modutime.c index a47bc073f2753..2a88a4224bcd4 100644 --- a/ports/mimxrt/modutime.c +++ b/ports/mimxrt/modutime.c @@ -30,8 +30,6 @@ #include "extmod/utime_mphal.h" #include "fsl_snvs_lp.h" -extern int calc_weekday(int y, int m, int d); - // localtime([secs]) // Convert a time expressed in seconds since the Epoch into an 8-tuple which // contains: (year, month, mday, hour, minute, second, weekday, yearday) @@ -48,7 +46,7 @@ STATIC mp_obj_t time_localtime(size_t n_args, const mp_obj_t *args) { mp_obj_new_int(t.hour), mp_obj_new_int(t.minute), mp_obj_new_int(t.second), - mp_obj_new_int(calc_weekday(t.year, t.month, t.day)), + mp_obj_new_int(timeutils_calc_weekday(t.year, t.month, t.day)), mp_obj_new_int(timeutils_year_day(t.year, t.month, t.day)), }; return mp_obj_new_tuple(8, tuple); diff --git a/ports/rp2/machine_rtc.c b/ports/rp2/machine_rtc.c index 797bee5ed3f15..4ef2158f515b5 100644 --- a/ports/rp2/machine_rtc.c +++ b/ports/rp2/machine_rtc.c @@ -94,7 +94,7 @@ STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) { .year = mp_obj_get_int(items[0]), .month = mp_obj_get_int(items[1]), .day = mp_obj_get_int(items[2]), - .dotw = mp_obj_get_int(items[3]), + .dotw = timeutils_calc_weekday(t.year, t.month, t.day), .hour = mp_obj_get_int(items[4]), .min = mp_obj_get_int(items[5]), .sec = mp_obj_get_int(items[6]), diff --git a/ports/rp2/main.c b/ports/rp2/main.c index 7709a478bc80a..8c5772171aac9 100644 --- a/ports/rp2/main.c +++ b/ports/rp2/main.c @@ -80,7 +80,7 @@ int main(int argc, char **argv) { .year = 2021, .month = 1, .day = 1, - .dotw = 5, // 0 is Sunday, so 5 is Friday + .dotw = 4, // 0 is Monday, so 4 is Friday .hour = 0, .min = 0, .sec = 0,