rp2/machine_rtc: In RTC.datetime, compute weekday automatically.

Calculating the weekday each time you want to set a date is error prone and
tiresome.  MicroPython can do it on its own - hardware on some ports do not
support storing weekday in hardware and always computes it on the fly,
ignoring the value given to the constructor.

During discussion for #7432 the conclusion was that there seems to be no
obvious reason to let user set the weekday to an incorrect value so it
makes sense to just ignore the provided weekday value and always compute
the correct value.  This patch introduces this change for the rp2 port.

Signed-off-by: Krzysztof Adamski <k@japko.eu>
This commit is contained in:
Krzysztof Adamski 2021-06-20 11:49:56 +02:00 committed by Damien George
parent 35b1359a3a
commit feb7e2e864
1 changed files with 2 additions and 1 deletions

View File

@ -94,11 +94,12 @@ 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]),
.hour = mp_obj_get_int(items[4]),
.min = mp_obj_get_int(items[5]),
.sec = mp_obj_get_int(items[6]),
};
// Deliberately ignore the weekday argument and compute the proper value
t.dotw = timeutils_calc_weekday(t.year, t.month, t.day);
if (!rtc_set_datetime(&t)) {
mp_raise_OSError(MP_EINVAL);