summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2015-04-08 06:56:43 -0600
committerGregory Nutt <gnutt@nuttx.org>2015-04-08 06:56:43 -0600
commit356c6e3973ac16a8c49c197f0a8077d28d149f05 (patch)
treec1c841e3411eb6a8284fa2f22ceb7b81740900d5
parent2ec0c4a7b8e685c80ba24ed8f3ff13f61480f782 (diff)
downloadpx4-nuttx-356c6e3973ac16a8c49c197f0a8077d28d149f05.tar.gz
px4-nuttx-356c6e3973ac16a8c49c197f0a8077d28d149f05.tar.bz2
px4-nuttx-356c6e3973ac16a8c49c197f0a8077d28d149f05.zip
Implements CONFIG_TIME_EXTENDED as we discussed relative to providing the last 3 members of the tm struct and support for filling them in and even using the wday in the STM32 RTC. From David Sidrane.
-rw-r--r--nuttx/arch/arm/src/stm32/stm32_rtcc.c14
-rw-r--r--nuttx/include/nuttx/time.h22
-rw-r--r--nuttx/include/time.h2
-rw-r--r--nuttx/libc/Kconfig10
-rw-r--r--nuttx/libc/time/Make.defs5
-rw-r--r--nuttx/libc/time/lib_dayofweek.c106
-rw-r--r--nuttx/libc/time/lib_gmtimer.c20
7 files changed, 168 insertions, 11 deletions
diff --git a/nuttx/arch/arm/src/stm32/stm32_rtcc.c b/nuttx/arch/arm/src/stm32/stm32_rtcc.c
index fb6dbe525..da37fd5c3 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,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/nuttx/include/nuttx/time.h b/nuttx/include/nuttx/time.h
index 315c4431a..986366479 100644
--- a/nuttx/include/nuttx/time.h
+++ b/nuttx/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 <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
@@ -101,6 +101,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
*
* Description:
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..967a9f4d4 100644
--- a/nuttx/libc/Kconfig
+++ b/nuttx/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/nuttx/libc/time/Make.defs b/nuttx/libc/time/Make.defs
index 1d599ec93..f54aa5385 100644
--- a/nuttx/libc/time/Make.defs
+++ b/nuttx/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 <gnutt@nuttx.org>
#
# 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/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 <gnutt@nuttx.org>
+ * David Sidrane <david_s5@nscdg.com>
+ *
+ * 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 <nuttx/config.h>
+
+#include <stdint.h>
+#include <stdbool.h>
+
+#include <nuttx/time.h>
+
+#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..2597c429f 100644
--- a/nuttx/libc/time/lib_gmtimer.c
+++ b/nuttx/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 <gnutt@nuttx.org>
*
* 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;
}