diff --git a/arch/arm/src/stm32/stm32_rtcc.c b/arch/arm/src/stm32/stm32_rtcc.c index fb6dbe5250..da37fd5c31 100644 --- a/arch/arm/src/stm32/stm32_rtcc.c +++ b/arch/arm/src/stm32/stm32_rtcc.c @@ -880,6 +880,7 @@ int up_rtc_getdatetime(FAR struct tm *tp) * Days: 1-31 match in both cases. * Month: STM32 is 1-12, struct tm is 0-11. * Years: STM32 is 00-99, struct tm is years since 1900. + * WeekDay: STM32 is 1 = Mon - 7 = Sun * * Issue: I am not sure what the STM32 years mean. Are these the * years 2000-2099? I'll assume so. @@ -894,6 +895,13 @@ int up_rtc_getdatetime(FAR struct tm *tp) tmp = (dr & (RTC_DR_YU_MASK|RTC_DR_YT_MASK)) >> RTC_DR_YU_SHIFT; tp->tm_year = rtc_bcd2bin(tmp) + 100; +#if defined(CONFIG_TIME_EXTENDED) + tmp = (dr & RTC_DR_WDU_MASK) >> RTC_DR_WDU_SHIFT; + tp->tm_wday = tmp % 7; + tp->tm_yday = tp->tm_mday + clock_daysbeforemonth(tp->tm_mon, clock_isleapyear(tp->tm_year + 1900)); + tp->tm_isdst = 0 +#endif + #ifdef CONFIG_STM32_HAVE_RTC_SUBSECONDS /* Return RTC sub-seconds if no configured and if a non-NULL value * of nsec has been provided to receive the sub-second value. @@ -992,14 +1000,18 @@ int stm32_rtc_setdatetime(FAR const struct tm *tp) * Days: 1-31 match in both cases. * Month: STM32 is 1-12, struct tm is 0-11. * Years: STM32 is 00-99, struct tm is years since 1900. - * + * WeekDay: STM32 is 1 = Mon - 7 = Sun * Issue: I am not sure what the STM32 years mean. Are these the * years 2000-2099? I'll assume so. */ dr = (rtc_bin2bcd(tp->tm_mday) << RTC_DR_DU_SHIFT) | ((rtc_bin2bcd(tp->tm_mon + 1)) << RTC_DR_MU_SHIFT) | +#if defined(CONFIG_TIME_EXTENDED) + ((tp->tm_wday == 0 ? 7 : (tp->tm_wday & 7)) << RTC_DR_WDU_SHIFT) | +#endif ((rtc_bin2bcd(tp->tm_year - 100)) << RTC_DR_YU_SHIFT); + dr &= ~RTC_DR_RESERVED_BITS; /* Disable the write protection for RTC registers */ diff --git a/include/nuttx/time.h b/include/nuttx/time.h index 315c4431a5..9863664798 100644 --- a/include/nuttx/time.h +++ b/include/nuttx/time.h @@ -1,7 +1,7 @@ /**************************************************************************** * include/nuttx/time.h * - * Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -100,6 +100,26 @@ int clock_isleapyear(int year); int clock_daysbeforemonth(int month, bool leapyear); +/**************************************************************************** + * Function: clock_dayoftheweek + * + * Description: + * Get the day of the week + * + * Input Parameters: + * mday - The day of the month 1 - 31 + * month - The month of the year 1 - 12 + * year - the year including the 1900 + * + * Returned value: + * Zero based day of the week 0-6, 0 = Sunday, 1 = Monday... 6 = Saturday + * + ****************************************************************************/ + +#if defined(CONFIG_TIME_EXTENDED) +int clock_dayoftheweek(int mday, int month, int year); +#endif + /**************************************************************************** * Function: clock_calendar2utc * diff --git a/include/time.h b/include/time.h index 4b21270d91..6596bf32f3 100644 --- a/include/time.h +++ b/include/time.h @@ -136,7 +136,7 @@ struct tm int tm_mday; /* Day of the month (1-31) */ int tm_mon; /* Month (0-11) */ int tm_year; /* Years since 1900 */ -#ifdef CONFIG_LIBC_LOCALTIME +#if defined(CONFIG_LIBC_LOCALTIME) || defined(CONFIG_TIME_EXTENDED) int tm_wday; /* Day of the week (0-6) */ int tm_yday; /* Day of the year (0-365) */ int tm_isdst; /* Non-0 if daylight savings time is in effect */ diff --git a/libc/Kconfig b/libc/Kconfig index 5ea546aa93..967a9f4d4a 100644 --- a/libc/Kconfig +++ b/libc/Kconfig @@ -285,6 +285,16 @@ config LIBC_TZDIR endif +config TIME_EXTENDED + bool "Add day of week, year support" + default "n" + ---help--- + Selecting TIME_EXTENDED adds tm_wday, tm_yday and tm_isdst + to the tm struct. This allows integration with 3rd party libraries + that expect the tm struct to contain these members. + + Note: tm_isdst is always 0 + config LIB_SENDFILE_BUFSIZE int "sendfile() buffer size" default 512 diff --git a/libc/time/Make.defs b/libc/time/Make.defs index 1d599ec93d..f54aa5385a 100644 --- a/libc/time/Make.defs +++ b/libc/time/Make.defs @@ -1,7 +1,7 @@ ############################################################################ # libc/time/Make.defs # -# Copyright (C) 2011-2012, 2014 Gregory Nutt. All rights reserved. +# Copyright (C) 2011-2012, 2014-2015 Gregory Nutt. All rights reserved. # Author: Gregory Nutt # # Redistribution and use in source and binary forms, with or without @@ -42,6 +42,9 @@ ifdef CONFIG_LIBC_LOCALTIME CSRCS += lib_localtime.c else CSRCS += lib_mktime.c lib_gmtime.c lib_gmtimer.c +ifdef CONFIG_TIME_EXTENDED +CSRCS += lib_dayofweek.c +endif endif # Add the time directory to the build diff --git a/libc/time/lib_dayofweek.c b/libc/time/lib_dayofweek.c new file mode 100644 index 0000000000..bcfefd2b78 --- /dev/null +++ b/libc/time/lib_dayofweek.c @@ -0,0 +1,106 @@ +/**************************************************************************** + * libc/time/lib_dayofweek.c + * + * Copyright (C) 2009, 2011 - 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * David Sidrane + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include + +#if defined(CONFIG_TIME_EXTENDED) + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Public Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Public Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Function: clock_dayoftheweek + * + * Description: + * Get the day of the week + * + * Input Parameters: + * mday - The day of the month 1 - 31 + * month - The month of the year 1 - 12 + * year - the year including the 1900 + * + * Returned value: + * Zero based day of the week 0-6, 0 = Sunday, 1 = Monday... 6 = Saturday + * + ****************************************************************************/ + +int clock_dayoftheweek(int mday, int month, int year) +{ + if (month <= 2) { + year--; + month += 12; + } + month -= 2; + return (mday + year + year/4 - year/100 + year/400 + ( 31 * month) / 12) % 7; +} +#endif /* CONFIG_TIME_EXTENDED */ diff --git a/libc/time/lib_gmtimer.c b/libc/time/lib_gmtimer.c index b4d1452bbb..2597c429fc 100644 --- a/libc/time/lib_gmtimer.c +++ b/libc/time/lib_gmtimer.c @@ -1,7 +1,7 @@ /**************************************************************************** * libc/time/lib_gmtimer.c * - * Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009, 2011, 2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -343,12 +343,18 @@ FAR struct tm *gmtime_r(FAR const time_t *timer, FAR struct tm *result) /* Then return the struct tm contents */ - result->tm_year = (int)year - 1900; /* Relative to 1900 */ - result->tm_mon = (int)month - 1; /* zero-based */ - result->tm_mday = (int)day; /* one-based */ - result->tm_hour = (int)hour; - result->tm_min = (int)min; - result->tm_sec = (int)sec; + result->tm_year = (int)year - 1900; /* Relative to 1900 */ + result->tm_mon = (int)month - 1; /* zero-based */ + result->tm_mday = (int)day; /* one-based */ + result->tm_hour = (int)hour; + result->tm_min = (int)min; + result->tm_sec = (int)sec; + +#if defined(CONFIG_TIME_EXTENDED) + result->tm_wday = clock_dayoftheweek(day, month, year); + result->tm_yday = day + clock_daysbeforemonth(result->tm_mon, clock_isleapyear(year)); + result->tm_isdst = 0; +#endif return result; }