Skip to content

Commit

Permalink
mimxrt: Move calc_weekday helper function to timeutils.
Browse files Browse the repository at this point in the history
This function may be useful for other ports as well so lets move it to
timeutils so it can be reused.

Signed-off-by: Krzysztof Adamski <k@japko.eu>
  • Loading branch information
kadamski authored and dpgeorge committed Jun 25, 2021
1 parent b51ae20 commit 6409bbc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
7 changes: 7 additions & 0 deletions lib/timeutils/timeutils.c
Expand Up @@ -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;
}
2 changes: 2 additions & 0 deletions lib/timeutils/timeutils.h
Expand Up @@ -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
10 changes: 2 additions & 8 deletions ports/mimxrt/machine_rtc.c
Expand Up @@ -26,6 +26,7 @@
*/

#include "py/runtime.h"
#include "lib/timeutils/timeutils.h"
#include "modmachine.h"
#include "ticks.h"
#include "fsl_snvs_lp.h"
Expand All @@ -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);
Expand All @@ -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),
Expand Down
4 changes: 1 addition & 3 deletions ports/mimxrt/modutime.c
Expand Up @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 6409bbc

Please sign in to comment.