tm->tm_yday - mdays[tmptim];

    // Determine days since Sunday (0 - 6)
    ptm->tm_wday = (static_cast<int>(*timp / _DAY_SEC) + _BASE_DOW) % 7;

    // Determine hours since midnight (0 - 23), minutes after the hour
    // (0 - 59), and seconds after the minute (0 - 59).
    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);
}



// Gets the thread-local buffer to be used by gmtime.  Returns a pointer to the
// buffer on success; returns null and sets errno on failure.
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;
}


// Converts a time_t value into a tm structure in UTC.  Returns a pointer to a
// thread-local buffer containing the tm structure on success; returns null on
// failure.
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);
}
