From d4e66c30f16f005c16d354053725832382ac705d Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Sun, 12 Apr 2015 09:01:35 -0600 Subject: ix an error in time initialization when there is not RTC and the time is initialized from a fixed configured value. The call to clock_calendar2utc() was returning the time in units of seconds. The initialization logic, however, was expecting to get time in units of days. --- nuttx/libc/time/lib_calendar2utc.c | 28 +++++++++++++++++-------- nuttx/libc/time/lib_mktime.c | 42 ++++---------------------------------- 2 files changed, 23 insertions(+), 47 deletions(-) diff --git a/nuttx/libc/time/lib_calendar2utc.c b/nuttx/libc/time/lib_calendar2utc.c index 847d31661..4ed9f1d60 100644 --- a/nuttx/libc/time/lib_calendar2utc.c +++ b/nuttx/libc/time/lib_calendar2utc.c @@ -193,17 +193,27 @@ time_t clock_calendar2utc(int year, int month, int day) time_t clock_calendar2utc(int year, int month, int day) { - struct tm t; + time_t days; - /* mktime can (kind of) do this */ + /* Years since epoch in units of days (ignoring leap years). */ - t.tm_year = year; - t.tm_mon = month; - t.tm_mday = day; - t.tm_hour = 0; - t.tm_min = 0; - t.tm_sec = 0; - return mktime(&t); + days = (year - 1970) * 365; + + /* Add in the extra days for the leap years prior to the current year. */ + + days += (year - 1969) >> 2; + + /* Add in the days up to the beginning of this month. */ + + days += (time_t)clock_daysbeforemonth(month, clock_isleapyear(year)); + + /* Add in the days since the beginning of this month (days are 1-based). */ + + days += day - 1; + + /* Then convert the seconds and add in hours, minutes, and seconds */ + + return days; } #endif /* CONFIG_GREGORIAN_TIME */ diff --git a/nuttx/libc/time/lib_mktime.c b/nuttx/libc/time/lib_mktime.c index 5a9c11163..b216c8dc3 100644 --- a/nuttx/libc/time/lib_mktime.c +++ b/nuttx/libc/time/lib_mktime.c @@ -77,14 +77,13 @@ ****************************************************************************/ /**************************************************************************** - * Function: mktime + * Name: mktime * * Description: - * Time conversion (based on the POSIX API) + * Time conversion (based on the POSIX API) * ****************************************************************************/ -#ifdef CONFIG_GREGORIAN_TIME time_t mktime(FAR struct tm *tp) { time_t ret; @@ -94,48 +93,15 @@ time_t mktime(FAR struct tm *tp) * month, and date */ - jdn = clock_calendar2utc(tp->tm_year+1900, tp->tm_mon+1, tp->tm_mday); + jdn = clock_calendar2utc(tp->tm_year + 1900, tp->tm_mon, tp->tm_mday); sdbg("jdn=%d tm_year=%d tm_mon=%d tm_mday=%d\n", (int)jdn, tp->tm_year, tp->tm_mon, tp->tm_mday); /* Return the seconds into the julian day. */ - ret = ((jdn*24 + tp->tm_hour)*60 + tp->tm_min)*60 + tp->tm_sec; + ret = ((jdn * 24 + tp->tm_hour) * 60 + tp->tm_min) * 60 + tp->tm_sec; sdbg("ret=%d tm_hour=%d tm_min=%d tm_sec=%d\n", (int)ret, tp->tm_hour, tp->tm_min, tp->tm_sec); return ret; } -#else - -/* Simple version that only works for dates within a (relatively) small range - * from the epoch. It does not handle earlier days or longer days where leap - * seconds, etc. apply. - */ - -time_t mktime(FAR struct tm *tp) -{ - unsigned int days; - - /* Years since epoch in units of days (ignoring leap years). */ - - days = (tp->tm_year - 70) * 365; - - /* Add in the extra days for the leap years prior to the current year. */ - - days += (tp->tm_year - 69) >> 2; - - /* Add in the days up to the beginning of this month. */ - - days += (time_t)clock_daysbeforemonth(tp->tm_mon, clock_isleapyear(tp->tm_year + 1900)); - - /* Add in the days since the beginning of this month (days are 1-based). */ - - days += tp->tm_mday - 1; - - /* Then convert the seconds and add in hours, minutes, and seconds */ - - return ((days * 24 + tp->tm_hour) * 60 + tp->tm_min) * 60 + tp->tm_sec; -} -#endif /* CONFIG_GREGORIAN_TIME */ - -- cgit v1.2.3