Skip to content

Commit fd4eec5

Browse files
robert-hhdpgeorge
authored andcommitted
mimxrt/machine_rtc: Change RTC.datetime() tuple to match other ports.
This change moves the datetime tuple format back to the one used by all the other ports: (year, month, day, weekday, hour, minute, second, microsecond) Weekday is a number between 0 and 6, with 0 assigned to Monday. It has to be provided when setting the RTC with datetime(), but will be ignored on entry and calculated when needed. The weekday() method was removed, since that is now again a part of the datetime tuple. The now() method was updated so it continues to return a tuple that matches CPython's datetime module.
1 parent 37d01d4 commit fd4eec5

File tree

1 file changed

+22
-18
lines changed

1 file changed

+22
-18
lines changed

ports/mimxrt/machine_rtc.c

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ typedef struct _machine_rtc_obj_t {
3939
STATIC const machine_rtc_obj_t machine_rtc_obj = {{&machine_rtc_type}};
4040

4141
// Calculate the weekday from the date.
42-
// The result is zero based with 0 = Sunday.
42+
// The result is zero based with 0 = Monday.
4343
// by Michael Keith and Tom Craver, 1990.
4444
static int calc_weekday(int y, int m, int d) {
45-
return (d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) % 7;
45+
return ((d += m < 3 ? y-- : y - 2, 23 * m / 9 + d + 4 + y / 4 - y / 100 + y / 400) + 6) % 7;
4646
}
4747

4848
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) {
@@ -66,11 +66,11 @@ STATIC mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
6666
mp_obj_new_int(srtc_date.year),
6767
mp_obj_new_int(srtc_date.month),
6868
mp_obj_new_int(srtc_date.day),
69+
mp_obj_new_int(calc_weekday(srtc_date.year, srtc_date.month, srtc_date.day)),
6970
mp_obj_new_int(srtc_date.hour),
7071
mp_obj_new_int(srtc_date.minute),
7172
mp_obj_new_int(srtc_date.second),
7273
mp_obj_new_int(ticks_us64() % 1000000),
73-
mp_const_none,
7474
};
7575
return mp_obj_new_tuple(8, tuple);
7676
} else {
@@ -84,9 +84,10 @@ STATIC mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
8484
srtc_date.year = year >= 100 ? year : year + 2000; // allow 21 for 2021
8585
srtc_date.month = mp_obj_get_int(items[1]);
8686
srtc_date.day = mp_obj_get_int(items[2]);
87-
srtc_date.hour = mp_obj_get_int(items[3]);
88-
srtc_date.minute = mp_obj_get_int(items[4]);
89-
srtc_date.second = mp_obj_get_int(items[5]);
87+
// Ignore weekday at items[3]
88+
srtc_date.hour = mp_obj_get_int(items[4]);
89+
srtc_date.minute = mp_obj_get_int(items[5]);
90+
srtc_date.second = mp_obj_get_int(items[6]);
9091
if (SNVS_LP_SRTC_SetDatetime(SNVS, &srtc_date) != kStatus_Success) {
9192
mp_raise_ValueError(NULL);
9293
}
@@ -101,7 +102,21 @@ STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
101102
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_rtc_datetime_obj, 1, 2, machine_rtc_datetime);
102103

103104
STATIC mp_obj_t machine_rtc_now(mp_obj_t self_in) {
104-
return machine_rtc_datetime_helper(1, &self_in);
105+
// Get date and time in CPython order.
106+
snvs_lp_srtc_datetime_t srtc_date;
107+
SNVS_LP_SRTC_GetDatetime(SNVS, &srtc_date);
108+
109+
mp_obj_t tuple[8] = {
110+
mp_obj_new_int(srtc_date.year),
111+
mp_obj_new_int(srtc_date.month),
112+
mp_obj_new_int(srtc_date.day),
113+
mp_obj_new_int(srtc_date.hour),
114+
mp_obj_new_int(srtc_date.minute),
115+
mp_obj_new_int(srtc_date.second),
116+
mp_obj_new_int((ticks_us64() + us_offset) % 1000000),
117+
mp_const_none,
118+
};
119+
return mp_obj_new_tuple(8, tuple);
105120
}
106121
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_now_obj, machine_rtc_now);
107122

@@ -112,16 +127,6 @@ STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
112127
}
113128
STATIC MP_DEFINE_CONST_FUN_OBJ_2(machine_rtc_init_obj, machine_rtc_init);
114129

115-
STATIC mp_obj_t machine_rtc_weekday(mp_obj_t self_in) {
116-
(void)self_in; // unused
117-
int day;
118-
snvs_lp_srtc_datetime_t srtc_date;
119-
SNVS_LP_SRTC_GetDatetime(SNVS, &srtc_date);
120-
day = calc_weekday(srtc_date.year, srtc_date.month, srtc_date.day);
121-
return MP_OBJ_NEW_SMALL_INT((day + 6) % 7);
122-
}
123-
STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_rtc_weekday_obj, machine_rtc_weekday);
124-
125130
// calibration(cal)
126131
// When the argument is a number in the range [-16 to 15], set the calibration value.
127132
STATIC mp_obj_t machine_rtc_calibration(mp_obj_t self_in, mp_obj_t cal_in) {
@@ -143,7 +148,6 @@ STATIC const mp_rom_map_elem_t machine_rtc_locals_dict_table[] = {
143148
{ MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_rtc_init_obj) },
144149
{ MP_ROM_QSTR(MP_QSTR_datetime), MP_ROM_PTR(&machine_rtc_datetime_obj) },
145150
{ MP_ROM_QSTR(MP_QSTR_now), MP_ROM_PTR(&machine_rtc_now_obj) },
146-
{ MP_ROM_QSTR(MP_QSTR_weekday), MP_ROM_PTR(&machine_rtc_weekday_obj) },
147151
{ MP_ROM_QSTR(MP_QSTR_calibration), MP_ROM_PTR(&machine_rtc_calibration_obj) },
148152
};
149153
STATIC MP_DEFINE_CONST_DICT(machine_rtc_locals_dict, machine_rtc_locals_dict_table);

0 commit comments

Comments
 (0)