|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_time.h>
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl compute_year(__time32_t& caltim, bool& is_leap_year) throw()
|
| {
|
|
|
|
|
|
|
| int tmptim = static_cast<int>(caltim / _FOUR_YEAR_SEC);
|
| caltim -= static_cast<__time32_t>(tmptim) * _FOUR_YEAR_SEC;
|
|
|
|
|
| tmptim = (tmptim * 4) + 70;
|
|
|
| if (caltim >= _YEAR_SEC)
|
| {
|
| tmptim++;
|
| caltim -= _YEAR_SEC;
|
|
|
| if (caltim >= _YEAR_SEC)
|
| {
|
| tmptim++;
|
| caltim -= _YEAR_SEC;
|
|
|
|
|
|
|
| if (caltim >= (_YEAR_SEC + _DAY_SEC))
|
| {
|
| tmptim++;
|
| caltim -= (_YEAR_SEC + _DAY_SEC);
|
| }
|
| else
|
| {
|
|
|
| is_leap_year = true;
|
| }
|
| }
|
| }
|
|
|
| return tmptim;
|
| }
|
|
|
| static int __cdecl compute_year(__time64_t& caltim, bool& is_leap_year) throw()
|
| {
|
|
|
| int tmptim = static_cast<int>(caltim / _YEAR_SEC) + 70;
|
| caltim -= static_cast<__time64_t>(tmptim - 70) * _YEAR_SEC;
|
|
|
|
|
| caltim -= static_cast<__time64_t>(__crt_time_elapsed_leap_years(tmptim)) * _DAY_SEC;
|
|
|
|
|
|
|
| if (caltim < 0)
|
| {
|
| caltim += static_cast<__time64_t>(_YEAR_SEC);
|
| tmptim--;
|
| if (__crt_time_is_leap_year(tmptim))
|
| {
|
| caltim += _DAY_SEC;
|
| is_leap_year = true;
|
| }
|
| }
|
| else if (__crt_time_is_leap_year(tmptim))
|
| {
|
| is_leap_year = true;
|
| }
|
|
|
| return tmptim;
|
| }
|
|
|
|
|
|
|
|
|
| template <typename TimeType>
|
| static errno_t __cdecl common_gmtime_s(tm* const ptm, TimeType const* const timp) throw()
|
| {
|
| typedef __crt_time_time_t_traits<__time64_t> time_traits;
|
|
|
| _VALIDATE_RETURN_ERRCODE(ptm != nullptr, EINVAL)
|
| memset(ptm, 0xff, sizeof(tm));
|
|
|
| _VALIDATE_RETURN_ERRCODE(timp != nullptr, EINVAL);
|
| TimeType caltim = *timp;
|
|
|
| _VALIDATE_RETURN_ERRCODE_NOEXC(caltim >= _MIN_LOCAL_TIME, EINVAL)
|
|
|
|
|
|
|
| _VALIDATE_RETURN_ERRCODE_NOEXC(caltim <= time_traits::max_time_t + _MAX_LOCAL_TIME, EINVAL)
|
|
|
|
|
|
|
| bool is_leap_year = false;
|
| ptm->tm_year = compute_year(caltim, is_leap_year);
|
|
|
|
|
|
|
| ptm->tm_yday = static_cast<int>(caltim / _DAY_SEC);
|
| caltim -= static_cast<TimeType>(ptm->tm_yday) * _DAY_SEC;
|
|
|
|
|
| int const* const mdays = is_leap_year ? _lpdays : _days;
|
|
|
| int tmptim = 0;
|
| for (tmptim = 1 ; mdays[tmptim] < ptm->tm_yday ; tmptim++)
|
| {
|
| }
|
|
|
| ptm->tm_mon = --tmptim;
|
|
|
| ptm->tm_mday = ptm->tm_yday - mdays[tmptim];
|
|
|
|
|
| ptm->tm_wday = (static_cast<int>(*timp / _DAY_SEC) + _BASE_DOW) % 7;
|
|
|
|
|
|
|
| ptm->tm_hour = static_cast<int>(caltim / 3600);
|
| caltim -= static_cast<TimeType>(ptm->tm_hour) * 3600L;
|
|
|
| ptm->tm_min = static_cast<int>(caltim / 60);
|
| ptm->tm_sec = static_cast<int>(caltim - (ptm->tm_min) * 60);
|
|
|
| ptm->tm_isdst = 0;
|
| return 0;
|
| }
|
|
|
| extern "C" errno_t __cdecl _gmtime32_s(tm* const result, __time32_t const* const time_value)
|
| {
|
| return common_gmtime_s(result, time_value);
|
| }
|
|
|
| extern "C" errno_t __cdecl _gmtime64_s(tm* const result, __time64_t const* const time_value)
|
| {
|
| return common_gmtime_s(result, time_value);
|
| }
|
|
|
|
|
|
|
|
|
|
|
| extern "C" tm* __cdecl __getgmtimebuf()
|
| {
|
| __acrt_ptd* const ptd = __acrt_getptd_noexit();
|
| if (ptd == nullptr)
|
| {
|
| errno = ENOMEM;
|
| return nullptr;
|
| }
|
|
|
| if (ptd->_gmtime_buffer != nullptr)
|
| {
|
| return ptd->_gmtime_buffer;
|
| }
|
|
|
| ptd->_gmtime_buffer = _malloc_crt_t(tm, 1).detach();
|
| if (ptd->_gmtime_buffer == nullptr)
|
| {
|
| errno = ENOMEM;
|
| return nullptr;
|
| }
|
|
|
| return ptd->_gmtime_buffer;
|
| }
|
|
|
|
|
|
|
|
|
|
|
| template <typename TimeType>
|
| _Success_(return != 0)
|
| static tm* __cdecl common_gmtime(TimeType const* const time_value) throw()
|
| {
|
| tm* const ptm = __getgmtimebuf();
|
| if (ptm == nullptr)
|
| return nullptr;
|
|
|
| if (common_gmtime_s(ptm, time_value) != 0)
|
| return nullptr;
|
|
|
| return ptm;
|
| }
|
|
|
| extern "C" tm* __cdecl _gmtime32(__time32_t const* const time_value)
|
| {
|
| return common_gmtime(time_value);
|
| }
|
|
|
| extern "C" tm* __cdecl _gmtime64(__time64_t const* const time_value)
|
| {
|
| return common_gmtime(time_value);
|
| }
|
|
|