diff --git a/support/platform/include/rtc_base.h b/support/platform/include/rtc_base.h index ebc7b1fb0f55811a30ff903474ce1e49936081ae..d37fc94c17c2ab0453f79695b93f9c7d933697be 100644 --- a/support/platform/include/rtc_base.h +++ b/support/platform/include/rtc_base.h @@ -44,10 +44,10 @@ extern "C" { #define IS_INVALID_YEAR(year) ((year) < RTC_BEGIN_YEAR) #define IS_INVALID_MONTH(m) (((m) < RTC_JANUARY) || ((m) > RTC_MAX_MONTH)) #define IS_INVALID_WEEKDAY(wd) (((wd) < 1) || ((wd) > RTC_MAX_WEEKDAY)) -#define IS_INVALID_HOUR(hour) (((hour) < 0) || ((hour) >= RTC_MAX_HOUR)) -#define IS_INVALID_MIN(min) (((min) < 0) || ((min) >= RTC_MAX_MINUTE)) -#define IS_INVALID_SECOND(s) (((s) < 0) || ((s) >= RTC_MAX_SECOND)) -#define IS_INVALID_MS(ms) (((ms) < 0) || ((ms) >= RTC_MAX_MS)) +#define IS_INVALID_HOUR(hour) ((hour) >= RTC_MAX_HOUR) +#define IS_INVALID_MIN(min) ((min) >= RTC_MAX_MINUTE) +#define IS_INVALID_SECOND(s) ((s) >= RTC_MAX_SECOND) +#define IS_INVALID_MS(ms) ((ms) >= RTC_MAX_MS) enum RtcMonth { RTC_JANUARY = 1, diff --git a/support/platform/src/rtc_base.c b/support/platform/src/rtc_base.c index 5e671ac0600b3f65e229b4c602295a4a07f0477b..25be00507119e77a01e32598e6c1b699652dec57 100644 --- a/support/platform/src/rtc_base.c +++ b/support/platform/src/rtc_base.c @@ -98,14 +98,14 @@ uint64_t RtcTimeToTimestamp(const struct RtcTime *time) seconds = ((uint64_t)time->hour * RTC_MAX_MINUTE + time->minute) * RTC_MAX_SECOND + time->second; days = time->day - RTC_UNIT_DIFF; - month = time->month - RTC_UNIT_DIFF; + month = time->month; year = time->year; - while (--month >= 0) { + for (month--; month >= RTC_JANUARY; month--) { days += RtcGetMonthDays(IS_LEAP_YEAR(time->year), month); } - - while (--year >= RTC_BEGIN_YEAR) { + + for (year--; year >= RTC_BEGIN_YEAR; year--) { days += RTC_YEAR_DAYS(year); } @@ -129,7 +129,7 @@ void TimestampToRtcTime(struct RtcTime *time, const uint64_t seconds) time->year++; } - time->month = 0; + time->month = RTC_JANUARY; while (days >= RtcGetMonthDays(IS_LEAP_YEAR(time->year), time->month)) { days -= RtcGetMonthDays(IS_LEAP_YEAR(time->year), time->month); time->month++; @@ -140,7 +140,6 @@ void TimestampToRtcTime(struct RtcTime *time, const uint64_t seconds) time->minute = (daySeconds % RTC_HOUR_SECONDS) / RTC_MAX_MINUTE; time->hour = daySeconds / RTC_HOUR_SECONDS; - time->month += RTC_UNIT_DIFF; time->day += RTC_UNIT_DIFF; time->weekday = RtcGetWeekDay(time); PLAT_LOGV("TimestampToRtc:year-month-day weekday hour:min:second ms %04u-%02u-%02u %u %02u:%02u:%02u .%03u",