From c9dded6775ca8a0934739f56c29337a893b5da7c Mon Sep 17 00:00:00 2001 From: Jeff Johnston Date: Thu, 11 Sep 2014 21:55:01 +0000 Subject: 2014-09-11 Freddie Chopin * libc/time/month_lengths.c: New file with __month_lengths array (previously mon_lengths array in mktm_r.c) * libc/time/tzcalc_limits.c: New file with __tzcalc_limits() from mktm_r.c * libc/time/lcltime_r.c (localtime_r): Simplify by changing call to _mktm_r() with call to gmtime_r() and code moved from _mktm_r() which was used to do time zone adjustments * libc/time/gmtime_r.c (gmtime_r): Simplify by moving all relevant code from _mktm_r(), breaking all dependencies on time zone related functions * libc/time/mktm_r.c: Delete file * libc/time/local.h: Update accordingly - remove declaration of _mktm_r(), add declaration of __month_lengths[] * libc/time/Makefile.am: Modify accordingly. * libc/time/Makefile.in: Regenerate. --- newlib/libc/time/gmtime_r.c | 97 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 95 insertions(+), 2 deletions(-) (limited to 'newlib/libc/time/gmtime_r.c') diff --git a/newlib/libc/time/gmtime_r.c b/newlib/libc/time/gmtime_r.c index 14c9c7640..1111cbc0e 100644 --- a/newlib/libc/time/gmtime_r.c +++ b/newlib/libc/time/gmtime_r.c @@ -1,14 +1,107 @@ /* * gmtime_r.c + * Original Author: Adapted from tzcode maintained by Arthur David Olson. + * Modifications: + * - Changed to mktm_r and added __tzcalc_limits - 04/10/02, Jeff Johnston + * - Fixed bug in mday computations - 08/12/04, Alex Mogilnikov + * - Fixed bug in __tzcalc_limits - 08/12/04, Alex Mogilnikov + * - Move code from _mktm_r() to gmtime_r() - 05/09/14, Freddie Chopin + * + * Converts the calendar time pointed to by tim_p into a broken-down time + * expressed as local time. Returns a pointer to a structure containing the + * broken-down time. */ -#include #include "local.h" +/* there are 97 leap years in 400-year periods */ +#define DAYS_PER_400_YEARS ((400 - 97) * 365 + 97 * 366) +/* there are 24 leap years in 100-year periods */ +#define DAYS_PER_100_YEARS ((100 - 24) * 365 + 24 * 366) +/* there is one leap year every 4 years */ +#define DAYS_PER_4_YEARS (3 * 365 + 366) + +static _CONST int days_per_year[4] = { + 365, /* 1970 or 1966 */ + 365, /* 1971 or 1967 */ + 366, /* 1972 or 1968 */ + 365 /* 1973 or 1969 */ +} ; + struct tm * _DEFUN (gmtime_r, (tim_p, res), _CONST time_t *__restrict tim_p _AND struct tm *__restrict res) { - return (_mktm_r (tim_p, res, 1)); + long days, rem; + _CONST time_t lcltime = *tim_p; + int year; + int years400, years100, years4; + int yearleap; + _CONST int *ip; + + days = ((long)lcltime) / SECSPERDAY; + rem = ((long)lcltime) % SECSPERDAY; + while (rem < 0) + { + rem += SECSPERDAY; + --days; + } + while (rem >= SECSPERDAY) + { + rem -= SECSPERDAY; + ++days; + } + + /* compute hour, min, and sec */ + res->tm_hour = (int) (rem / SECSPERHOUR); + rem %= SECSPERHOUR; + res->tm_min = (int) (rem / SECSPERMIN); + res->tm_sec = (int) (rem % SECSPERMIN); + + /* compute day of week */ + if ((res->tm_wday = ((EPOCH_WDAY + days) % DAYSPERWEEK)) < 0) + res->tm_wday += DAYSPERWEEK; + + /* compute year & day of year */ + years400 = days / DAYS_PER_400_YEARS; + days -= years400 * DAYS_PER_400_YEARS; + years100 = days / DAYS_PER_100_YEARS; + days -= years100 * DAYS_PER_100_YEARS; + years4 = days / DAYS_PER_4_YEARS; + days -= years4 * DAYS_PER_4_YEARS; + + year = EPOCH_YEAR + years400 * 400 + years100 * 100 + years4 * 4; + + /* the value left in days is based in 1970 */ + if (days < 0) + { + ip = &days_per_year[3]; + while (days < 0) + { + days += *ip--; + --year; + } + } + else + { + ip = &days_per_year[0]; + while (days >= *ip) + { + days -= *ip++; + ++year; + } + } + + res->tm_year = year - YEAR_BASE; + res->tm_yday = days; + yearleap = isleap(year); + ip = __month_lengths[yearleap]; + for (res->tm_mon = 0; days >= ip[res->tm_mon]; ++res->tm_mon) + days -= ip[res->tm_mon]; + res->tm_mday = days + 1; + + res->tm_isdst = 0; + + return (res); } -- cgit v1.2.3