DateTime: fix missing weekday in datetime_timestamp_to_datetime conversion (#3508)

This commit is contained in:
あく 2024-03-11 22:09:39 +09:00 committed by GitHub
parent 4f7eb77071
commit adbe4d44f4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 2 deletions

View file

@ -112,7 +112,7 @@ MU_TEST_SUITE(test_datetime_validate_datetime) {
MU_TEST(test_datetime_timestamp_to_datetime_min) {
uint32_t test_value = 0;
DateTime min_datetime_expected = {0, 0, 0, 1, 1, 1970, 0};
DateTime min_datetime_expected = {0, 0, 0, 1, 1, 1970, 4};
DateTime result = {0};
datetime_timestamp_to_datetime(test_value, &result);
@ -122,7 +122,7 @@ MU_TEST(test_datetime_timestamp_to_datetime_min) {
MU_TEST(test_datetime_timestamp_to_datetime_max) {
uint32_t test_value = UINT32_MAX;
DateTime max_datetime_expected = {6, 28, 15, 7, 2, 2106, 0};
DateTime max_datetime_expected = {6, 28, 15, 7, 2, 2106, 7};
DateTime result = {0};
datetime_timestamp_to_datetime(test_value, &result);
@ -141,10 +141,26 @@ MU_TEST(test_datetime_timestamp_to_datetime_to_timestamp) {
mu_assert_int_eq(test_value, result);
}
MU_TEST(test_datetime_timestamp_to_datetime_weekday) {
uint32_t test_value = 1709748421; // Wed Mar 06 18:07:01 2024 UTC
DateTime datetime = {0};
datetime_timestamp_to_datetime(test_value, &datetime);
mu_assert_int_eq(datetime.hour, 18);
mu_assert_int_eq(datetime.minute, 7);
mu_assert_int_eq(datetime.second, 1);
mu_assert_int_eq(datetime.day, 6);
mu_assert_int_eq(datetime.month, 3);
mu_assert_int_eq(datetime.weekday, 3);
mu_assert_int_eq(datetime.year, 2024);
}
MU_TEST_SUITE(test_datetime_timestamp_to_datetime_suite) {
MU_RUN_TEST(test_datetime_timestamp_to_datetime_min);
MU_RUN_TEST(test_datetime_timestamp_to_datetime_max);
MU_RUN_TEST(test_datetime_timestamp_to_datetime_to_timestamp);
MU_RUN_TEST(test_datetime_timestamp_to_datetime_weekday);
}
MU_TEST(test_datetime_datetime_to_timestamp_min) {

View file

@ -71,6 +71,7 @@ void datetime_timestamp_to_datetime(uint32_t timestamp, DateTime* datetime) {
uint32_t seconds_in_day = timestamp % SECONDS_PER_DAY;
datetime->year = EPOCH_START_YEAR;
datetime->weekday = ((days + 3) % 7) + 1;
while(days >= datetime_get_days_per_year(datetime->year)) {
days -= datetime_get_days_per_year(datetime->year);