2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
* gmtime_r.c
|
2014-09-12 05:55:01 +08:00
|
|
|
* 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 <alx@intellectronika.ru>
|
|
|
|
* - Fixed bug in __tzcalc_limits - 08/12/04, Alex Mogilnikov <alx@intellectronika.ru>
|
|
|
|
* - Move code from _mktm_r() to gmtime_r() - 05/09/14, Freddie Chopin <freddie_chopin@op.pl>
|
2014-12-08 19:48:31 +08:00
|
|
|
* - Fixed bug in calculations for dates after year 2069 or before year 1901. Ideas for
|
|
|
|
* solution taken from musl's __secs_to_tm() - 07/12/2014, Freddie Chopin
|
|
|
|
* <freddie_chopin@op.pl>
|
2015-06-15 18:00:59 +08:00
|
|
|
* - Use faster algorithm from civil_from_days() by Howard Hinnant - 12/06/2014,
|
|
|
|
* Freddie Chopin <freddie_chopin@op.pl>
|
2014-09-12 05:55:01 +08:00
|
|
|
*
|
|
|
|
* 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.
|
2000-02-18 03:39:52 +08:00
|
|
|
*/
|
|
|
|
|
2002-04-18 05:23:31 +08:00
|
|
|
#include "local.h"
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2015-06-15 18:00:59 +08:00
|
|
|
/* Move epoch from 01.01.1970 to 01.03.0000 (yes, Year 0) - this is the first
|
|
|
|
* day of a 400-year long "era", right after additional day of leap year.
|
|
|
|
* This adjustment is required only for date calculation, so instead of
|
|
|
|
* modifying time_t value (which would require 64-bit operations to work
|
|
|
|
* correctly) it's enough to adjust the calculated number of days since epoch.
|
|
|
|
*/
|
|
|
|
#define EPOCH_ADJUSTMENT_DAYS 719468L
|
2014-12-08 19:48:31 +08:00
|
|
|
/* year to which the adjustment was made */
|
2015-06-15 18:00:59 +08:00
|
|
|
#define ADJUSTED_EPOCH_YEAR 0
|
|
|
|
/* 1st March of year 0 is Wednesday */
|
2014-12-08 19:48:31 +08:00
|
|
|
#define ADJUSTED_EPOCH_WDAY 3
|
2014-11-12 17:10:22 +08:00
|
|
|
/* there are 97 leap years in 400-year periods. ((400 - 97) * 365 + 97 * 366) */
|
2015-06-15 18:00:59 +08:00
|
|
|
#define DAYS_PER_ERA 146097L
|
2014-11-12 17:10:22 +08:00
|
|
|
/* there are 24 leap years in 100-year periods. ((100 - 24) * 365 + 24 * 366) */
|
2015-06-15 18:00:59 +08:00
|
|
|
#define DAYS_PER_CENTURY 36524L
|
2014-09-12 05:55:01 +08:00
|
|
|
/* there is one leap year every 4 years */
|
|
|
|
#define DAYS_PER_4_YEARS (3 * 365 + 366)
|
2014-12-08 19:48:31 +08:00
|
|
|
/* number of days in a non-leap year */
|
|
|
|
#define DAYS_PER_YEAR 365
|
|
|
|
/* number of days in January */
|
|
|
|
#define DAYS_IN_JANUARY 31
|
|
|
|
/* number of days in non-leap February */
|
|
|
|
#define DAYS_IN_FEBRUARY 28
|
2015-06-15 18:00:59 +08:00
|
|
|
/* number of years per era */
|
|
|
|
#define YEARS_PER_ERA 400
|
2014-09-12 05:55:01 +08:00
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
struct tm *
|
2017-12-04 11:43:30 +08:00
|
|
|
gmtime_r (const time_t *__restrict tim_p,
|
2013-11-25 21:46:23 +08:00
|
|
|
struct tm *__restrict res)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2014-09-12 05:55:01 +08:00
|
|
|
long days, rem;
|
2017-12-04 10:25:16 +08:00
|
|
|
const time_t lcltime = *tim_p;
|
2015-06-15 18:00:59 +08:00
|
|
|
int era, weekday, year;
|
|
|
|
unsigned erayear, yearday, month, day;
|
|
|
|
unsigned long eraday;
|
2014-09-12 05:55:01 +08:00
|
|
|
|
2017-09-07 21:01:47 +08:00
|
|
|
days = lcltime / SECSPERDAY + EPOCH_ADJUSTMENT_DAYS;
|
|
|
|
rem = lcltime % SECSPERDAY;
|
2014-12-08 19:48:31 +08:00
|
|
|
if (rem < 0)
|
2014-09-12 05:55:01 +08:00
|
|
|
{
|
|
|
|
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 */
|
2014-12-08 19:48:31 +08:00
|
|
|
if ((weekday = ((ADJUSTED_EPOCH_WDAY + days) % DAYSPERWEEK)) < 0)
|
|
|
|
weekday += DAYSPERWEEK;
|
|
|
|
res->tm_wday = weekday;
|
2014-09-12 05:55:01 +08:00
|
|
|
|
2015-06-15 18:00:59 +08:00
|
|
|
/* compute year, month, day & day of year */
|
|
|
|
/* for description of this algorithm see
|
|
|
|
* http://howardhinnant.github.io/date_algorithms.html#civil_from_days */
|
|
|
|
era = (days >= 0 ? days : days - (DAYS_PER_ERA - 1)) / DAYS_PER_ERA;
|
|
|
|
eraday = days - era * DAYS_PER_ERA; /* [0, 146096] */
|
|
|
|
erayear = (eraday - eraday / (DAYS_PER_4_YEARS - 1) + eraday / DAYS_PER_CENTURY -
|
|
|
|
eraday / (DAYS_PER_ERA - 1)) / 365; /* [0, 399] */
|
|
|
|
yearday = eraday - (DAYS_PER_YEAR * erayear + erayear / 4 - erayear / 100); /* [0, 365] */
|
|
|
|
month = (5 * yearday + 2) / 153; /* [0, 11] */
|
|
|
|
day = yearday - (153 * month + 2) / 5 + 1; /* [1, 31] */
|
|
|
|
month += month < 10 ? 2 : -10;
|
|
|
|
year = ADJUSTED_EPOCH_YEAR + erayear + era * YEARS_PER_ERA + (month <= 1);
|
2014-12-08 19:48:31 +08:00
|
|
|
|
2015-06-15 18:00:59 +08:00
|
|
|
res->tm_yday = yearday >= DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY ?
|
|
|
|
yearday - (DAYS_PER_YEAR - DAYS_IN_JANUARY - DAYS_IN_FEBRUARY) :
|
|
|
|
yearday + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + isleap(erayear);
|
2014-12-08 19:48:31 +08:00
|
|
|
res->tm_year = year - YEAR_BASE;
|
|
|
|
res->tm_mon = month;
|
2015-06-15 18:00:59 +08:00
|
|
|
res->tm_mday = day;
|
2014-09-12 05:55:01 +08:00
|
|
|
|
|
|
|
res->tm_isdst = 0;
|
|
|
|
|
|
|
|
return (res);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|