From 6c18397707a17eb2097f4957fb4355a46398958d Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Tue, 7 Apr 2015 16:37:35 -1000 Subject: Added CONFIG_TIME_EXTENDED to support tm_wday, tm_yday and tm_isdst for integration with 3rd party libraries --- nuttx/arch/arm/src/stm32/stm32_rtcc.c | 13 ++++- nuttx/include/nuttx/time.h | 19 ++++++ nuttx/include/time.h | 2 +- nuttx/libc/Kconfig | 9 +++ nuttx/libc/time/Make.defs | 3 + nuttx/libc/time/lib_dayofweek.c | 106 ++++++++++++++++++++++++++++++++++ nuttx/libc/time/lib_gmtimer.c | 5 ++ 7 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 nuttx/libc/time/lib_dayofweek.c diff --git a/nuttx/arch/arm/src/stm32/stm32_rtcc.c b/nuttx/arch/arm/src/stm32/stm32_rtcc.c index fb6dbe525..413096cb1 100644 --- a/nuttx/arch/arm/src/stm32/stm32_rtcc.c +++ b/nuttx/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,12 @@ 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 +999,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/nuttx/include/nuttx/time.h b/nuttx/include/nuttx/time.h index 315c4431a..ae5f13ac8 100644 --- a/nuttx/include/nuttx/time.h +++ b/nuttx/include/nuttx/time.h @@ -100,6 +100,25 @@ 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/nuttx/include/time.h b/nuttx/include/time.h index 4b21270d9..6596bf32f 100644 --- a/nuttx/include/time.h +++ b/nuttx/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/nuttx/libc/Kconfig b/nuttx/libc/Kconfig index 5ea546aa9..15613ca8e 100644 --- a/nuttx/libc/Kconfig +++ b/nuttx/libc/Kconfig @@ -285,6 +285,15 @@ 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/nuttx/libc/time/Make.defs b/nuttx/libc/time/Make.defs index 1d599ec93..7440d00eb 100644 --- a/nuttx/libc/time/Make.defs +++ b/nuttx/libc/time/Make.defs @@ -43,6 +43,9 @@ CSRCS += lib_localtime.c else CSRCS += lib_mktime.c lib_gmtime.c lib_gmtimer.c endif +ifdef CONFIG_TIME_EXTENDED +CSRCS += lib_dayofweek.c +endif # Add the time directory to the build diff --git a/nuttx/libc/time/lib_dayofweek.c b/nuttx/libc/time/lib_dayofweek.c new file mode 100644 index 000000000..bcfefd2b7 --- /dev/null +++ b/nuttx/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/nuttx/libc/time/lib_gmtimer.c b/nuttx/libc/time/lib_gmtimer.c index b4d1452bb..df2bd1ed3 100644 --- a/nuttx/libc/time/lib_gmtimer.c +++ b/nuttx/libc/time/lib_gmtimer.c @@ -350,6 +350,11 @@ FAR struct tm *gmtime_r(FAR const time_t *timer, FAR struct tm *result) 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; } -- cgit v1.2.3