|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| #include <corecrt_internal_time.h>
|
| #include <locale.h>
|
|
|
|
|
|
|
| _DEFINE_SET_FUNCTION(_set_daylight, int, _daylight)
|
| _DEFINE_SET_FUNCTION(_set_dstbias, long, _dstbias )
|
| _DEFINE_SET_FUNCTION(_set_timezone, long, _timezone)
|
|
|
|
|
|
|
|
|
|
|
| static wchar_t* last_wide_tz = nullptr;
|
|
|
|
|
|
|
| static int tz_api_used;
|
| static TIME_ZONE_INFORMATION tz_info;
|
|
|
| static __crt_state_management::dual_state_global<long> tzset_init_state;
|
|
|
| namespace
|
| {
|
|
|
| struct transitiondate
|
| {
|
| int yr;
|
| int yd;
|
| int ms;
|
| };
|
|
|
| enum class date_type
|
| {
|
| absolute_date,
|
| day_in_month
|
| };
|
|
|
| enum class transition_type
|
| {
|
| start_of_dst,
|
| end_of_dst
|
| };
|
|
|
| size_t const local_env_buffer_size = 256;
|
| int const milliseconds_per_day = 24 * 60 * 60 * 1000;
|
| }
|
|
|
|
|
| static transitiondate dststart = { -1, 0, 0 };
|
| static transitiondate dstend = { -1, 0, 0 };
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static wchar_t* get_tz_environment_variable(wchar_t (&local_buffer)[local_env_buffer_size]) throw()
|
| {
|
| size_t required_length;
|
| errno_t const status = _wgetenv_s(&required_length, local_buffer, local_env_buffer_size, L"TZ");
|
| if (status == 0)
|
| {
|
| return local_buffer;
|
| }
|
|
|
| if (status != ERANGE)
|
| {
|
| return nullptr;
|
| }
|
|
|
| __crt_unique_heap_ptr<wchar_t> dynamic_buffer(_malloc_crt_t(wchar_t, required_length));
|
| if (dynamic_buffer.get() == nullptr)
|
| {
|
| return nullptr;
|
| }
|
|
|
| size_t actual_length;
|
| if (_wgetenv_s(&actual_length, dynamic_buffer.get(), required_length, L"TZ") != 0)
|
| {
|
| return nullptr;
|
| }
|
|
|
| return dynamic_buffer.detach();
|
| }
|
|
|
| static void __cdecl tzset_os_copy_to_tzname(const wchar_t * const timezone_name, wchar_t * const wide_tzname, char * const narrow_tzname, unsigned int const code_page)
|
| {
|
|
|
|
|
| _ERRCHECK(wcsncpy_s(wide_tzname, _TZ_STRINGS_SIZE, timezone_name, 32));
|
|
|
|
|
|
|
| __acrt_WideCharToMultiByte(
|
| code_page,
|
| 0,
|
| timezone_name,
|
| -1,
|
| narrow_tzname,
|
| _TZ_STRINGS_SIZE,
|
| nullptr,
|
| nullptr
|
| );
|
| }
|
|
|
|
|
|
|
| static void __cdecl tzset_from_system_nolock() throw()
|
| {
|
| _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
|
| char** tzname = _tzname;
|
| wchar_t** wide_tzname = __wide_tzname();
|
| _END_SECURE_CRT_DEPRECATION_DISABLE
|
|
|
| long timezone = 0;
|
| int daylight = 0;
|
| long dstbias = 0;
|
| _ERRCHECK(_get_timezone(&timezone));
|
| _ERRCHECK(_get_daylight(&daylight));
|
| _ERRCHECK(_get_dstbias (&dstbias ));
|
|
|
|
|
| _free_crt(last_wide_tz);
|
| last_wide_tz = nullptr;
|
|
|
| if (GetTimeZoneInformation(&tz_info) != 0xFFFFFFFF)
|
| {
|
|
|
| tz_api_used = 1;
|
|
|
|
|
| timezone = tz_info.Bias * 60;
|
|
|
| if (tz_info.StandardDate.wMonth != 0)
|
| timezone += tz_info.StandardBias * 60;
|
|
|
|
|
|
|
|
|
| if (tz_info.DaylightDate.wMonth != 0 && tz_info.DaylightBias != 0)
|
| {
|
| daylight = 1;
|
| dstbias = (tz_info.DaylightBias - tz_info.StandardBias) * 60;
|
| }
|
| else
|
| {
|
| daylight = 0;
|
|
|
|
|
|
|
|
|
|
|
| dstbias = 0;
|
| }
|
|
|
| memset(wide_tzname[0], 0, _TZ_STRINGS_SIZE * sizeof(wchar_t));
|
| memset(wide_tzname[1], 0, _TZ_STRINGS_SIZE * sizeof(wchar_t));
|
| memset(tzname[0], 0, _TZ_STRINGS_SIZE);
|
| memset(tzname[1], 0, _TZ_STRINGS_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| unsigned const code_page = ___lc_codepage_func();
|
|
|
| tzset_os_copy_to_tzname(tz_info.StandardName, wide_tzname[0], tzname[0], code_page);
|
| tzset_os_copy_to_tzname(tz_info.DaylightName, wide_tzname[1], tzname[1], code_page);
|
| }
|
|
|
| _set_timezone(timezone);
|
| _set_daylight(daylight);
|
| _set_dstbias(dstbias);
|
| }
|
|
|
| static void __cdecl tzset_env_copy_to_tzname(const wchar_t * const tz_env, wchar_t * const wide_tzname, char * const narrow_tzname, rsize_t const tzname_length)
|
| {
|
| _ERRCHECK(wcsncpy_s(wide_tzname, _TZ_STRINGS_SIZE, tz_env, tzname_length));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| __acrt_WideCharToMultiByte(
|
| __acrt_get_utf8_acp_compatibility_codepage(),
|
| 0,
|
| wide_tzname,
|
| static_cast<int>(tzname_length),
|
| narrow_tzname,
|
| _TZ_STRINGS_SIZE - 1,
|
| nullptr,
|
| nullptr);
|
| }
|
|
|
| static void __cdecl tzset_from_environment_nolock(_In_z_ wchar_t* tz_env) throw()
|
| {
|
| _BEGIN_SECURE_CRT_DEPRECATION_DISABLE
|
| char** tzname = _tzname;
|
| wchar_t** wide_tzname = __wide_tzname();
|
| _END_SECURE_CRT_DEPRECATION_DISABLE
|
|
|
| long timezone = 0;
|
| int daylight = 0;
|
| _ERRCHECK(_get_timezone(&timezone));
|
| _ERRCHECK(_get_daylight(&daylight));
|
|
|
|
|
|
|
| if (last_wide_tz != nullptr && wcscmp(tz_env, last_wide_tz) == 0)
|
| {
|
| return;
|
| }
|
|
|
|
|
| auto new_wide_tz = _malloc_crt_t(wchar_t, wcslen(tz_env) + 1);
|
| if (!new_wide_tz)
|
| {
|
| return;
|
| }
|
|
|
| _free_crt(last_wide_tz);
|
| last_wide_tz = new_wide_tz.detach();
|
|
|
| _ERRCHECK(wcscpy_s(last_wide_tz, wcslen(tz_env) + 1, tz_env));
|
|
|
|
|
| memset(wide_tzname[0], 0, _TZ_STRINGS_SIZE * sizeof(wchar_t));
|
| memset(wide_tzname[1], 0, _TZ_STRINGS_SIZE * sizeof(wchar_t));
|
| memset(tzname[0], 0, _TZ_STRINGS_SIZE);
|
| memset(tzname[1], 0, _TZ_STRINGS_SIZE);
|
|
|
| rsize_t const tzname_length = 3;
|
|
|
|
|
| tzset_env_copy_to_tzname(tz_env, wide_tzname[0], tzname[0], tzname_length);
|
|
|
|
|
| for (rsize_t i = 0; i < tzname_length; ++i)
|
| {
|
| if (*tz_env)
|
| {
|
| ++tz_env;
|
| }
|
| }
|
|
|
|
|
|
|
|
|
| bool const is_negative_difference = *tz_env == L'-';
|
| if (is_negative_difference)
|
| {
|
| ++tz_env;
|
| }
|
|
|
| wchar_t * dummy;
|
| int const decimal_base = 10;
|
|
|
|
|
| timezone = wcstol(tz_env, &dummy, decimal_base) * 3600;
|
| while (*tz_env == '+' || (*tz_env >= L'0' && *tz_env <= L'9'))
|
| {
|
| ++tz_env;
|
| }
|
|
|
|
|
|
|
| if (*tz_env == L':')
|
| {
|
|
|
| timezone += wcstol(++tz_env, &dummy, decimal_base) * 60;
|
| while (*tz_env >= L'0' && *tz_env <= L'9')
|
| {
|
| ++tz_env;
|
| }
|
|
|
|
|
| if (*tz_env == L':')
|
| {
|
|
|
| timezone += wcstol(++tz_env, &dummy, decimal_base);
|
| while (*tz_env >= L'0' && *tz_env <= L'9')
|
| {
|
| ++tz_env;
|
| }
|
| }
|
| }
|
|
|
| if (is_negative_difference)
|
| {
|
| timezone = -timezone;
|
| }
|
|
|
|
|
| daylight = *tz_env ? 1 : 0;
|
|
|
| if (daylight)
|
| {
|
|
|
| tzset_env_copy_to_tzname(tz_env, wide_tzname[1], tzname[1], tzname_length);
|
| }
|
|
|
| _set_timezone(timezone);
|
| _set_daylight(daylight);
|
| }
|
|
|
| static void __cdecl tzset_nolock() throw()
|
| {
|
|
|
| tz_api_used = 0;
|
|
|
|
|
|
|
| dststart.yr = dstend.yr = -1;
|
|
|
|
|
| wchar_t local_env_buffer[local_env_buffer_size];
|
| wchar_t* const tz_env = get_tz_environment_variable(local_env_buffer);
|
|
|
|
|
|
|
| __crt_unique_heap_ptr<wchar_t> tz_env_cleanup(tz_env == local_env_buffer
|
| ? nullptr
|
| : tz_env);
|
|
|
|
|
|
|
|
|
| if (tz_env == nullptr || tz_env[0] == '\0')
|
| return tzset_from_system_nolock();
|
|
|
| return tzset_from_environment_nolock(tz_env);
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" void __cdecl _tzset()
|
| {
|
| __acrt_lock(__acrt_time_lock);
|
| __try
|
| {
|
| tzset_nolock();
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_time_lock);
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" void __cdecl __tzset()
|
| {
|
| auto const first_time = tzset_init_state.dangerous_get_state_array() + __crt_state_management::get_current_state_index();
|
|
|
| if (__crt_interlocked_read(first_time) != 0)
|
| {
|
| return;
|
| }
|
|
|
| __acrt_lock(__acrt_time_lock);
|
| __try
|
| {
|
| if (__crt_interlocked_read(first_time) != 0)
|
| {
|
| __leave;
|
| }
|
|
|
| tzset_nolock();
|
|
|
| _InterlockedIncrement(first_time);
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_time_lock);
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static void __cdecl cvtdate(
|
| transition_type const trantype,
|
| date_type const datetype,
|
| int const year,
|
| int const month,
|
| int const week,
|
| int const dayofweek,
|
| int const date,
|
| int const hour,
|
| int const min,
|
| int const sec,
|
| int const msec
|
| ) throw()
|
| {
|
| int yearday;
|
| int monthdow;
|
| long dstbias = 0;
|
|
|
| if (datetype == date_type::day_in_month)
|
| {
|
|
|
| yearday = 1 + (__crt_time_is_leap_year(year)
|
| ? _lpdays[month - 1]
|
| : _days[month - 1]);
|
|
|
|
|
| monthdow = (yearday + ((year - 70) * 365) +
|
| __crt_time_elapsed_leap_years(year) + _BASE_DOW) % 7;
|
|
|
|
|
| if (monthdow <= dayofweek)
|
| yearday += (dayofweek - monthdow) + (week - 1) * 7;
|
| else
|
| yearday += (dayofweek - monthdow) + week * 7;
|
|
|
|
|
|
|
|
|
| int const days_to_compare = __crt_time_is_leap_year(year)
|
| ? _lpdays[month]
|
| : _days[month];
|
|
|
| if (week == 5 && yearday > days_to_compare)
|
| {
|
| yearday -= 7;
|
| }
|
| }
|
| else
|
| {
|
| yearday = __crt_time_is_leap_year(year)
|
| ? _lpdays[month - 1]
|
| : _days[month - 1];
|
|
|
| yearday += date;
|
| }
|
|
|
| if (trantype == transition_type::start_of_dst)
|
| {
|
| dststart.yd = yearday;
|
| dststart.ms = msec + (1000 * (sec + 60 * (min + 60 * hour)));
|
|
|
|
|
|
|
| dststart.yr = year;
|
| }
|
| else
|
| {
|
| dstend.yd = yearday;
|
| dstend.ms = msec + (1000 * (sec + 60 * (min + 60 * hour)));
|
|
|
|
|
|
|
|
|
| _ERRCHECK(_get_dstbias(&dstbias));
|
| dstend.ms += (dstbias * 1000);
|
| if (dstend.ms < 0)
|
| {
|
| dstend.ms += milliseconds_per_day;
|
| dstend.yd--;
|
| }
|
| else if (dstend.ms >= milliseconds_per_day)
|
| {
|
| dstend.ms -= milliseconds_per_day;
|
| dstend.yd++;
|
| }
|
|
|
|
|
|
|
| dstend.yr = year;
|
| }
|
|
|
| return;
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| static int __cdecl _isindst_nolock(tm* const tb) throw()
|
| {
|
| int daylight = 0;
|
| _ERRCHECK(_get_daylight(&daylight));
|
| if (daylight == 0)
|
| return 0;
|
|
|
|
|
|
|
|
|
| if (tb->tm_year != dststart.yr || tb->tm_year != dstend.yr)
|
| {
|
| if (tz_api_used)
|
| {
|
|
|
| if (tz_info.DaylightDate.wYear == 0)
|
| {
|
| cvtdate(
|
| transition_type::start_of_dst,
|
| date_type::day_in_month,
|
| tb->tm_year,
|
| tz_info.DaylightDate.wMonth,
|
| tz_info.DaylightDate.wDay,
|
| tz_info.DaylightDate.wDayOfWeek,
|
| 0,
|
| tz_info.DaylightDate.wHour,
|
| tz_info.DaylightDate.wMinute,
|
| tz_info.DaylightDate.wSecond,
|
| tz_info.DaylightDate.wMilliseconds);
|
| }
|
| else
|
| {
|
| cvtdate(
|
| transition_type::start_of_dst,
|
| date_type::absolute_date,
|
| tb->tm_year,
|
| tz_info.DaylightDate.wMonth,
|
| 0,
|
| 0,
|
| tz_info.DaylightDate.wDay,
|
| tz_info.DaylightDate.wHour,
|
| tz_info.DaylightDate.wMinute,
|
| tz_info.DaylightDate.wSecond,
|
| tz_info.DaylightDate.wMilliseconds);
|
| }
|
|
|
|
|
| if (tz_info.StandardDate.wYear == 0)
|
| {
|
| cvtdate(
|
| transition_type::end_of_dst,
|
| date_type::day_in_month,
|
| tb->tm_year,
|
| tz_info.StandardDate.wMonth,
|
| tz_info.StandardDate.wDay,
|
| tz_info.StandardDate.wDayOfWeek,
|
| 0,
|
| tz_info.StandardDate.wHour,
|
| tz_info.StandardDate.wMinute,
|
| tz_info.StandardDate.wSecond,
|
| tz_info.StandardDate.wMilliseconds);
|
| }
|
| else
|
| {
|
| cvtdate(
|
| transition_type::end_of_dst,
|
| date_type::absolute_date,
|
| tb->tm_year,
|
| tz_info.StandardDate.wMonth,
|
| 0,
|
| 0,
|
| tz_info.StandardDate.wDay,
|
| tz_info.StandardDate.wHour,
|
| tz_info.StandardDate.wMinute,
|
| tz_info.StandardDate.wSecond,
|
| tz_info.StandardDate.wMilliseconds);
|
| }
|
| }
|
| else
|
| {
|
|
|
|
|
| int startmonth = 3;
|
| int startweek = 2;
|
| int endmonth = 11;
|
| int endweek = 1;
|
|
|
|
|
| if (107 > tb->tm_year)
|
| {
|
| startmonth = 4;
|
| startweek = 1;
|
| endmonth = 10;
|
| endweek = 5;
|
| }
|
|
|
| cvtdate(
|
| transition_type::start_of_dst,
|
| date_type::day_in_month,
|
| tb->tm_year,
|
| startmonth,
|
| startweek,
|
| 0,
|
| 0,
|
| 2,
|
| 0,
|
| 0,
|
| 0);
|
|
|
| cvtdate(
|
| transition_type::end_of_dst,
|
| date_type::day_in_month,
|
| tb->tm_year,
|
| endmonth,
|
| endweek,
|
| 0,
|
| 0,
|
| 2,
|
| 0,
|
| 0,
|
| 0);
|
| }
|
| }
|
|
|
|
|
| if (dststart.yd < dstend.yd)
|
| {
|
|
|
| if (tb->tm_yday < dststart.yd || tb->tm_yday > dstend.yd)
|
| return 0;
|
|
|
| if (tb->tm_yday > dststart.yd && tb->tm_yday < dstend.yd)
|
| return 1;
|
| }
|
| else
|
| {
|
|
|
| if (tb->tm_yday < dstend.yd || tb->tm_yday > dststart.yd)
|
| return 1;
|
|
|
| if (tb->tm_yday > dstend.yd && tb->tm_yday < dststart.yd)
|
| return 0;
|
| }
|
|
|
| long const ms = 1000 * (tb->tm_sec + 60 * tb->tm_min + 3600 * tb->tm_hour);
|
|
|
| if (tb->tm_yday == dststart.yd)
|
| {
|
|
|
| return ms >= dststart.ms ? 1 : 0;
|
| }
|
| else
|
| {
|
| return ms < dstend.ms ? 1 : 0;
|
| }
|
| }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| extern "C" int __cdecl _isindst(tm* const tb)
|
| {
|
| int retval = 0;
|
|
|
| __acrt_lock(__acrt_time_lock);
|
| __try
|
| {
|
| retval = _isindst_nolock(tb);
|
| }
|
| __finally
|
| {
|
| __acrt_unlock(__acrt_time_lock);
|
| }
|
|
|
| return retval;
|
| }
|
|
|