Skip to content

Commit da776c9

Browse files
committed
Test timezone
1 parent 64db979 commit da776c9

3 files changed

Lines changed: 10 additions & 123 deletions

File tree

include/fmt/chrono.h

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,28 +1268,8 @@ class tm_writer {
12681268
write_utc_offset(tm.tm_gmtoff, ns);
12691269
}
12701270
template <typename T, FMT_ENABLE_IF(!has_member_data_tm_gmtoff<T>::value)>
1271-
void format_utc_offset_impl(const T& tm, numeric_system ns) {
1272-
#if defined(_WIN32) && defined(_UCRT)
1273-
tzset_once();
1274-
long offset = 0;
1275-
_get_timezone(&offset);
1276-
if (tm.tm_isdst) {
1277-
long dstbias = 0;
1278-
_get_dstbias(&dstbias);
1279-
offset += dstbias;
1280-
}
1281-
write_utc_offset(-offset, ns);
1282-
#else
1283-
if (ns == numeric_system::standard) return format_localized('z');
1284-
1285-
// Extract timezone offset from timezone conversion functions.
1286-
std::tm gtm = tm;
1287-
std::time_t gt = std::mktime(&gtm);
1288-
std::tm ltm = gmtime(gt);
1289-
std::time_t lt = std::mktime(&ltm);
1290-
long long offset = gt - lt;
1291-
write_utc_offset(offset, ns);
1292-
#endif
1271+
void format_utc_offset_impl(const T&, numeric_system ns) {
1272+
write_utc_offset(0, ns);
12931273
}
12941274

12951275
template <typename T, FMT_ENABLE_IF(has_member_data_tm_zone<T>::value)>

test/chrono-test.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,11 @@ auto strftime_full_utc(TimePoint tp) -> std::string {
263263
return system_strftime("%Y-%m-%d %H:%M:%S", &tm);
264264
}
265265

266-
TEST(chrono_test, system_clock_time_point) {
266+
TEST(chrono_test, sys_time) {
267+
auto time =
268+
fmt::sys_time<std::chrono::seconds>(std::chrono::seconds(290088000));
269+
EXPECT_EQ(fmt::format("{:%z}", time), "+0000");
270+
267271
auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
268272
std::chrono::system_clock::now());
269273
EXPECT_EQ(strftime_full_utc(t1), fmt::format("{:%Y-%m-%d %H:%M:%S}", t1));
@@ -303,7 +307,7 @@ TEST(chrono_test, system_clock_time_point) {
303307
}
304308

305309
// Timezone formatters tests makes sense for localtime.
306-
#if FMT_HAS_C99_STRFTIME
310+
#if FMT_HAS_C99_STRFTIME && !defined(_WIN32)
307311
spec_list = {"%z", "%Z"};
308312
#else
309313
spec_list = {"%Z"};

test/xchar-test.cc

Lines changed: 2 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -224,106 +224,9 @@ TEST(xchar_test, chrono) {
224224
EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
225225
EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25");
226226
EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
227-
}
228-
229-
std::wstring system_wcsftime(const std::wstring& format, const std::tm* timeptr,
230-
std::locale* locptr = nullptr) {
231-
auto loc = locptr ? *locptr : std::locale::classic();
232-
auto& facet = std::use_facet<std::time_put<wchar_t>>(loc);
233-
std::wostringstream os;
234-
os.imbue(loc);
235-
facet.put(os, os, L' ', timeptr, format.c_str(),
236-
format.c_str() + format.size());
237-
#ifdef _WIN32
238-
// Workaround a bug in older versions of Universal CRT.
239-
auto str = os.str();
240-
if (str == L"-0000") str = L"+0000";
241-
return str;
242-
#else
243-
return os.str();
244-
#endif
245-
}
246-
247-
TEST(chrono_test_wchar, time_point) {
248-
auto t1 = std::chrono::time_point_cast<std::chrono::seconds>(
249-
std::chrono::system_clock::now());
250-
251-
std::vector<std::wstring> spec_list = {
252-
L"%%", L"%n", L"%t", L"%Y", L"%EY", L"%y", L"%Oy", L"%Ey", L"%C",
253-
L"%EC", L"%G", L"%g", L"%b", L"%h", L"%B", L"%m", L"%Om", L"%U",
254-
L"%OU", L"%W", L"%OW", L"%V", L"%OV", L"%j", L"%d", L"%Od", L"%e",
255-
L"%Oe", L"%a", L"%A", L"%w", L"%Ow", L"%u", L"%Ou", L"%H", L"%OH",
256-
L"%I", L"%OI", L"%M", L"%OM", L"%S", L"%OS", L"%x", L"%Ex", L"%X",
257-
L"%EX", L"%D", L"%F", L"%R", L"%T", L"%p"};
258-
#ifndef _WIN32
259-
// Disabled on Windows, because these formats is not consistent among
260-
// platforms.
261-
spec_list.insert(spec_list.end(), {L"%c", L"%Ec", L"%r"});
262-
#elif !FMT_HAS_C99_STRFTIME
263-
// Only C89 conversion specifiers when using MSVCRT instead of UCRT
264-
spec_list = {L"%%", L"%Y", L"%y", L"%b", L"%B", L"%m", L"%U",
265-
L"%W", L"%j", L"%d", L"%a", L"%A", L"%w", L"%H",
266-
L"%I", L"%M", L"%S", L"%x", L"%X", L"%p"};
267-
#endif
268-
spec_list.push_back(L"%Y-%m-%d %H:%M:%S");
269227

270-
for (const auto& spec : spec_list) {
271-
auto t = std::chrono::system_clock::to_time_t(t1);
272-
auto tm = *std::gmtime(&t);
273-
274-
auto sys_output = system_wcsftime(spec, &tm);
275-
276-
auto fmt_spec = fmt::format(L"{{:{}}}", spec);
277-
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), t1));
278-
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
279-
}
280-
281-
// Timezone formatters tests makes sense for localtime.
282-
#if FMT_HAS_C99_STRFTIME
283-
spec_list = {L"%z", L"%Z"};
284-
#else
285-
spec_list = {L"%Z"};
286-
#endif
287-
for (const auto& spec : spec_list) {
288-
auto t = std::chrono::system_clock::to_time_t(t1);
289-
auto tm = *std::localtime(&t);
290-
291-
auto sys_output = system_wcsftime(spec, &tm);
292-
293-
auto fmt_spec = fmt::format(L"{{:{}}}", spec);
294-
EXPECT_EQ(sys_output, fmt::format(fmt::runtime(fmt_spec), tm));
295-
296-
if (spec == L"%z") {
297-
sys_output.insert(sys_output.end() - 2, 1, L':');
298-
EXPECT_EQ(sys_output, fmt::format(L"{:%Ez}", tm));
299-
EXPECT_EQ(sys_output, fmt::format(L"{:%Oz}", tm));
300-
}
301-
}
302-
303-
// Separate tests for UTC, since std::time_put can use local time and ignoring
304-
// the timezone in std::tm (if it presents on platform).
305-
if (fmt::detail::has_member_data_tm_zone<std::tm>::value) {
306-
auto t = std::chrono::system_clock::to_time_t(t1);
307-
auto tm = *std::gmtime(&t);
308-
309-
std::vector<std::wstring> tz_names = {L"GMT", L"UTC"};
310-
EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", t1)));
311-
EXPECT_THAT(tz_names, Contains(fmt::format(L"{:%Z}", tm)));
312-
}
313-
314-
if (fmt::detail::has_member_data_tm_gmtoff<std::tm>::value) {
315-
auto t = std::chrono::system_clock::to_time_t(t1);
316-
auto tm = *std::gmtime(&t);
317-
318-
EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", t1));
319-
EXPECT_EQ(L"+0000", fmt::format(L"{:%z}", tm));
320-
321-
EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", t1));
322-
EXPECT_EQ(L"+00:00", fmt::format(L"{:%Ez}", tm));
323-
324-
EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", t1));
325-
EXPECT_EQ(L"+00:00", fmt::format(L"{:%Oz}", tm));
326-
}
228+
auto t = fmt::sys_time<std::chrono::seconds>(std::chrono::seconds(290088000));
229+
EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t), "1979-03-12 12:00:00");
327230
}
328231

329232
TEST(xchar_test, color) {

0 commit comments

Comments
 (0)