diff options
author | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-07-11 17:24:14 +0000 |
---|---|---|
committer | patacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3> | 2009-07-11 17:24:14 +0000 |
commit | fb117bfced091fd2ee5a32f2ab39b6d7a48e0676 (patch) | |
tree | 5ff18dab2d32ec4643b3d664522b3ed018d0f60c /nuttx | |
parent | 4a528af5569e0385555ac4f5de09677f6bcef754 (diff) | |
download | px4-nuttx-fb117bfced091fd2ee5a32f2ab39b6d7a48e0676.tar.gz px4-nuttx-fb117bfced091fd2ee5a32f2ab39b6d7a48e0676.tar.bz2 px4-nuttx-fb117bfced091fd2ee5a32f2ab39b6d7a48e0676.zip |
mktime/gmtime_r moved and highly simplified
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@1973 42af7a65-404d-4744-a932-0658087f49c3
Diffstat (limited to 'nuttx')
-rw-r--r-- | nuttx/ChangeLog | 6 | ||||
-rw-r--r-- | nuttx/Documentation/NuttX.html | 7 | ||||
-rw-r--r-- | nuttx/Documentation/NuttxPortingGuide.html | 11 | ||||
-rw-r--r-- | nuttx/configs/README.txt | 8 | ||||
-rw-r--r-- | nuttx/include/nuttx/regex.h | 6 | ||||
-rw-r--r-- | nuttx/include/nuttx/time.h | 114 | ||||
-rw-r--r-- | nuttx/lib/Makefile | 6 | ||||
-rw-r--r-- | nuttx/lib/lib_gmtimer.c (renamed from nuttx/sched/gmtime_r.c) | 157 | ||||
-rw-r--r-- | nuttx/lib/lib_mktime.c | 149 | ||||
-rw-r--r-- | nuttx/lib/lib_timeutils.c (renamed from nuttx/sched/mktime.c) | 119 | ||||
-rw-r--r-- | nuttx/sched/Makefile | 4 | ||||
-rw-r--r-- | nuttx/sched/clock_initialize.c | 60 | ||||
-rw-r--r-- | nuttx/sched/clock_internal.h | 14 |
13 files changed, 524 insertions, 137 deletions
diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 65fcd4ebd..97bb304f1 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -804,5 +804,11 @@ 0.4.10 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> + * lib/: Added some basic regex-subset, pattern matching functions + * lib/: Greatly simplified mktime() and gmtime_r(). The Gregorian and + Julian time calculations were interesting, but not necessary in the + typical embeddd system. + + diff --git a/nuttx/Documentation/NuttX.html b/nuttx/Documentation/NuttX.html index 36eba419b..627a5bd6c 100644 --- a/nuttx/Documentation/NuttX.html +++ b/nuttx/Documentation/NuttX.html @@ -8,7 +8,7 @@ <tr align="center" bgcolor="#e4e4e4"> <td> <h1><big><font color="#3c34ec"><i>NuttX RTOS</i></font></big></h1> - <p>Last Updated: June 26, 2009</p> + <p>Last Updated: July 11, 2009</p> </td> </tr> </table> @@ -1477,6 +1477,11 @@ buildroot-0.1.7 2009-06-26 <spudmonkey@racsa.co.cr> <pre><ul> nuttx-0.4.10 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> + * lib/: Added some basic regex-subset, pattern matching functions + * lib/: Greatly simplified mktime() and gmtime_r(). The Gregorian and + Julian time calculations were interesting, but not necessary in the + typical embeddd system. + nuttx-0.4.10 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> pascal-0.1.3 2009-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> diff --git a/nuttx/Documentation/NuttxPortingGuide.html b/nuttx/Documentation/NuttxPortingGuide.html index 2580cf0b0..9acd622cd 100644 --- a/nuttx/Documentation/NuttxPortingGuide.html +++ b/nuttx/Documentation/NuttxPortingGuide.html @@ -12,7 +12,7 @@ <h1><big><font color="#3c34ec"> <i>NuttX RTOS Porting Guide</i> </font></big></h1> - <p>Last Updated: June 18, 2009</p> + <p>Last Updated: July 11, 2009</p> </td> </tr> </table> @@ -2054,7 +2054,14 @@ extern void up_ledoff(int led); Used to initialize the internal time logic. </li> <li> - <code>CONFIG_JULIAN_TIME</code>: Enables Julian time conversions + <code>CONFIG_GREGORIAN_TIME</code>: Enables Gregorian time conversions. + You would only need this if you are concerned about accurate time conversions in + the recent past or in the distant future. + </li> + <li> + <code>CONFIG_JULIAN_TIME</code>: Enables Julian time conversions. + You would only need this if you are concerned about accurate time conversion in the distand past. + You must also define <code>CONFIG_GREGORIAN_TIME</code> in order to use Julian time. </li> <li> <code>CONFIG_DEV_CONSOLE</code>: Set if architecture-specific logic diff --git a/nuttx/configs/README.txt b/nuttx/configs/README.txt index 855941870..04833669b 100644 --- a/nuttx/configs/README.txt +++ b/nuttx/configs/README.txt @@ -194,7 +194,13 @@ defconfig -- This is a configuration file similar to the Linux instrumentation is selected. Set to zero to disable. CONFIG_START_YEAR, CONFIG_START_MONTH, CONFIG_START_DAY - Used to initialize the internal time logic. - CONFIG_JULIAN_TIME - Enables Julian time conversions + CONFIG_GREGORIAN_TIME - Enables Gregorian time conversions. + You would only need this if you are concerned about accurate + time conversions in the past or in the distant future. + CONFIG_JULIAN_TIME - Enables Julian time conversions. You + would only need this if you are concerned about accurate + time conversion in the distand past. You must also define + CONFIG_GREGORIAN_TIME in order to use Julian time. CONFIG_DEV_CONSOLE - Set if architecture-specific logic provides /dev/console. Enables stdout, stderr, stdin. CONFIG_MUTEX_TYPES - Set to enable support for recursive and diff --git a/nuttx/include/nuttx/regex.h b/nuttx/include/nuttx/regex.h index b6fee9374..15f44a747 100644 --- a/nuttx/include/nuttx/regex.h +++ b/nuttx/include/nuttx/regex.h @@ -49,11 +49,7 @@ ****************************************************************************/ /**************************************************************************** - * Global Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Global Function Prototypes + * Public Function Prototypes ****************************************************************************/ #ifdef __cplusplus diff --git a/nuttx/include/nuttx/time.h b/nuttx/include/nuttx/time.h new file mode 100644 index 000000000..1e5e50cb5 --- /dev/null +++ b/nuttx/include/nuttx/time.h @@ -0,0 +1,114 @@ +/**************************************************************************** + * include/nuttx/time.h + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_TIME_H +#define __INCLUDE_NUTTX_TIME_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> +#include <sys/types.h> + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ + +/* If Gregorian time is not supported, then neither is Julian */ + +#ifndef CONFIG_GREGORIAN_TIME +# undef CONFIG_JULIAN_TIME +#else +# define JD_OF_EPOCH 2440588 /* Julian Date of noon, J1970 */ + +# ifdef CONFIG_JULIAN_TIME +# define GREG_DUTC -141427 /* Default is October 15, 1582 */ +# define GREG_YEAR 1582 +# define GREG_MONTH 10 +# define GREG_DAY 15 +# endif /* CONFIG_JULIAN_TIME */ +#endif /* !CONFIG_GREGORIAN_TIME */ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#ifndef CONFIG_GREGORIAN_TIME +extern uint16 g_daysbeforemonth[13]; +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#ifdef __cplusplus +#define EXTERN extern "C" +extern "C" { +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Function: clock_isleapyear + * + * Description: + * Return true if the specified year is a leap year + * + ****************************************************************************/ + +#ifndef CONFIG_GREGORIAN_TIME +EXTERN int clock_isleapyear(int year); +#endif + +/**************************************************************************** + * 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. + * + ****************************************************************************/ + +EXTERN time_t clock_calendar2utc(int year, int month, int day); + +#undef EXTERN +#ifdef __cplusplus +} +#endif + +#endif /* __INCLUDE_NUTTX_TIME_H */ diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile index e00eb5c8c..b2792cdf3 100644 --- a/nuttx/lib/Makefile +++ b/nuttx/lib/Makefile @@ -83,6 +83,8 @@ UNISTD_SRCS += lib_chdir.c lib_getcwd.c endif endif +TIME_SRCS = lib_mktime.c lib_gmtimer.c lib_timeutils.c + NET_SRCS = lib_htons.c lib_htonl.c lib_inetntoa.c lib_etherntoa.c LIBGEN_SRCS = lib_basename.c lib_dirname.c @@ -98,8 +100,8 @@ DQ_SRCS = dq_addlast.c dq_addfirst.c dq_addafter.c dq_addbefore.c \ DBG_SRCS = lib_dbg.c lib_dumpbuffer.c CSRCS = $(MISC_SRCS) $(STRING_SRCS) $(CTYPE_SRCS) $(STDIO_SRCS) $(STDLIB_SRCS) \ - $(MATH_SRCS) $(UNISTD_SRCS) $(NET_SRCS) $(LIBGEN_SRCS) $(REGEX_SRCS) \ - $(SQ_SRCS) $(DQ_SRCS) $(DBG_SRCS) + $(MATH_SRCS) $(UNISTD_SRCS) $(TIME_SRCS) $(NET_SRCS) $(LIBGEN_SRCS) \ + $(REGEX_SRCS) $(SQ_SRCS) $(DQ_SRCS) $(DBG_SRCS) COBJS = $(CSRCS:.c=$(OBJEXT)) SRCS = $(ASRCS) $(CSRCS) diff --git a/nuttx/sched/gmtime_r.c b/nuttx/lib/lib_gmtimer.c index 98658676e..2758acd1a 100644 --- a/nuttx/sched/gmtime_r.c +++ b/nuttx/lib/lib_gmtimer.c @@ -1,7 +1,7 @@ -/************************************************************ - * gmtime_r.c +/**************************************************************************** + * lib/lib_gmtimer.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 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. * @@ -31,56 +31,61 @@ * 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 <time.h> #include <errno.h> #include <debug.h> -#include "clock_internal.h" -/************************************************************ +#include <nuttx/time.h> + +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * 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 * @@ -90,8 +95,9 @@ static void clock_utc2julian(time_t jdn, int *year, int *month, int *day); * 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 @@ -132,8 +138,8 @@ static void clock_utc2gregorian(time_t jd, int *year, int *month, int *day) *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; @@ -153,26 +159,114 @@ static void clock_utc2julian(time_t jd, int *year, int *month, int *day) *month = m; *day = d; } + #endif /* CONFIG_JULIAN_TIME */ +#else/* CONFIG_GREGORIAN_TIME */ + +static void clock_utc2calendar(time_t days, int *year, int *month, int *day) +{ + int value; + int tmp; + boolean leapyear; + + /* There must be a better way to do this than the brute for method below */ + + value = 70; + for (;;) + { + /* Is this year a leap year (we'll need this later too) */ + + leapyear = clock_isleapyear(value + 1900); + + /* 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 = value; + + /* Handle the month */ + + value = 0; /* zero-based */ + for (;;) + { + /* Get the number of days that occurred before the beginning of the next month */ + + tmp = g_daysbeforemonth[value + 1]; + if (value >= 2 && leapyear) + { + tmp++; + } + + /* Does that equal or exceed the number of days we have remaining? */ + + if (tmp >= days) + { + /* Yes.. this is the one we want. The 'days' for this number of days that + * occurred before this month. + */ + + days -= g_daysbeforemonth[value]; + break; + } + else + { + /* No... try the next month */ + + value++; + } + } + + /* 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; + *day = days + 1; /* 1-based */ +} + +#endif /* CONFIG_GREGORIAN_TIME */ + +/**************************************************************************** * Public Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Function: gmtime_r * * Description: * Time conversion (based on the POSIX API) * - ************************************************************/ + ****************************************************************************/ struct tm *gmtime_r(const time_t *clock, struct tm *result) { time_t time; time_t jdn; - int year, month, day; - int hour, min, sec; + int year; + int month; + int day; + int hour; + int min; + int sec; /* Get the seconds since the EPOCH */ @@ -213,3 +307,4 @@ struct tm *gmtime_r(const time_t *clock, struct tm *result) return result; } + diff --git a/nuttx/lib/lib_mktime.c b/nuttx/lib/lib_mktime.c new file mode 100644 index 000000000..594a0672b --- /dev/null +++ b/nuttx/lib/lib_mktime.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * lib/lib_mktime.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt <spudmonkey@racsa.co.cr> + * + * 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 <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(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 on longer days where leap + * seconds, etc. apply. + */ + +time_t mktime(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 (ignoring any possible leap day). */ + + days += (time_t)g_daysbeforemonth[tp->tm_mon]; + + /* Add in the leap day for this year (months are zero based) */ + + if (tp->tm_mon >= 2 && clock_isleapyear(tp->tm_year + 1900)) + { + days++; + } + + /* 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 */ + diff --git a/nuttx/sched/mktime.c b/nuttx/lib/lib_timeutils.c index f842e2435..31404edde 100644 --- a/nuttx/sched/mktime.c +++ b/nuttx/lib/lib_timeutils.c @@ -1,7 +1,7 @@ -/************************************************************ - * mktime.c +/**************************************************************************** + * lib/lib_calendar2utc.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 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. * @@ -31,46 +31,56 @@ * 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 <time.h> #include <debug.h> -#include "clock_internal.h" -/************************************************************ +#include <nuttx/time.h> + +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Type Declarations - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ -/********************************************************** +/**************************************************************************** * Public Constant Data - **********************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Variables - ************************************************************/ + ****************************************************************************/ + +#ifndef CONFIG_GREGORIAN_TIME +uint16 g_daysbeforemonth[13] = +{ + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 +}; +#endif -/********************************************************** +/**************************************************************************** * Private Variables - **********************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Function: clock_gregorian2utc, clock_julian2utc * * Description: @@ -79,8 +89,9 @@ * 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; @@ -102,43 +113,29 @@ static time_t clock_julian2utc(int year, int month, int day) + (275*month)/9 + day + 1729777; } -#endif +#endif /* CONFIG_JULIAN_TIME */ +#endif /* CONFIG_GREGORIAN_TIME */ -/************************************************************ +/**************************************************************************** * Public Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ - * Function: mktime +/**************************************************************************** + * Function: clock_isleapyear * * Description: - * Time conversion (based on the POSIX API) + * Return true if the specified year is a leap year * - ************************************************************/ + ****************************************************************************/ -time_t mktime(struct tm *tp) +#ifndef CONFIG_GREGORIAN_TIME +int clock_isleapyear(int year) { - 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; + return year % 400 ? (year % 100 ? (year % 4 ? 0 : 1) : 0) : 1; } +#endif /* !CONFIG_GREGORIAN_TIME */ -/************************************************************ +/**************************************************************************** * Function: clock_calendar2utc * * Description: @@ -147,8 +144,9 @@ time_t mktime(struct tm *tp) * the Astronomical Almanac. University Science Books, * Mill Valley. * - ************************************************************/ + ****************************************************************************/ +#ifdef CONFIG_GREGORIAN_TIME time_t clock_calendar2utc(int year, int month, int day) { int dyear; @@ -209,3 +207,20 @@ time_t clock_calendar2utc(int year, int month, int day) #endif /* CONFIG_JULIAN_TIME */ } +#else +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 */ + diff --git a/nuttx/sched/Makefile b/nuttx/sched/Makefile index b16ab0b94..288aade45 100644 --- a/nuttx/sched/Makefile +++ b/nuttx/sched/Makefile @@ -71,8 +71,8 @@ TIME_SRCS = sched_processtimer.c ifneq ($(CONFIG_DISABLE_SIGNALS),y) TIME_SRCS += sleep.c usleep.c endif -CLOCK_SRCS = clock_initialize.c mktime.c gmtime_r.c clock_settime.c clock_gettime.c \ - clock_getres.c clock_time2ticks.c clock_abstime2ticks.c clock_ticks2time.c +CLOCK_SRCS = clock_initialize.c clock_settime.c clock_gettime.c clock_getres.c \ + clock_time2ticks.c clock_abstime2ticks.c clock_ticks2time.c SIGNAL_SRCS = sig_initialize.c \ sig_action.c sig_procmask.c sig_pending.c sig_suspend.c \ diff --git a/nuttx/sched/clock_initialize.c b/nuttx/sched/clock_initialize.c index 7b63a6521..8dc8a00e8 100644 --- a/nuttx/sched/clock_initialize.c +++ b/nuttx/sched/clock_initialize.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * clock_initialize.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 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. * @@ -31,61 +31,65 @@ * 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 <time.h> #include <errno.h> #include <debug.h> +#include <nuttx/time.h> + #include "clock_internal.h" -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Type Declarations - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ -/********************************************************** +/************************************************************************** * Public Constant Data - **********************************************************/ + **************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Variables - ************************************************************/ + ****************************************************************************/ volatile uint32 g_system_timer = 0; -struct timespec g_basetime = {0,0}; -uint32 g_tickbias = 0; +struct timespec g_basetime = {0,0}; +uint32 g_tickbias = 0; -/********************************************************** +/************************************************************************** * Private Variables - **********************************************************/ + **************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Functions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Function: clock_initialize * * Description: * Perform one-time initialization of the timing facilities. * - ************************************************************/ + ****************************************************************************/ void clock_initialize(void) { @@ -112,7 +116,7 @@ void clock_initialize(void) g_tickbias = 0; } -/************************************************************ +/**************************************************************************** * Function: clock_timer * * Description: @@ -120,7 +124,7 @@ void clock_initialize(void) * time clock interrupt occurs. The interval of this * clock interrupt must be MSEC_PER_TICK * - ************************************************************/ + ****************************************************************************/ void clock_timer(void) { diff --git a/nuttx/sched/clock_internal.h b/nuttx/sched/clock_internal.h index 2c298983b..7d1dbb629 100644 --- a/nuttx/sched/clock_internal.h +++ b/nuttx/sched/clock_internal.h @@ -1,7 +1,7 @@ /******************************************************************************** * clock_internal.h * - * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. + * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <spudmonkey@racsa.co.cr> * * Redistribution and use in source and binary forms, with or without @@ -49,17 +49,6 @@ * Definitions ********************************************************************************/ -#define JD_OF_EPOCH 2440588 /* Julian Date of noon, J1970 */ - -#ifdef CONFIG_JULIAN_TIME - -# define GREG_DUTC -141427 /* Default is October 15, 1582 */ -# define GREG_YEAR 1582 -# define GREG_MONTH 10 -# define GREG_DAY 15 - -#endif /* CONFIG_JULIAN_TIME */ - /******************************************************************************** * Public Type Definitions ********************************************************************************/ @@ -78,7 +67,6 @@ extern uint32 g_tickbias; extern void weak_function clock_initialize(void); extern void weak_function clock_timer(void); -extern time_t clock_calendar2utc(int year, int month, int day); extern int clock_abstime2ticks(clockid_t clockid, FAR const struct timespec *abstime, FAR int *ticks); |