mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-06 13:31:35 +08:00
Manually fixup merge botches via direct comparison with NuttX r5447.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
############################################################################
|
||||
# libc/time/Make.defs
|
||||
#
|
||||
# Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
# Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
############################################################################
|
||||
|
||||
# Add the time C files to the build
|
||||
|
||||
CSRCS += lib_mktime.c lib_gmtime.c lib_gmtimer.c lib_strftime.c \
|
||||
lib_calendar2utc.c lib_daysbeforemonth.c lib_isleapyear.c lib_time.c
|
||||
|
||||
# Add the time directory to the build
|
||||
|
||||
DEPPATH += --dep-path time
|
||||
VPATH += :time
|
||||
@@ -0,0 +1,209 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_calendar2utc.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: clock_gregorian2utc, clock_julian2utc
|
||||
*
|
||||
* Description:
|
||||
* UTC conversion routines. These conversions are based
|
||||
* on algorithms from p. 604 of Seidelman, P. K. 1992.
|
||||
* Explanatory Supplement to the Astronomical Almanac.
|
||||
* University Science Books, Mill Valley.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_GREGORIAN_TIME
|
||||
static time_t clock_gregorian2utc(int year, int month, int day)
|
||||
{
|
||||
int temp;
|
||||
|
||||
/* temp = (month - 14)/12; */
|
||||
|
||||
temp = (month <= 2 ? -1:0);
|
||||
|
||||
return (1461*(year + 4800 + temp))/4
|
||||
+ (367*(month - 2 - 12*temp))/12
|
||||
- (3*((year + 4900 + temp)/100))/4 + day - 32075;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
static time_t clock_julian2utc(int year, int month, int day)
|
||||
{
|
||||
return 367*year
|
||||
- (7*(year + 5001 + (month-9)/7))/4
|
||||
+ (275*month)/9
|
||||
+ day + 1729777;
|
||||
}
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
#endif /* CONFIG_GREGORIAN_TIME */
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: clock_calendar2utc
|
||||
*
|
||||
* Description:
|
||||
* Calendar/UTC conversion based on algorithms from p. 604
|
||||
* of Seidelman, P. K. 1992. Explanatory Supplement to
|
||||
* the Astronomical Almanac. University Science Books,
|
||||
* Mill Valley.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_GREGORIAN_TIME
|
||||
time_t clock_calendar2utc(int year, int month, int day)
|
||||
{
|
||||
int dyear;
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
bool isgreg;
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
|
||||
/* Correct year & month ranges. Shift month into range 1-12 */
|
||||
|
||||
dyear = (month-1) / 12;
|
||||
month -= 12 * dyear;
|
||||
year += dyear;
|
||||
|
||||
if (month < 1)
|
||||
{
|
||||
month += 12;
|
||||
year -= 1;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
/* Determine which calendar to use */
|
||||
|
||||
if (year > GREG_YEAR)
|
||||
{
|
||||
isgreg = true;
|
||||
}
|
||||
else if (year < GREG_YEAR)
|
||||
{
|
||||
isgreg = false;
|
||||
}
|
||||
else if (month > GREG_MONTH)
|
||||
{
|
||||
isgreg = true;
|
||||
}
|
||||
else if (month < GREG_MONTH)
|
||||
{
|
||||
isgreg = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
isgreg = (day >= GREG_DAY);
|
||||
}
|
||||
|
||||
/* Calculate and return date */
|
||||
|
||||
if (isgreg)
|
||||
{
|
||||
return clock_gregorian2utc(year, month, day) - JD_OF_EPOCH;
|
||||
}
|
||||
else
|
||||
{
|
||||
return clock_julian2utc (year, month, day) - JD_OF_EPOCH;
|
||||
}
|
||||
|
||||
#else /* CONFIG_JULIAN_TIME */
|
||||
|
||||
return clock_gregorian2utc(year, month, day) - JD_OF_EPOCH;
|
||||
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
}
|
||||
#else
|
||||
|
||||
/* A highly simplified version that only handles days in the time
|
||||
* since Jan 1, 1970.
|
||||
*/
|
||||
|
||||
time_t clock_calendar2utc(int year, int month, int day)
|
||||
{
|
||||
struct tm t;
|
||||
|
||||
/* mktime can (kind of) do this */
|
||||
|
||||
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);
|
||||
}
|
||||
#endif /* CONFIG_GREGORIAN_TIME */
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_daysbeforemonth.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
uint16_t g_daysbeforemonth[13] =
|
||||
{
|
||||
0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: clock_daysbeforemonth
|
||||
*
|
||||
* Description:
|
||||
* Get the number of days that occurred before the beginning of the month.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int clock_daysbeforemonth(int month, bool leapyear)
|
||||
{
|
||||
int retval = g_daysbeforemonth[month];
|
||||
if (month >= 2 && leapyear)
|
||||
{
|
||||
retval++;
|
||||
}
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_gmtime.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <time.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Public Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: gmtime
|
||||
*
|
||||
* Description:
|
||||
* Similar to gmtime_r, but not thread-safe
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
struct tm *gmtime(const time_t *timer)
|
||||
{
|
||||
static struct tm tm;
|
||||
return gmtime_r(timer, &tm);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,355 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_gmtimer.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <stdbool.h>
|
||||
#include <time.h>
|
||||
#include <errno.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#define SEC_PER_MIN ((time_t)60)
|
||||
#define SEC_PER_HOUR ((time_t)60 * SEC_PER_MIN)
|
||||
#define SEC_PER_DAY ((time_t)24 * SEC_PER_HOUR)
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/* Calendar/UTC conversion routines */
|
||||
|
||||
static void clock_utc2calendar(time_t utc, int *year, int *month, int *day);
|
||||
#ifdef CONFIG_GREGORIAN_TIME
|
||||
static void clock_utc2gregorian (time_t jdn, int *year, int *month, int *day);
|
||||
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
static void clock_utc2julian(time_t jdn, int *year, int *month, int *day);
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
#endif /* CONFIG_GREGORIAN_TIME */
|
||||
|
||||
/**************************************************************************
|
||||
* Public Constant Data
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/**************************************************************************
|
||||
* Private Variables
|
||||
**************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: clock_calendar2utc, clock_gregorian2utc,
|
||||
* and clock_julian2utc
|
||||
*
|
||||
* Description:
|
||||
* Calendar to UTC conversion routines. These conversions
|
||||
* are based on algorithms from p. 604 of Seidelman, P. K.
|
||||
* 1992. Explanatory Supplement to the Astronomical
|
||||
* Almanac. University Science Books, Mill Valley.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_GREGORIAN_TIME
|
||||
static void clock_utc2calendar(time_t utc, int *year, int *month, int *day)
|
||||
{
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
|
||||
if (utc >= GREG_DUTC)
|
||||
{
|
||||
clock_utc2gregorian(utc + JD_OF_EPOCH, year, month, day);
|
||||
}
|
||||
else
|
||||
{
|
||||
clock_utc2julian (utc + JD_OF_EPOCH, year, month, day);
|
||||
}
|
||||
|
||||
#else /* CONFIG_JULIAN_TIME */
|
||||
|
||||
clock_utc2gregorian(utc + JD_OF_EPOCH, year, month, day);
|
||||
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
}
|
||||
|
||||
static void clock_utc2gregorian(time_t jd, int *year, int *month, int *day)
|
||||
{
|
||||
long l, n, i, j, d, m, y;
|
||||
|
||||
l = jd + 68569;
|
||||
n = (4*l) / 146097;
|
||||
l = l - (146097*n + 3)/4;
|
||||
i = (4000*(l+1))/1461001;
|
||||
l = l - (1461*i)/4 + 31;
|
||||
j = (80*l)/2447;
|
||||
d = l - (2447*j)/80;
|
||||
l = j/11;
|
||||
m = j + 2 - 12*l;
|
||||
y = 100*(n-49) + i + l;
|
||||
|
||||
*year = y;
|
||||
*month = m;
|
||||
*day = d;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_JULIAN_TIME
|
||||
|
||||
static void clock_utc2julian(time_t jd, int *year, int *month, int *day)
|
||||
{
|
||||
long j, k, l, n, d, i, m, y;
|
||||
|
||||
j = jd + 1402;
|
||||
k = (j-1)/1461;
|
||||
l = j - 1461*k;
|
||||
n = (l-1)/365 - l/1461;
|
||||
i = l - 365*n + 30;
|
||||
j = (80*i)/2447;
|
||||
d = i - (2447*j)/80;
|
||||
i = j/11;
|
||||
m = j + 2 - 12*i;
|
||||
y = 4*k + n + i - 4716;
|
||||
|
||||
*year = y;
|
||||
*month = m;
|
||||
*day = d;
|
||||
}
|
||||
|
||||
#endif /* CONFIG_JULIAN_TIME */
|
||||
#else/* CONFIG_GREGORIAN_TIME */
|
||||
|
||||
/* Only handles dates since Jan 1, 1970 */
|
||||
|
||||
static void clock_utc2calendar(time_t days, int *year, int *month, int *day)
|
||||
{
|
||||
int value;
|
||||
int min;
|
||||
int max;
|
||||
int tmp;
|
||||
bool leapyear;
|
||||
|
||||
/* There is one leap year every four years, so we can get close with the
|
||||
* following:
|
||||
*/
|
||||
|
||||
value = days / (4*365 + 1); /* Number of 4-years periods since the epoch*/
|
||||
days -= value * (4*365 + 1); /* Remaining days */
|
||||
value <<= 2; /* Years since the epoch */
|
||||
|
||||
/* Then we will brute force the next 0-3 years */
|
||||
|
||||
for (;;)
|
||||
{
|
||||
/* Is this year a leap year (we'll need this later too) */
|
||||
|
||||
leapyear = clock_isleapyear(value + 1970);
|
||||
|
||||
/* Get the number of days in the year */
|
||||
|
||||
tmp = (leapyear ? 366 : 365);
|
||||
|
||||
/* Do we have that many days? */
|
||||
|
||||
if (days >= tmp)
|
||||
{
|
||||
/* Yes.. bump up the year */
|
||||
|
||||
value++;
|
||||
days -= tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Nope... then go handle months */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* At this point, value has the year and days has number days into this year */
|
||||
|
||||
*year = 1970 + value;
|
||||
|
||||
/* Handle the month (zero based) */
|
||||
|
||||
min = 0;
|
||||
max = 11;
|
||||
|
||||
do
|
||||
{
|
||||
/* Get the midpoint */
|
||||
|
||||
value = (min + max) >> 1;
|
||||
|
||||
/* Get the number of days that occurred before the beginning of the month
|
||||
* following the midpoint.
|
||||
*/
|
||||
|
||||
tmp = clock_daysbeforemonth(value + 1, leapyear);
|
||||
|
||||
/* Does the number of days before this month that equal or exceed the
|
||||
* number of days we have remaining?
|
||||
*/
|
||||
|
||||
if (tmp > days)
|
||||
{
|
||||
/* Yes.. then the month we want is somewhere from 'min' and to the
|
||||
* midpoint, 'value'. Could it be the midpoint?
|
||||
*/
|
||||
|
||||
tmp = clock_daysbeforemonth(value, leapyear);
|
||||
if (tmp > days)
|
||||
{
|
||||
/* No... The one we want is somewhere between min and value-1 */
|
||||
|
||||
max = value - 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Yes.. 'value' contains the month that we want */
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* No... The one we want is somwhere between value+1 and max */
|
||||
|
||||
min = value + 1;
|
||||
}
|
||||
|
||||
/* If we break out of the loop because min == max, then we want value
|
||||
* to be equal to min == max.
|
||||
*/
|
||||
|
||||
value = min;
|
||||
}
|
||||
while (min < max);
|
||||
|
||||
/* The selected month number is in value. Subtract the number of days in the
|
||||
* selected month
|
||||
*/
|
||||
|
||||
days -= clock_daysbeforemonth(value, leapyear);
|
||||
|
||||
/* At this point, value has the month into this year (zero based) and days has
|
||||
* number of days into this month (zero based)
|
||||
*/
|
||||
|
||||
*month = value + 1; /* 1-based */
|
||||
*day = days + 1; /* 1-based */
|
||||
}
|
||||
|
||||
#endif /* CONFIG_GREGORIAN_TIME */
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: gmtime_r
|
||||
*
|
||||
* Description:
|
||||
* Time conversion (based on the POSIX API)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
FAR struct tm *gmtime_r(FAR const time_t *timer, FAR struct tm *result)
|
||||
{
|
||||
time_t epoch;
|
||||
time_t jdn;
|
||||
int year;
|
||||
int month;
|
||||
int day;
|
||||
int hour;
|
||||
int min;
|
||||
int sec;
|
||||
|
||||
/* Get the seconds since the EPOCH */
|
||||
|
||||
epoch = *timer;
|
||||
sdbg("timer=%d\n", (int)epoch);
|
||||
|
||||
/* Convert to days, hours, minutes, and seconds since the EPOCH */
|
||||
|
||||
jdn = epoch / SEC_PER_DAY;
|
||||
epoch -= SEC_PER_DAY * jdn;
|
||||
|
||||
hour = epoch / SEC_PER_HOUR;
|
||||
epoch -= SEC_PER_HOUR * hour;
|
||||
|
||||
min = epoch / SEC_PER_MIN;
|
||||
epoch -= SEC_PER_MIN * min;
|
||||
|
||||
sec = epoch;
|
||||
|
||||
sdbg("hour=%d min=%d sec=%d\n",
|
||||
(int)hour, (int)min, (int)sec);
|
||||
|
||||
/* Convert the days since the EPOCH to calendar day */
|
||||
|
||||
clock_utc2calendar(jdn, &year, &month, &day);
|
||||
|
||||
sdbg("jdn=%d year=%d month=%d day=%d\n",
|
||||
(int)jdn, (int)year, (int)month, (int)day);
|
||||
|
||||
/* 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;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_isleapyear.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: clock_isleapyear
|
||||
*
|
||||
* Description:
|
||||
* Return true if the specified year is a leap year
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
int clock_isleapyear(int year)
|
||||
{
|
||||
return year % 400 ? (year % 100 ? (year % 4 ? 0 : 1) : 0) : 1;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,141 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_mktime.c
|
||||
*
|
||||
* Copyright (C) 2007, 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <time.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Variables
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: mktime
|
||||
*
|
||||
* Description:
|
||||
* Time conversion (based on the POSIX API)
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef CONFIG_GREGORIAN_TIME
|
||||
time_t mktime(const struct tm *tp)
|
||||
{
|
||||
time_t ret;
|
||||
time_t jdn;
|
||||
|
||||
/* Get the EPOCH-relative julian date from the calendar year,
|
||||
* month, and date
|
||||
*/
|
||||
|
||||
jdn = clock_calendar2utc(tp->tm_year+1900, tp->tm_mon+1, 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;
|
||||
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(const 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 */
|
||||
|
||||
@@ -0,0 +1,398 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_strftime.c
|
||||
*
|
||||
* Copyright (C) 2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <sys/types.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <debug.h>
|
||||
|
||||
#include <nuttx/time.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Type Declarations
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Constant Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const char * const g_abbrevmonthname[12] =
|
||||
{
|
||||
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
|
||||
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
||||
};
|
||||
|
||||
static const char * const g_monthname[12] =
|
||||
{
|
||||
"January", "February", "March", "April", "May", "June",
|
||||
"July", "August", "September", "October", "November", "December"
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: strftime
|
||||
*
|
||||
* Description:
|
||||
* The strftime() function formats the broken-down time tm according to
|
||||
* the format specification format and places the result in the character
|
||||
* array s of size max.
|
||||
*
|
||||
* Ordinary characters placed in the format string are copied to s without
|
||||
* conversion. Conversion specifications are introduced by a '%' charac-
|
||||
* ter, and terminated by a conversion specifier character, and are
|
||||
* replaced in s as follows:
|
||||
*
|
||||
* %b The abbreviated month name according to the current locale.
|
||||
* %B The full month name according to the current locale.
|
||||
* %C The century number (year/100) as a 2-digit integer. (SU)
|
||||
* %d The day of the month as a decimal number (range 01 to 31).
|
||||
* %e Like %d, the day of the month as a decimal number, but a leading
|
||||
* zero is replaced by a space.
|
||||
* %h Equivalent to %b. (SU)
|
||||
* %H The hour as a decimal number using a 24-hour clock (range 00 to 23).
|
||||
* %I The hour as a decimal number using a 12-hour clock (range 01 to 12).
|
||||
* %j The day of the year as a decimal number (range 001 to 366).
|
||||
* %k The hour (24-hour clock) as a decimal number (range 0 to 23);
|
||||
* single digits are preceded by a blank. (See also %H.) (TZ)
|
||||
* %l The hour (12-hour clock) as a decimal number (range 1 to 12);
|
||||
* single digits are preceded by a blank. (See also %I.) (TZ)
|
||||
* %m The month as a decimal number (range 01 to 12).
|
||||
* %M The minute as a decimal number (range 00 to 59).
|
||||
* %n A newline character. (SU)
|
||||
* %p Either "AM" or "PM" according to the given time value, or the
|
||||
* corresponding strings for the current locale. Noon is treated
|
||||
* as "PM" and midnight as "AM".
|
||||
* %P Like %p but in lowercase: "am" or "pm" or a corresponding string
|
||||
* for the current locale. (GNU)
|
||||
* %s The number of seconds since the Epoch, that is, since 1970-01-01
|
||||
* 00:00:00 UTC. (TZ)
|
||||
* %S The second as a decimal number (range 00 to 60). (The range is
|
||||
* up to 60 to allow for occasional leap seconds.)
|
||||
* %t A tab character. (SU)
|
||||
* %y The year as a decimal number without a century (range 00 to 99).
|
||||
* %Y The year as a decimal number including the century.
|
||||
* %% A literal '%' character.
|
||||
*
|
||||
* Returned Value:
|
||||
* The strftime() function returns the number of characters placed in the
|
||||
* array s, not including the terminating null byte, provided the string,
|
||||
* including the terminating null byte, fits. Otherwise, it returns 0,
|
||||
* and the contents of the array is undefined.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
size_t strftime(char *s, size_t max, const char *format, const struct tm *tm)
|
||||
{
|
||||
const char *str;
|
||||
char *dest = s;
|
||||
int chleft = max;
|
||||
int value;
|
||||
int len;
|
||||
|
||||
while (*format && chleft > 0)
|
||||
{
|
||||
/* Just copy regular characters */
|
||||
|
||||
if (*format != '%')
|
||||
{
|
||||
*dest++ = *format++;
|
||||
chleft--;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* Handle the format character */
|
||||
|
||||
format++;
|
||||
len = 0;
|
||||
|
||||
switch (*format++)
|
||||
{
|
||||
/* %a: A three-letter abbreviation for the day of the week. */
|
||||
/* %A: The full name for the day of the week. */
|
||||
|
||||
case 'a':
|
||||
case 'A':
|
||||
{
|
||||
len = snprintf(dest, chleft, "Day"); /* Not supported */
|
||||
}
|
||||
break;
|
||||
|
||||
/* %h: Equivalent to %b */
|
||||
|
||||
case 'h':
|
||||
|
||||
/* %b: The abbreviated month name according to the current locale. */
|
||||
|
||||
case 'b':
|
||||
{
|
||||
if (tm->tm_mon < 12)
|
||||
{
|
||||
str = g_abbrevmonthname[tm->tm_mon];
|
||||
len = snprintf(dest, chleft, "%s", str);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* %B: The full month name according to the current locale. */
|
||||
|
||||
case 'B':
|
||||
{
|
||||
if (tm->tm_mon < 12)
|
||||
{
|
||||
str = g_monthname[tm->tm_mon];
|
||||
len = snprintf(dest, chleft, "%s", str);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* %y: The year as a decimal number without a century (range 00 to 99). */
|
||||
|
||||
case 'y':
|
||||
|
||||
/* %C: The century number (year/100) as a 2-digit integer. */
|
||||
|
||||
case 'C':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_year % 100);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %d: The day of the month as a decimal number (range 01 to 31). */
|
||||
|
||||
case 'd':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_mday);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %e: Like %d, the day of the month as a decimal number, but a leading
|
||||
* zero is replaced by a space.
|
||||
*/
|
||||
|
||||
case 'e':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%2d", tm->tm_mday);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %H: The hour as a decimal number using a 24-hour clock (range 00 to 23). */
|
||||
|
||||
case 'H':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_hour);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %I: The hour as a decimal number using a 12-hour clock (range 01 to 12). */
|
||||
|
||||
case 'I':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_hour % 12);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %j: The day of the year as a decimal number (range 001 to 366). */
|
||||
|
||||
case 'j':
|
||||
{
|
||||
if (tm->tm_mon < 12)
|
||||
{
|
||||
value = clock_daysbeforemonth(tm->tm_mon, clock_isleapyear(tm->tm_year)) + tm->tm_mday;
|
||||
len = snprintf(dest, chleft, "%03d", value);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
/* %k: The hour (24-hour clock) as a decimal number (range 0 to 23);
|
||||
* single digits are preceded by a blank.
|
||||
*/
|
||||
|
||||
case 'k':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%2d", tm->tm_hour);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %l: The hour (12-hour clock) as a decimal number (range 1 to 12);
|
||||
* single digits are preceded by a blank.
|
||||
*/
|
||||
|
||||
case 'l':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%2d", tm->tm_hour % 12);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %m: The month as a decimal number (range 01 to 12). */
|
||||
|
||||
case 'm':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_mon + 1);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %M: The minute as a decimal number (range 00 to 59). */
|
||||
|
||||
case 'M':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_min);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %n: A newline character. */
|
||||
|
||||
case 'n':
|
||||
{
|
||||
*dest = '\n';
|
||||
len = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
/* %p: Either "AM" or "PM" according to the given time value. */
|
||||
|
||||
case 'p':
|
||||
{
|
||||
if (tm->tm_hour >= 12)
|
||||
{
|
||||
str = "PM";
|
||||
}
|
||||
else
|
||||
{
|
||||
str = "AM";
|
||||
}
|
||||
len = snprintf(dest, chleft, "%s", str);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %P: Like %p but in lowercase: "am" or "pm" */
|
||||
|
||||
case 'P':
|
||||
{
|
||||
if (tm->tm_hour >= 12)
|
||||
{
|
||||
str = "pm";
|
||||
}
|
||||
else
|
||||
{
|
||||
str = "am";
|
||||
}
|
||||
len = snprintf(dest, chleft, "%s", str);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %s: The number of seconds since the Epoch, that is, since 1970-01-01
|
||||
* 00:00:00 UTC.
|
||||
*/
|
||||
|
||||
case 's':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%d", mktime(tm));
|
||||
}
|
||||
break;
|
||||
|
||||
/* %S: The second as a decimal number (range 00 to 60). (The range is
|
||||
* up to 60 to allow for occasional leap seconds.)
|
||||
*/
|
||||
|
||||
case 'S':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%02d", tm->tm_sec);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %t: A tab character. */
|
||||
|
||||
case 't':
|
||||
{
|
||||
*dest = '\t';
|
||||
len = 1;
|
||||
}
|
||||
break;
|
||||
|
||||
/* %Y: The year as a decimal number including the century. */
|
||||
|
||||
case 'Y':
|
||||
{
|
||||
len = snprintf(dest, chleft, "%04d", tm->tm_year + 1900);
|
||||
}
|
||||
break;
|
||||
|
||||
/* %%: A literal '%' character. */
|
||||
|
||||
case '%':
|
||||
{
|
||||
*dest = '%';
|
||||
len = 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/* Update counts and pointers */
|
||||
|
||||
dest += len;
|
||||
chleft -= len;
|
||||
}
|
||||
|
||||
return max - chleft;
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/****************************************************************************
|
||||
* libc/time/lib_time.c
|
||||
*
|
||||
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* 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 <sys/time.h>
|
||||
#include <time.h>
|
||||
|
||||
#ifndef CONFIG_DISABLE_CLOCK
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Function: time
|
||||
*
|
||||
* Description:
|
||||
* Get the current calendar time as a time_t object. The function returns
|
||||
* this value, and if the argument is not a null pointer, the value is also
|
||||
* set to the object pointed by tloc.
|
||||
*
|
||||
* Note that this function is just a thin wrapper around gettimeofday()
|
||||
* and is provided for compatibility. gettimeofday() is the preffered way
|
||||
* to obtain system time.
|
||||
*
|
||||
* Parameters:
|
||||
* Pointer to an object of type time_t, where the time value is stored.
|
||||
* Alternativelly, this parameter can be a null pointer, in which case the
|
||||
* parameter is not used, but a time_t object is still returned by the
|
||||
* function.
|
||||
*
|
||||
* Return Value:
|
||||
* The current calendar time as a time_t object. If the argument is not
|
||||
* a null pointer, the return value is the same as the one stored in the
|
||||
* location pointed by the argument.
|
||||
*
|
||||
* If the function could not retrieve the calendar time, it returns a -1
|
||||
* value.
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
time_t time(time_t *tloc)
|
||||
{
|
||||
struct timeval tp;
|
||||
int ret;
|
||||
|
||||
/* Get the current time from the system */
|
||||
|
||||
ret = gettimeofday(&tp, NULL);
|
||||
if (ret == OK)
|
||||
{
|
||||
/* Return the seconds since the epoch */
|
||||
|
||||
if (tloc)
|
||||
{
|
||||
*tloc = tp.tv_sec;
|
||||
}
|
||||
|
||||
return tp.tv_sec;
|
||||
}
|
||||
|
||||
return (time_t)ERROR;
|
||||
}
|
||||
|
||||
#endif /* !CONFIG_DISABLE_CLOCK */
|
||||
Reference in New Issue
Block a user