Changeset View
Changeset View
Standalone View
Standalone View
contrib/tzcode/stdtime/localtime.c
Show First 20 Lines • Show All 1,515 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
gmtime_key_error = _pthread_key_create(&gmtime_key, free); | gmtime_key_error = _pthread_key_create(&gmtime_key, free); | ||||
} | } | ||||
struct tm * | struct tm * | ||||
gmtime(const time_t *const timep) | gmtime(const time_t *const timep) | ||||
{ | { | ||||
struct tm *p_tm; | struct tm *p_tm, *res; | ||||
if (__isthreaded != 0) { | if (__isthreaded != 0) { | ||||
_pthread_once(&gmtime_once, gmtime_key_init); | _pthread_once(&gmtime_once, gmtime_key_init); | ||||
if (gmtime_key_error != 0) { | if (gmtime_key_error != 0) { | ||||
errno = gmtime_key_error; | errno = gmtime_key_error; | ||||
return(NULL); | return(NULL); | ||||
} | } | ||||
/* | /* | ||||
* Changed to follow POSIX.1 threads standard, which | * Changed to follow POSIX.1 threads standard, which | ||||
* is what BSD currently has. | * is what BSD currently has. | ||||
*/ | */ | ||||
if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) { | if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) { | ||||
if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) | if ((p_tm = (struct tm *)malloc(sizeof(struct tm))) | ||||
== NULL) { | == NULL) { | ||||
return(NULL); | return(NULL); | ||||
} | } | ||||
_pthread_setspecific(gmtime_key, p_tm); | _pthread_setspecific(gmtime_key, p_tm); | ||||
} | } | ||||
gmtsub(timep, 0L, p_tm); | res = gmtsub(timep, 0L, p_tm); | ||||
return(p_tm); | return (res); | ||||
} | } | ||||
else { | else { | ||||
gmtsub(timep, 0L, &tm); | res = gmtsub(timep, 0L, &tm); | ||||
return(&tm); | return (res); | ||||
} | } | ||||
} | } | ||||
/* | /* | ||||
* Re-entrant version of gmtime. | * Re-entrant version of gmtime. | ||||
*/ | */ | ||||
struct tm * | struct tm * | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | #endif /* State Farm */ | ||||
while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { | while (tdays < 0 || tdays >= year_lengths[isleap(y)]) { | ||||
int newy; | int newy; | ||||
register time_t tdelta; | register time_t tdelta; | ||||
register int idelta; | register int idelta; | ||||
register int leapdays; | register int leapdays; | ||||
tdelta = tdays / DAYSPERLYEAR; | tdelta = tdays / DAYSPERLYEAR; | ||||
idelta = tdelta; | idelta = tdelta; | ||||
if (tdelta - idelta >= 1 || idelta - tdelta >= 1) | if (tdelta - idelta >= 1 || idelta - tdelta >= 1) { | ||||
errno = EOVERFLOW; | |||||
return NULL; | return NULL; | ||||
} | |||||
if (idelta == 0) | if (idelta == 0) | ||||
idelta = (tdays < 0) ? -1 : 1; | idelta = (tdays < 0) ? -1 : 1; | ||||
newy = y; | newy = y; | ||||
if (increment_overflow(&newy, idelta)) | if (increment_overflow(&newy, idelta)) { | ||||
errno = EOVERFLOW; | |||||
return NULL; | return NULL; | ||||
} | |||||
leapdays = leaps_thru_end_of(newy - 1) - | leapdays = leaps_thru_end_of(newy - 1) - | ||||
leaps_thru_end_of(y - 1); | leaps_thru_end_of(y - 1); | ||||
tdays -= ((time_t) newy - y) * DAYSPERNYEAR; | tdays -= ((time_t) newy - y) * DAYSPERNYEAR; | ||||
tdays -= leapdays; | tdays -= leapdays; | ||||
y = newy; | y = newy; | ||||
} | } | ||||
{ | { | ||||
register long seconds; | register long seconds; | ||||
Show All 11 Lines | while (rem < 0) { | ||||
rem += SECSPERDAY; | rem += SECSPERDAY; | ||||
--idays; | --idays; | ||||
} | } | ||||
while (rem >= SECSPERDAY) { | while (rem >= SECSPERDAY) { | ||||
rem -= SECSPERDAY; | rem -= SECSPERDAY; | ||||
++idays; | ++idays; | ||||
} | } | ||||
while (idays < 0) { | while (idays < 0) { | ||||
if (increment_overflow(&y, -1)) | if (increment_overflow(&y, -1)) { | ||||
errno = EOVERFLOW; | |||||
return NULL; | return NULL; | ||||
} | |||||
idays += year_lengths[isleap(y)]; | idays += year_lengths[isleap(y)]; | ||||
} | } | ||||
while (idays >= year_lengths[isleap(y)]) { | while (idays >= year_lengths[isleap(y)]) { | ||||
idays -= year_lengths[isleap(y)]; | idays -= year_lengths[isleap(y)]; | ||||
if (increment_overflow(&y, 1)) | if (increment_overflow(&y, 1)) { | ||||
errno = EOVERFLOW; | |||||
return NULL; | return NULL; | ||||
} | } | ||||
} | |||||
tmp->tm_year = y; | tmp->tm_year = y; | ||||
if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE)) | if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE)) { | ||||
errno = EOVERFLOW; | |||||
return NULL; | return NULL; | ||||
} | |||||
tmp->tm_yday = idays; | tmp->tm_yday = idays; | ||||
/* | /* | ||||
** The "extra" mods below avoid overflow problems. | ** The "extra" mods below avoid overflow problems. | ||||
*/ | */ | ||||
tmp->tm_wday = EPOCH_WDAY + | tmp->tm_wday = EPOCH_WDAY + | ||||
((y - EPOCH_YEAR) % DAYSPERWEEK) * | ((y - EPOCH_YEAR) % DAYSPERWEEK) * | ||||
(DAYSPERNYEAR % DAYSPERWEEK) + | (DAYSPERNYEAR % DAYSPERWEEK) + | ||||
leaps_thru_end_of(y - 1) - | leaps_thru_end_of(y - 1) - | ||||
▲ Show 20 Lines • Show All 536 Lines • Show Last 20 Lines |