mirror of
https://github.com/apache/nuttx.git
synced 2026-06-05 07:12:54 +08:00
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.
This commit is contained in:
@@ -880,6 +880,7 @@ int up_rtc_getdatetime(FAR struct tm *tp)
|
|||||||
* Days: 1-31 match in both cases.
|
* Days: 1-31 match in both cases.
|
||||||
* Month: STM32 is 1-12, struct tm is 0-11.
|
* Month: STM32 is 1-12, struct tm is 0-11.
|
||||||
* Years: STM32 is 00-99, struct tm is years since 1900.
|
* 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
|
* Issue: I am not sure what the STM32 years mean. Are these the
|
||||||
* years 2000-2099? I'll assume so.
|
* 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;
|
tmp = (dr & (RTC_DR_YU_MASK|RTC_DR_YT_MASK)) >> RTC_DR_YU_SHIFT;
|
||||||
tp->tm_year = rtc_bcd2bin(tmp) + 100;
|
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
|
#ifdef CONFIG_STM32_HAVE_RTC_SUBSECONDS
|
||||||
/* Return RTC sub-seconds if no configured and if a non-NULL value
|
/* Return RTC sub-seconds if no configured and if a non-NULL value
|
||||||
* of nsec has been provided to receive the sub-second 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.
|
* Days: 1-31 match in both cases.
|
||||||
* Month: STM32 is 1-12, struct tm is 0-11.
|
* Month: STM32 is 1-12, struct tm is 0-11.
|
||||||
* Years: STM32 is 00-99, struct tm is years since 1900.
|
* 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
|
* Issue: I am not sure what the STM32 years mean. Are these the
|
||||||
* years 2000-2099? I'll assume so.
|
* years 2000-2099? I'll assume so.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
dr = (rtc_bin2bcd(tp->tm_mday) << RTC_DR_DU_SHIFT) |
|
dr = (rtc_bin2bcd(tp->tm_mday) << RTC_DR_DU_SHIFT) |
|
||||||
((rtc_bin2bcd(tp->tm_mon + 1)) << RTC_DR_MU_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);
|
((rtc_bin2bcd(tp->tm_year - 100)) << RTC_DR_YU_SHIFT);
|
||||||
|
|
||||||
dr &= ~RTC_DR_RESERVED_BITS;
|
dr &= ~RTC_DR_RESERVED_BITS;
|
||||||
|
|
||||||
/* Disable the write protection for RTC registers */
|
/* Disable the write protection for RTC registers */
|
||||||
|
|||||||
+21
-1
@@ -1,7 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* include/nuttx/time.h
|
* 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>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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);
|
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
|
* Function: clock_calendar2utc
|
||||||
*
|
*
|
||||||
|
|||||||
+1
-1
@@ -136,7 +136,7 @@ struct tm
|
|||||||
int tm_mday; /* Day of the month (1-31) */
|
int tm_mday; /* Day of the month (1-31) */
|
||||||
int tm_mon; /* Month (0-11) */
|
int tm_mon; /* Month (0-11) */
|
||||||
int tm_year; /* Years since 1900 */
|
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_wday; /* Day of the week (0-6) */
|
||||||
int tm_yday; /* Day of the year (0-365) */
|
int tm_yday; /* Day of the year (0-365) */
|
||||||
int tm_isdst; /* Non-0 if daylight savings time is in effect */
|
int tm_isdst; /* Non-0 if daylight savings time is in effect */
|
||||||
|
|||||||
@@ -285,6 +285,16 @@ config LIBC_TZDIR
|
|||||||
|
|
||||||
endif
|
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
|
config LIB_SENDFILE_BUFSIZE
|
||||||
int "sendfile() buffer size"
|
int "sendfile() buffer size"
|
||||||
default 512
|
default 512
|
||||||
|
|||||||
+4
-1
@@ -1,7 +1,7 @@
|
|||||||
############################################################################
|
############################################################################
|
||||||
# libc/time/Make.defs
|
# 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>
|
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
#
|
#
|
||||||
# Redistribution and use in source and binary forms, with or without
|
# Redistribution and use in source and binary forms, with or without
|
||||||
@@ -42,6 +42,9 @@ ifdef CONFIG_LIBC_LOCALTIME
|
|||||||
CSRCS += lib_localtime.c
|
CSRCS += lib_localtime.c
|
||||||
else
|
else
|
||||||
CSRCS += lib_mktime.c lib_gmtime.c lib_gmtimer.c
|
CSRCS += lib_mktime.c lib_gmtime.c lib_gmtimer.c
|
||||||
|
ifdef CONFIG_TIME_EXTENDED
|
||||||
|
CSRCS += lib_dayofweek.c
|
||||||
|
endif
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# Add the time directory to the build
|
# Add the time directory to the build
|
||||||
|
|||||||
@@ -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 */
|
||||||
+13
-7
@@ -1,7 +1,7 @@
|
|||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* libc/time/lib_gmtimer.c
|
* 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>
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* 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 */
|
/* Then return the struct tm contents */
|
||||||
|
|
||||||
result->tm_year = (int)year - 1900; /* Relative to 1900 */
|
result->tm_year = (int)year - 1900; /* Relative to 1900 */
|
||||||
result->tm_mon = (int)month - 1; /* zero-based */
|
result->tm_mon = (int)month - 1; /* zero-based */
|
||||||
result->tm_mday = (int)day; /* one-based */
|
result->tm_mday = (int)day; /* one-based */
|
||||||
result->tm_hour = (int)hour;
|
result->tm_hour = (int)hour;
|
||||||
result->tm_min = (int)min;
|
result->tm_min = (int)min;
|
||||||
result->tm_sec = (int)sec;
|
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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user