struct tm * __cdecl localtime ( const time_t *ptime ) { REG1 struct tm *ptm; long ltime; /* * Check for illegal time_t value */ if ( (long)*ptime < 0L ) return( NULL ); #ifdef _WIN32 __tzset(); #else /* _WIN32 */ #if defined (_M_MPPC) || defined (_M_M68K) _tzset(); #endif /* defined (_M_MPPC) || defined (_M_M68K) */ #endif /* _WIN32 */ if ( (*ptime > 3 * _DAY_SEC) && (*ptime < LONG_MAX - 3 * _DAY_SEC) ) { /* * The date does not fall within the first three, or last * three, representable days of the Epoch. Therefore, there * is no possibility of overflowing or underflowing the * time_t representation as we compensate for timezone and * Daylight Savings Time. */ ltime = (long)*ptime - _timezone; ptm = gmtime( (time_t *)<ime ); /* * Check and adjust for Daylight Saving Time. */ if ( _daylight && _isindst( ptm ) ) { ltime -= _dstbias; ptm = gmtime( (time_t *)<ime ); ptm->tm_isdst = 1; } } else { ptm = gmtime( ptime ); /* * The date falls with the first three, or last three days * of the Epoch. It is possible the time_t representation * would overflow or underflow while compensating for * timezone and Daylight Savings Time. Therefore, make the * timezone and Daylight Savings Time adjustments directly * in the tm structure. The beginning of the Epoch is * 00:00:00, 01-01-70 (UTC) and the last representable second * in the Epoch is 03:14:07, 01-19-2038 (UTC). This will be * used in the calculations below. * * First, adjust for the timezone. */ if ( _isindst(ptm) ) ltime = (long)ptm->tm_sec - (_timezone + _dstbias); else ltime = (long)ptm->tm_sec - _timezone; ptm->tm_sec = (int)(ltime % 60); if ( ptm->tm_sec < 0 ) { ptm->tm_sec += 60; ltime -= 60; } ltime = (long)ptm->tm_min + ltime/60; ptm->tm_min = (int)(ltime % 60); if ( ptm->tm_min < 0 ) { ptm->tm_min += 60; ltime -= 60; } ltime = (long)ptm->tm_hour + ltime/60; ptm->tm_hour = (int)(ltime % 24); if ( ptm->tm_hour < 0 ) { ptm->tm_hour += 24; ltime -=24; } ltime /= 24; if ( ltime > 0L ) { /* * There is no possibility of overflowing the tm_mday * and tm_yday fields since the date can be no later * than January 19. */ ptm->tm_wday = (ptm->tm_wday + ltime) % 7; ptm->tm_mday += ltime; ptm->tm_yday += ltime; } else if ( ltime < 0L ) { /* * It is possible to underflow the tm_mday and tm_yday * fields. If this happens, then adjusted date must * lie in December 1969. */ ptm->tm_wday = (ptm->tm_wday + 7 + ltime) % 7; if ( (ptm->tm_mday += ltime) <= 0 ) { ptm->tm_mday += 31; ptm->tm_yday = 364; ptm->tm_mon = 11; ptm->tm_year--; } else { ptm->tm_yday += ltime; } } } return(ptm); }
static errno_t __cdecl common_localtime_s( tm* const ptm, TimeType const* const ptime ) throw() { typedef __crt_time_time_t_traits<TimeType> time_traits; _VALIDATE_RETURN_ERRCODE(ptm != nullptr, EINVAL); memset(ptm, 0xff, sizeof(tm)); _VALIDATE_RETURN_ERRCODE(ptime != nullptr, EINVAL); // Check for illegal time_t value: _VALIDATE_RETURN_ERRCODE_NOEXC(*ptime >= 0, EINVAL); _VALIDATE_RETURN_ERRCODE_NOEXC(*ptime <= time_traits::max_time_t, EINVAL); __tzset(); int daylight = 0; long dstbias = 0; long timezone = 0; _ERRCHECK(_get_daylight(&daylight)); _ERRCHECK(_get_dstbias (&dstbias )); _ERRCHECK(_get_timezone(&timezone)); if (*ptime > 3 * _DAY_SEC && *ptime < time_traits::max_time_t - 3 * _DAY_SEC) { // The date does not fall within the first three or last three representable // days; therefore, there is no possibility of overflowing or underflowing // the time_t representation as we compensate for time zone and daylight // savings time. TimeType ltime = *ptime - timezone; errno_t status0 = time_traits::gmtime_s(ptm, <ime); if (status0 != 0) return status0; // Check and adjust for daylight savings time: if (daylight && _isindst(ptm)) { ltime -= dstbias; errno_t const status1 = time_traits::gmtime_s(ptm, <ime); if (status1 != 0) return status1; ptm->tm_isdst = 1; } } else { // The date falls within the first three or last three representable days; // therefore, it is possible that the time_t representation would overflow // or underflow while compensating for time zone and daylight savings time. // Therefore, we make the time zone and daylight savings time adjustments // directly in the tm structure. errno_t const status0 = time_traits::gmtime_s(ptm, ptime); if (status0 != 0) return status0; TimeType ltime = static_cast<TimeType>(ptm->tm_sec); // First, adjust for the time zone: if (daylight && _isindst(ptm)) { ltime -= (timezone + dstbias); ptm->tm_isdst = 1; } else { ltime -= timezone; } ptm->tm_sec = static_cast<int>(ltime % 60); if (ptm->tm_sec < 0) { ptm->tm_sec += 60; ltime -= 60; } ltime = static_cast<TimeType>(ptm->tm_min) + ltime / 60; ptm->tm_min = static_cast<int>(ltime % 60); if (ptm->tm_min < 0) { ptm->tm_min += 60; ltime -= 60; } ltime = static_cast<TimeType>(ptm->tm_hour) + ltime / 60; ptm->tm_hour = static_cast<int>(ltime % 24); if (ptm->tm_hour < 0) { ptm->tm_hour += 24; ltime -=24; } ltime /= 24; if (ltime > 0) { // There is no possibility of overflowing the tm_day and tm_yday // members because the date can be no later than January 19. ptm->tm_wday = (ptm->tm_wday + static_cast<int>(ltime)) % 7; ptm->tm_mday += static_cast<int>(ltime); ptm->tm_yday += static_cast<int>(ltime); } else if (ltime < 0) { // It is possible to underflow the tm_mday and tm_yday fields. If // this happens, then the adjusted date must lie in December 1969: ptm->tm_wday = (ptm->tm_wday + 7 + static_cast<int>(ltime)) % 7; ptm->tm_mday += static_cast<int>(ltime); if (ptm->tm_mday <= 0) { ptm->tm_mday += 31; // Per assumption #4 above, the time zone can cause the date to // underflow the epoch by more than a day. ptm->tm_yday = ptm->tm_yday + static_cast<int>(ltime) + 365; ptm->tm_mon = 11; ptm->tm_year--; } else { ptm->tm_yday += static_cast<int>(ltime); } } } return 0; }