From 631788ece166f88d86b1818e43c265a9d56e3497 Mon Sep 17 00:00:00 2001 From: patacongo Date: Sat, 11 Jul 2009 17:24:14 +0000 Subject: [PATCH] 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 --- ChangeLog | 6 + Documentation/NuttX.html | 7 +- Documentation/NuttxPortingGuide.html | 11 +- configs/README.txt | 8 +- include/nuttx/regex.h | 6 +- include/nuttx/time.h | 114 +++++++++++++++++++ lib/Makefile | 6 +- sched/gmtime_r.c => lib/lib_gmtimer.c | 157 +++++++++++++++++++++----- lib/lib_mktime.c | 149 ++++++++++++++++++++++++ sched/mktime.c => lib/lib_timeutils.c | 119 ++++++++++--------- sched/Makefile | 4 +- sched/clock_initialize.c | 60 +++++----- sched/clock_internal.h | 14 +-- 13 files changed, 524 insertions(+), 137 deletions(-) create mode 100644 include/nuttx/time.h rename sched/gmtime_r.c => lib/lib_gmtimer.c (53%) create mode 100644 lib/lib_mktime.c rename sched/mktime.c => lib/lib_timeutils.c (56%) diff --git a/ChangeLog b/ChangeLog index 65fcd4ebd4..97bb304f1f 100644 --- a/ChangeLog +++ b/ChangeLog @@ -804,5 +804,11 @@ 0.4.10 2009-xx-xx Gregory Nutt + * 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/Documentation/NuttX.html b/Documentation/NuttX.html index 36eba419ba..627a5bd6c6 100644 --- a/Documentation/NuttX.html +++ b/Documentation/NuttX.html @@ -8,7 +8,7 @@

NuttX RTOS

-

Last Updated: June 26, 2009

+

Last Updated: July 11, 2009

@@ -1477,6 +1477,11 @@ buildroot-0.1.7 2009-06-26 <spudmonkey@racsa.co.cr>
    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/Documentation/NuttxPortingGuide.html b/Documentation/NuttxPortingGuide.html index 2580cf0b03..9acd622cdd 100644 --- a/Documentation/NuttxPortingGuide.html +++ b/Documentation/NuttxPortingGuide.html @@ -12,7 +12,7 @@

    NuttX RTOS Porting Guide

    -

    Last Updated: June 18, 2009

    +

    Last Updated: July 11, 2009

    @@ -2054,7 +2054,14 @@ extern void up_ledoff(int led); 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 recent 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 diff --git a/configs/README.txt b/configs/README.txt index 8559418700..04833669b0 100644 --- a/configs/README.txt +++ b/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/include/nuttx/regex.h b/include/nuttx/regex.h index b6fee93746..15f44a7478 100644 --- a/include/nuttx/regex.h +++ b/include/nuttx/regex.h @@ -49,11 +49,7 @@ ****************************************************************************/ /**************************************************************************** - * Global Function Prototypes - ****************************************************************************/ - -/**************************************************************************** - * Global Function Prototypes + * Public Function Prototypes ****************************************************************************/ #ifdef __cplusplus diff --git a/include/nuttx/time.h b/include/nuttx/time.h new file mode 100644 index 0000000000..1e5e50cb52 --- /dev/null +++ b/include/nuttx/time.h @@ -0,0 +1,114 @@ +/**************************************************************************** + * include/nuttx/time.h + * + * Copyright (C) 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 +#include + +/**************************************************************************** + * 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/lib/Makefile b/lib/Makefile index e00eb5c8c4..b2792cdf38 100644 --- a/lib/Makefile +++ b/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/sched/gmtime_r.c b/lib/lib_gmtimer.c similarity index 53% rename from sched/gmtime_r.c rename to lib/lib_gmtimer.c index 98658676e1..2758acd1a7 100644 --- a/sched/gmtime_r.c +++ b/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 * * 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 #include + #include #include #include -#include "clock_internal.h" -/************************************************************ +#include + +/**************************************************************************** * 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/lib/lib_mktime.c b/lib/lib_mktime.c new file mode 100644 index 0000000000..594a0672b0 --- /dev/null +++ b/lib/lib_mktime.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * lib/lib_mktime.c + * + * Copyright (C) 2007, 2009 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include +#include + +#include + +/**************************************************************************** + * 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/sched/mktime.c b/lib/lib_timeutils.c similarity index 56% rename from sched/mktime.c rename to lib/lib_timeutils.c index f842e2435b..31404edded 100644 --- a/sched/mktime.c +++ b/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 * * 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 #include + #include #include -#include "clock_internal.h" -/************************************************************ +#include + +/**************************************************************************** * 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/sched/Makefile b/sched/Makefile index b16ab0b948..288aade45c 100644 --- a/sched/Makefile +++ b/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/sched/clock_initialize.c b/sched/clock_initialize.c index 7b63a65215..8dc8a00e83 100644 --- a/sched/clock_initialize.c +++ b/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 * * 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 #include + #include #include #include +#include + #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/sched/clock_internal.h b/sched/clock_internal.h index 2c298983b0..7d1dbb6293 100644 --- a/sched/clock_internal.h +++ b/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 * * 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);