|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_time.h>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename TimeType>
|
| 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);
|
|
|
|
|
| _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)
|
| {
|
|
|
|
|
|
|
|
|
| TimeType ltime = *ptime - timezone;
|
|
|
| errno_t status0 = time_traits::gmtime_s(ptm, <ime);
|
| if (status0 != 0)
|
| return status0;
|
|
|
|
|
| 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
|
| {
|
|
|
|
|
|
|
|
|
|
|
| errno_t const status0 = time_traits::gmtime_s(ptm, ptime);
|
| if (status0 != 0)
|
| return status0;
|
|
|
| TimeType ltime = static_cast<TimeType>(ptm->tm_sec);
|
|
|
|
|
| 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)
|
| {
|
|
|
|
|
| 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)
|
| {
|
|
|
|
|
| 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;
|
|
|
|
|
|
|
| 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;
|
| }
|
|
|
| extern "C" errno_t __cdecl _localtime32_s(
|
| tm* const ptm,
|
| __time32_t const* const ptime
|
| )
|
| {
|
| return common_localtime_s(ptm, ptime);
|
| }
|
|
|
| extern "C" errno_t __cdecl _localtime64_s(
|
| tm* const ptm,
|
| __time64_t const* const ptime
|
| )
|
| {
|
| return common_localtime_s(ptm, ptime);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| template <typename TimeType>
|
| _Success_(return != 0)
|
| static tm* __cdecl common_localtime(TimeType const* const ptime) throw()
|
| {
|
| typedef __crt_time_time_t_traits<TimeType> time_traits;
|
|
|
| tm* const ptm = __getgmtimebuf();
|
| if (ptm == nullptr)
|
| return nullptr;
|
|
|
| errno_t const status = time_traits::localtime_s(ptm, ptime);
|
| if (status != 0)
|
| return nullptr;
|
|
|
| return ptm;
|
| }
|
|
|
| extern "C" tm* __cdecl _localtime32(__time32_t const* const ptime)
|
| {
|
| return common_localtime(ptime);
|
| }
|
|
|
| extern "C" tm* __cdecl _localtime64(__time64_t const* const ptime)
|
| {
|
| return common_localtime(ptime);
|
| }
|
|
|