@@ -39,10 +39,10 @@ typedef struct _machine_rtc_obj_t {
39
39
STATIC const machine_rtc_obj_t machine_rtc_obj = {{& machine_rtc_type }};
40
40
41
41
// Calculate the weekday from the date.
42
- // The result is zero based with 0 = Sunday .
42
+ // The result is zero based with 0 = Monday .
43
43
// by Michael Keith and Tom Craver, 1990.
44
44
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 ;
46
46
}
47
47
48
48
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)
66
66
mp_obj_new_int (srtc_date .year ),
67
67
mp_obj_new_int (srtc_date .month ),
68
68
mp_obj_new_int (srtc_date .day ),
69
+ mp_obj_new_int (calc_weekday (srtc_date .year , srtc_date .month , srtc_date .day )),
69
70
mp_obj_new_int (srtc_date .hour ),
70
71
mp_obj_new_int (srtc_date .minute ),
71
72
mp_obj_new_int (srtc_date .second ),
72
73
mp_obj_new_int (ticks_us64 () % 1000000 ),
73
- mp_const_none ,
74
74
};
75
75
return mp_obj_new_tuple (8 , tuple );
76
76
} else {
@@ -84,9 +84,10 @@ STATIC mp_obj_t machine_rtc_datetime_helper(size_t n_args, const mp_obj_t *args)
84
84
srtc_date .year = year >= 100 ? year : year + 2000 ; // allow 21 for 2021
85
85
srtc_date .month = mp_obj_get_int (items [1 ]);
86
86
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 ]);
90
91
if (SNVS_LP_SRTC_SetDatetime (SNVS , & srtc_date ) != kStatus_Success ) {
91
92
mp_raise_ValueError (NULL );
92
93
}
@@ -101,7 +102,21 @@ STATIC mp_obj_t machine_rtc_datetime(mp_uint_t n_args, const mp_obj_t *args) {
101
102
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (machine_rtc_datetime_obj , 1 , 2 , machine_rtc_datetime );
102
103
103
104
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 );
105
120
}
106
121
STATIC MP_DEFINE_CONST_FUN_OBJ_1 (machine_rtc_now_obj , machine_rtc_now );
107
122
@@ -112,16 +127,6 @@ STATIC mp_obj_t machine_rtc_init(mp_obj_t self_in, mp_obj_t date) {
112
127
}
113
128
STATIC MP_DEFINE_CONST_FUN_OBJ_2 (machine_rtc_init_obj , machine_rtc_init );
114
129
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
-
125
130
// calibration(cal)
126
131
// When the argument is a number in the range [-16 to 15], set the calibration value.
127
132
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[] = {
143
148
{ MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_rtc_init_obj ) },
144
149
{ MP_ROM_QSTR (MP_QSTR_datetime ), MP_ROM_PTR (& machine_rtc_datetime_obj ) },
145
150
{ 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 ) },
147
151
{ MP_ROM_QSTR (MP_QSTR_calibration ), MP_ROM_PTR (& machine_rtc_calibration_obj ) },
148
152
};
149
153
STATIC MP_DEFINE_CONST_DICT (machine_rtc_locals_dict , machine_rtc_locals_dict_table );
0 commit comments