2018-12-07 10:19:44 -08:00
|
|
|
// Formatting library for C++ - time formatting tests
|
|
|
|
//
|
|
|
|
// Copyright (c) 2012 - present, Victor Zverovich
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// For the license information refer to format.h.
|
|
|
|
|
|
|
|
#include "fmt/chrono.h"
|
|
|
|
|
2022-12-09 18:24:25 +05:00
|
|
|
#include <algorithm>
|
2021-10-16 22:17:11 +05:00
|
|
|
#include <ctime>
|
2021-10-09 16:27:02 +05:00
|
|
|
#include <vector>
|
|
|
|
|
2021-04-30 14:21:49 -07:00
|
|
|
#include "gtest-extra.h" // EXPECT_THROW_MSG
|
2021-05-24 07:23:56 -07:00
|
|
|
#include "util.h" // get_locale
|
2020-05-07 15:59:46 -07:00
|
|
|
|
2021-05-18 19:38:52 -07:00
|
|
|
using fmt::runtime;
|
2025-04-12 09:00:43 -07:00
|
|
|
using fmt::sys_time;
|
2021-05-24 07:23:56 -07:00
|
|
|
using testing::Contains;
|
|
|
|
|
2022-12-25 22:24:07 +05:00
|
|
|
#if defined(__MINGW32__) && !defined(_UCRT)
|
|
|
|
// Only C89 conversion specifiers when using MSVCRT instead of UCRT
|
|
|
|
# define FMT_HAS_C99_STRFTIME 0
|
|
|
|
#else
|
|
|
|
# define FMT_HAS_C99_STRFTIME 1
|
|
|
|
#endif
|
|
|
|
|
2023-12-03 19:29:58 +04:00
|
|
|
#if defined(__cpp_lib_chrono) && __cpp_lib_chrono >= 201907L
|
|
|
|
using days = std::chrono::days;
|
|
|
|
#else
|
|
|
|
using days = std::chrono::duration<std::chrono::hours::rep, std::ratio<86400>>;
|
|
|
|
#endif
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
auto make_tm() -> std::tm {
|
2018-12-07 10:19:44 -08:00
|
|
|
auto time = std::tm();
|
|
|
|
time.tm_mday = 1;
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
auto make_hour(int h) -> std::tm {
|
2018-12-07 10:19:44 -08:00
|
|
|
auto time = make_tm();
|
|
|
|
time.tm_hour = h;
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
auto make_minute(int m) -> std::tm {
|
2018-12-07 10:19:44 -08:00
|
|
|
auto time = make_tm();
|
|
|
|
time.tm_min = m;
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
auto make_second(int s) -> std::tm {
|
2018-12-07 10:19:44 -08:00
|
|
|
auto time = make_tm();
|
|
|
|
time.tm_sec = s;
|
|
|
|
return time;
|
|
|
|
}
|
|
|
|
|
2021-10-14 22:06:49 +05:00
|
|
|
std::string system_strftime(const std::string& format, const std::tm* timeptr,
|
2021-10-30 20:25:45 +05:00
|
|
|
std::locale* locptr = nullptr) {
|
|
|
|
auto loc = locptr ? *locptr : std::locale::classic();
|
|
|
|
auto& facet = std::use_facet<std::time_put<char>>(loc);
|
|
|
|
std::ostringstream os;
|
|
|
|
os.imbue(loc);
|
|
|
|
facet.put(os, os, ' ', timeptr, format.c_str(),
|
|
|
|
format.c_str() + format.size());
|
2021-11-13 14:31:57 +05:00
|
|
|
#ifdef _WIN32
|
|
|
|
// Workaround a bug in older versions of Universal CRT.
|
|
|
|
auto str = os.str();
|
|
|
|
if (str == "-0000") str = "+0000";
|
|
|
|
return str;
|
|
|
|
#else
|
2021-10-30 20:25:45 +05:00
|
|
|
return os.str();
|
2021-11-13 14:31:57 +05:00
|
|
|
#endif
|
2021-10-14 22:06:49 +05:00
|
|
|
}
|
|
|
|
|
2021-10-16 22:17:11 +05:00
|
|
|
FMT_CONSTEXPR std::tm make_tm(int year, int mon, int mday, int hour, int min,
|
|
|
|
int sec) {
|
|
|
|
auto tm = std::tm();
|
|
|
|
tm.tm_sec = sec;
|
|
|
|
tm.tm_min = min;
|
|
|
|
tm.tm_hour = hour;
|
|
|
|
tm.tm_mday = mday;
|
|
|
|
tm.tm_mon = mon - 1;
|
|
|
|
tm.tm_year = year - 1900;
|
|
|
|
return tm;
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_tm) {
|
|
|
|
auto tm = std::tm();
|
2019-03-14 18:10:56 -07:00
|
|
|
tm.tm_year = 116;
|
|
|
|
tm.tm_mon = 3;
|
|
|
|
tm.tm_mday = 25;
|
2021-04-29 15:25:02 -07:00
|
|
|
tm.tm_hour = 11;
|
|
|
|
tm.tm_min = 22;
|
|
|
|
tm.tm_sec = 33;
|
|
|
|
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
|
|
|
|
"The date is 2016-04-25 11:22:33.");
|
2021-10-09 16:27:02 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "2016");
|
2021-10-09 23:17:51 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C}", tm), "20");
|
2021-10-10 13:50:43 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
|
2021-10-09 16:27:02 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%e}", tm), "25");
|
|
|
|
EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/16");
|
2021-09-10 07:33:45 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25");
|
2021-09-13 11:42:26 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
|
2021-10-10 19:52:02 +05:00
|
|
|
|
2021-10-10 21:08:04 +05:00
|
|
|
// Short year
|
|
|
|
tm.tm_year = 999 - 1900;
|
2021-10-14 17:07:33 +05:00
|
|
|
tm.tm_mon = 0; // for %G
|
|
|
|
tm.tm_mday = 2; // for %G
|
|
|
|
tm.tm_wday = 3; // for %G
|
|
|
|
tm.tm_yday = 1; // for %G
|
2021-10-10 21:08:04 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "0999");
|
2021-10-13 20:26:11 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), "0999");
|
2021-10-14 17:07:33 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%G}", tm), "0999");
|
2021-10-10 21:08:04 +05:00
|
|
|
|
2021-10-13 20:26:11 +05:00
|
|
|
tm.tm_year = 27 - 1900;
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "0027");
|
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), "0027");
|
|
|
|
|
2021-10-28 01:29:07 +05:00
|
|
|
// Overflow year
|
|
|
|
tm.tm_year = 2147483647;
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "2147485547");
|
|
|
|
|
|
|
|
tm.tm_year = -2147483648;
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "-2147481748");
|
|
|
|
|
2021-10-10 19:52:02 +05:00
|
|
|
// for week on the year
|
|
|
|
// https://www.cl.cam.ac.uk/~mgk25/iso-time.html
|
2021-10-16 22:17:11 +05:00
|
|
|
std::vector<std::tm> tm_list = {
|
|
|
|
make_tm(1975, 12, 29, 12, 14, 16), // W01
|
|
|
|
make_tm(1977, 1, 2, 12, 14, 16), // W53
|
|
|
|
make_tm(1999, 12, 27, 12, 14, 16), // W52
|
|
|
|
make_tm(1999, 12, 31, 12, 14, 16), // W52
|
|
|
|
make_tm(2000, 1, 1, 12, 14, 16), // W52
|
|
|
|
make_tm(2000, 1, 2, 12, 14, 16), // W52
|
|
|
|
make_tm(2000, 1, 3, 12, 14, 16) // W1
|
2021-10-10 19:52:02 +05:00
|
|
|
};
|
2022-08-08 22:35:51 +02:00
|
|
|
|
2022-12-25 22:24:07 +05:00
|
|
|
#if !FMT_HAS_C99_STRFTIME
|
2022-08-08 22:35:51 +02:00
|
|
|
GTEST_SKIP() << "Skip the rest of this test because it relies on strftime() "
|
|
|
|
"conforming to C99, but on this platform, MINGW + MSVCRT, "
|
|
|
|
"the function conforms only to C89.";
|
|
|
|
#endif
|
|
|
|
|
2021-10-14 17:50:16 +05:00
|
|
|
const std::string iso_week_spec = "%Y-%m-%d: %G %g %V";
|
2021-10-16 22:17:11 +05:00
|
|
|
for (auto ctm : tm_list) {
|
|
|
|
// Calculate tm_yday, tm_wday, etc.
|
|
|
|
std::time_t t = std::mktime(&ctm);
|
2021-10-10 19:52:02 +05:00
|
|
|
tm = *std::localtime(&t);
|
|
|
|
|
2021-10-16 22:17:11 +05:00
|
|
|
auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec);
|
2021-10-14 22:06:49 +05:00
|
|
|
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
|
|
|
|
fmt::format(fmt::runtime(fmt_spec), tm));
|
2021-10-14 17:50:16 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Every day from 1970-01-01
|
|
|
|
std::time_t time_now = std::time(nullptr);
|
|
|
|
for (std::time_t t = 6 * 3600; t < time_now; t += 86400) {
|
|
|
|
tm = *std::localtime(&t);
|
|
|
|
|
2021-10-16 22:17:11 +05:00
|
|
|
auto fmt_spec = fmt::format("{{:{}}}", iso_week_spec);
|
2021-10-14 22:06:49 +05:00
|
|
|
EXPECT_EQ(system_strftime(iso_week_spec, &tm),
|
|
|
|
fmt::format(fmt::runtime(fmt_spec), tm));
|
2021-10-10 19:52:02 +05:00
|
|
|
}
|
2019-03-14 18:10:56 -07:00
|
|
|
}
|
|
|
|
|
2021-10-09 16:47:13 +05:00
|
|
|
// MSVC:
|
|
|
|
// minkernel\crts\ucrt\src\appcrt\time\wcsftime.cpp(971) : Assertion failed:
|
|
|
|
// timeptr->tm_year >= -1900 && timeptr->tm_year <= 8099
|
|
|
|
#ifndef _WIN32
|
2021-10-09 16:27:02 +05:00
|
|
|
TEST(chrono_test, format_tm_future) {
|
|
|
|
auto tm = std::tm();
|
|
|
|
tm.tm_year = 10445; // 10000+ years
|
|
|
|
tm.tm_mon = 3;
|
|
|
|
tm.tm_mday = 25;
|
|
|
|
tm.tm_hour = 11;
|
|
|
|
tm.tm_min = 22;
|
|
|
|
tm.tm_sec = 33;
|
|
|
|
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
|
|
|
|
"The date is 12345-04-25 11:22:33.");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "12345");
|
2021-10-09 23:17:51 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C}", tm), "123");
|
2021-10-10 13:50:43 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
|
2021-10-09 16:27:02 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/45");
|
|
|
|
EXPECT_EQ(fmt::format("{:%F}", tm), "12345-04-25");
|
|
|
|
EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(chrono_test, format_tm_past) {
|
|
|
|
auto tm = std::tm();
|
|
|
|
tm.tm_year = -2001;
|
|
|
|
tm.tm_mon = 3;
|
|
|
|
tm.tm_mday = 25;
|
|
|
|
tm.tm_hour = 11;
|
|
|
|
tm.tm_min = 22;
|
|
|
|
tm.tm_sec = 33;
|
|
|
|
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
|
|
|
|
"The date is -101-04-25 11:22:33.");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "-101");
|
2021-10-09 16:47:13 +05:00
|
|
|
|
2021-10-10 13:50:43 +05:00
|
|
|
// macOS %C - "-1"
|
|
|
|
// Linux %C - "-2"
|
|
|
|
// fmt %C - "-1"
|
2021-10-10 00:23:47 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C}", tm), "-1");
|
2021-10-10 13:50:43 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
|
2021-10-10 00:23:47 +05:00
|
|
|
|
2021-10-10 13:50:43 +05:00
|
|
|
// macOS %D - "04/25/01" (%y)
|
|
|
|
// Linux %D - "04/25/99" (%y)
|
|
|
|
// fmt %D - "04/25/01" (%y)
|
2021-10-10 00:23:47 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%D}", tm), "04/25/01");
|
2021-10-09 16:47:13 +05:00
|
|
|
|
2021-10-09 16:27:02 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%F}", tm), "-101-04-25");
|
|
|
|
EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
|
2021-10-16 22:17:11 +05:00
|
|
|
|
|
|
|
tm.tm_year = -1901; // -1
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "-001");
|
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
|
|
|
|
|
|
|
|
tm.tm_year = -1911; // -11
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", tm), "-011");
|
|
|
|
EXPECT_EQ(fmt::format("{:%C%y}", tm), fmt::format("{:%Y}", tm));
|
2021-10-09 16:27:02 +05:00
|
|
|
}
|
2021-10-09 16:47:13 +05:00
|
|
|
#endif
|
2021-10-09 16:27:02 +05:00
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, grow_buffer) {
|
|
|
|
auto s = std::string("{:");
|
2019-03-14 18:10:56 -07:00
|
|
|
for (int i = 0; i < 30; ++i) s += "%c";
|
|
|
|
s += "}\n";
|
2021-04-29 15:25:02 -07:00
|
|
|
auto t = std::time(nullptr);
|
2021-11-24 17:09:41 -05:00
|
|
|
(void)fmt::format(fmt::runtime(s), *std::localtime(&t));
|
2019-03-14 18:10:56 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_to_empty_container) {
|
2019-03-14 18:10:56 -07:00
|
|
|
auto time = std::tm();
|
|
|
|
time.tm_sec = 42;
|
2021-04-29 15:25:02 -07:00
|
|
|
auto s = std::string();
|
2019-03-14 18:10:56 -07:00
|
|
|
fmt::format_to(std::back_inserter(s), "{:%S}", time);
|
|
|
|
EXPECT_EQ(s, "42");
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, gmtime) {
|
|
|
|
auto t = std::time(nullptr);
|
2025-04-12 09:00:43 -07:00
|
|
|
auto expected = *std::gmtime(&t);
|
|
|
|
auto actual = fmt::gmtime(t);
|
|
|
|
EXPECT_EQ(actual.tm_sec, expected.tm_sec);
|
|
|
|
EXPECT_EQ(actual.tm_min, expected.tm_min);
|
|
|
|
EXPECT_EQ(actual.tm_hour, expected.tm_hour);
|
|
|
|
EXPECT_EQ(actual.tm_mday, expected.tm_mday);
|
|
|
|
EXPECT_EQ(actual.tm_mon, expected.tm_mon);
|
|
|
|
EXPECT_EQ(actual.tm_year, expected.tm_year);
|
|
|
|
EXPECT_EQ(actual.tm_wday, expected.tm_wday);
|
|
|
|
EXPECT_EQ(actual.tm_yday, expected.tm_yday);
|
|
|
|
EXPECT_EQ(actual.tm_isdst, expected.tm_isdst);
|
2021-04-02 11:17:14 -07:00
|
|
|
}
|
|
|
|
|
2025-04-12 08:37:15 -07:00
|
|
|
template <typename Time> void test_time(Time time) {
|
2025-03-23 12:07:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{}", time), "1979-03-12 12:00:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:}", time), "1979-03-12 12:00:00");
|
2022-12-16 16:23:52 +00:00
|
|
|
|
2025-03-29 09:31:11 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%%}", time), "%");
|
|
|
|
EXPECT_EQ(fmt::format("{:%n}", time), "\n");
|
|
|
|
EXPECT_EQ(fmt::format("{:%t}", time), "\t");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", time), "1979");
|
|
|
|
EXPECT_EQ(fmt::format("{:%EY}", time), "1979");
|
|
|
|
EXPECT_EQ(fmt::format("{:%y}", time), "79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Oy}", time), "79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ey}", time), "79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%C}", time), "19");
|
|
|
|
EXPECT_EQ(fmt::format("{:%EC}", time), "19");
|
|
|
|
EXPECT_EQ(fmt::format("{:%G}", time), "1979");
|
|
|
|
EXPECT_EQ(fmt::format("{:%g}", time), "79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%b}", time), "Mar");
|
|
|
|
EXPECT_EQ(fmt::format("{:%h}", time), "Mar");
|
|
|
|
EXPECT_EQ(fmt::format("{:%B}", time), "March");
|
|
|
|
EXPECT_EQ(fmt::format("{:%m}", time), "03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Om}", time), "03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%U}", time), "10");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OU}", time), "10");
|
|
|
|
EXPECT_EQ(fmt::format("{:%W}", time), "11");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OW}", time), "11");
|
|
|
|
EXPECT_EQ(fmt::format("{:%V}", time), "11");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OV}", time), "11");
|
|
|
|
EXPECT_EQ(fmt::format("{:%j}", time), "071");
|
|
|
|
EXPECT_EQ(fmt::format("{:%d}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Od}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%e}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Oe}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%a}", time), "Mon");
|
|
|
|
EXPECT_EQ(fmt::format("{:%A}", time), "Monday");
|
|
|
|
EXPECT_EQ(fmt::format("{:%w}", time), "1");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ow}", time), "1");
|
|
|
|
EXPECT_EQ(fmt::format("{:%u}", time), "1");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ou}", time), "1");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OH}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OI}", time), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M}", time), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OM}", time), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", time), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%OS}", time), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%x}", time), "03/12/79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ex}", time), "03/12/79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%X}", time), "12:00:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%EX}", time), "12:00:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%D}", time), "03/12/79");
|
|
|
|
EXPECT_EQ(fmt::format("{:%F}", time), "1979-03-12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%R}", time), "12:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%T}", time), "12:00:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%p}", time), "PM");
|
|
|
|
EXPECT_EQ(fmt::format("{:%c}", time), "Mon Mar 12 12:00:00 1979");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ec}", time), "Mon Mar 12 12:00:00 1979");
|
|
|
|
EXPECT_EQ(fmt::format("{:%r}", time), "12:00:00 PM");
|
2022-12-16 16:23:52 +00:00
|
|
|
|
2025-03-29 09:31:11 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", time), "1979-03-12 12:00:00");
|
2025-04-12 08:24:49 -07:00
|
|
|
}
|
2022-12-16 16:23:52 +00:00
|
|
|
|
2025-04-12 08:24:49 -07:00
|
|
|
TEST(chrono_test, sys_time) {
|
|
|
|
auto time =
|
|
|
|
fmt::sys_time<std::chrono::seconds>(std::chrono::seconds(290088000));
|
|
|
|
test_time(time);
|
|
|
|
EXPECT_EQ(fmt::format("{:%z}", time), "+0000");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Ez}", time), "+00:00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Oz}", time), "+00:00");
|
2025-04-12 08:37:15 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%Z}", time), "UTC");
|
2025-04-12 08:24:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(chrono_test, local_time) {
|
|
|
|
auto time =
|
|
|
|
fmt::local_time<std::chrono::seconds>(std::chrono::seconds(290088000));
|
|
|
|
test_time(time);
|
2025-03-29 08:46:52 -07:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%z}"), time),
|
|
|
|
fmt::format_error, "no timezone");
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%Z}"), time),
|
|
|
|
fmt::format_error, "no timezone");
|
2025-03-23 11:42:13 -07:00
|
|
|
}
|
2022-12-16 16:23:52 +00:00
|
|
|
|
2025-04-13 09:17:48 -07:00
|
|
|
template <typename T, FMT_ENABLE_IF(fmt::detail::has_tm_gmtoff<T>::value)>
|
2025-04-13 08:52:26 -07:00
|
|
|
bool set_tm_gmtoff(T& time, long offset) {
|
2025-04-12 10:14:57 -07:00
|
|
|
time.tm_gmtoff = offset;
|
|
|
|
return true;
|
|
|
|
}
|
2025-04-13 09:17:48 -07:00
|
|
|
template <typename T, FMT_ENABLE_IF(!fmt::detail::has_tm_gmtoff<T>::value)>
|
2025-04-13 08:52:26 -07:00
|
|
|
bool set_tm_gmtoff(T&, long) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2025-04-12 09:00:43 -07:00
|
|
|
TEST(chrono_test, tm) {
|
|
|
|
auto time = fmt::gmtime(290088000);
|
|
|
|
test_time(time);
|
2025-04-13 08:52:26 -07:00
|
|
|
if (set_tm_gmtoff(time, -28800)) {
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format(fmt::runtime("{:%z}"), time), "-0800");
|
|
|
|
EXPECT_EQ(fmt::format(fmt::runtime("{:%Ez}"), time), "-08:00");
|
|
|
|
EXPECT_EQ(fmt::format(fmt::runtime("{:%Oz}"), time), "-08:00");
|
|
|
|
} else {
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%z}"), time),
|
|
|
|
fmt::format_error, "no timezone");
|
2025-04-13 08:52:26 -07:00
|
|
|
}
|
|
|
|
char tz[] = "EET";
|
2025-04-13 09:17:48 -07:00
|
|
|
if (fmt::detail::set_tm_zone(time, tz)) {
|
2025-04-13 08:52:26 -07:00
|
|
|
EXPECT_EQ(fmt::format(fmt::runtime("{:%Z}"), time), "EET");
|
|
|
|
} else {
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(fmt::runtime("{:%Z}"), time),
|
|
|
|
fmt::format_error, "no timezone");
|
|
|
|
}
|
2025-04-12 09:00:43 -07:00
|
|
|
}
|
|
|
|
|
2025-03-23 09:49:23 -07:00
|
|
|
TEST(chrono_test, daylight_savings_time_end) {
|
|
|
|
// 2024-10-27 03:05 as the number of seconds since epoch in Europe/Kyiv time.
|
|
|
|
// It is slightly after the DST end and passing it to to_sys will result in
|
|
|
|
// an ambiguous time error:
|
|
|
|
// 2024-10-27 03:05:00 is ambiguous. It could be
|
|
|
|
// 2024-10-27 03:05:00 EEST == 2024-10-27 00:05:00 UTC or
|
|
|
|
// 2024-10-27 03:05:00 EET == 2024-10-27 01:05:00 UTC
|
|
|
|
auto t =
|
|
|
|
fmt::local_time<std::chrono::seconds>(std::chrono::seconds(1729998300));
|
|
|
|
EXPECT_EQ(fmt::format("{}", t), "2024-10-27 03:05:00");
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_default) {
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::seconds(42)), "42s");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::atto>(42)),
|
|
|
|
"42as");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::femto>(42)),
|
|
|
|
"42fs");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::pico>(42)),
|
|
|
|
"42ps");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::nanoseconds(42)), "42ns");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::microseconds(42)), "42µs");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::milliseconds(42)), "42ms");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::centi>(42)),
|
|
|
|
"42cs");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::deci>(42)),
|
|
|
|
"42ds");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::seconds(42)), "42s");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::deca>(42)),
|
|
|
|
"42das");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::hecto>(42)),
|
|
|
|
"42hs");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::kilo>(42)),
|
|
|
|
"42ks");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::mega>(42)),
|
|
|
|
"42Ms");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::giga>(42)),
|
|
|
|
"42Gs");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::tera>(42)),
|
|
|
|
"42Ts");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::peta>(42)),
|
|
|
|
"42Ps");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<int, std::exa>(42)),
|
|
|
|
"42Es");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::minutes(42)), "42min");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::hours(42)), "42h");
|
|
|
|
EXPECT_EQ(fmt::format("{}", days(42)), "42d");
|
2019-01-12 18:27:38 -08:00
|
|
|
EXPECT_EQ(
|
2023-12-29 16:34:42 -08:00
|
|
|
fmt::format("{}", std::chrono::duration<int, std::ratio<15, 1>>(42)),
|
|
|
|
"42[15]s");
|
2019-01-12 18:27:38 -08:00
|
|
|
EXPECT_EQ(
|
2023-12-29 16:34:42 -08:00
|
|
|
fmt::format("{}", std::chrono::duration<int, std::ratio<15, 4>>(42)),
|
|
|
|
"42[15/4]s");
|
2018-12-09 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
2023-01-09 13:25:31 -06:00
|
|
|
TEST(chrono_test, duration_align) {
|
2018-12-16 07:00:57 -08:00
|
|
|
auto s = std::chrono::seconds(42);
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:5}", s), "42s ");
|
|
|
|
EXPECT_EQ(fmt::format("{:{}}", s, 5), "42s ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>5}", s), " 42s");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^7}", s), "**42s**");
|
|
|
|
EXPECT_EQ(fmt::format("{:12%H:%M:%S}", std::chrono::seconds(12345)),
|
|
|
|
"03:25:45 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>12%H:%M:%S}", std::chrono::seconds(12345)),
|
|
|
|
" 03:25:45");
|
|
|
|
EXPECT_EQ(fmt::format("{:~^12%H:%M:%S}", std::chrono::seconds(12345)),
|
|
|
|
"~~03:25:45~~");
|
|
|
|
EXPECT_EQ(fmt::format("{:{}%H:%M:%S}", std::chrono::seconds(12345), 12),
|
|
|
|
"03:25:45 ");
|
2018-12-16 07:00:57 -08:00
|
|
|
}
|
|
|
|
|
2023-01-09 13:25:31 -06:00
|
|
|
TEST(chrono_test, tm_align) {
|
|
|
|
auto t = make_tm(1975, 12, 29, 12, 14, 16);
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%F %T}", t), "1975-12-29 12:14:16");
|
|
|
|
EXPECT_EQ(fmt::format("{:30%F %T}", t), "1975-12-29 12:14:16 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:{}%F %T}", t, 30), "1975-12-29 12:14:16 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:<30%F %T}", t), "1975-12-29 12:14:16 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:^30%F %T}", t), " 1975-12-29 12:14:16 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>30%F %T}", t), " 1975-12-29 12:14:16");
|
2023-01-09 13:25:31 -06:00
|
|
|
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:*<30%F %T}", t), "1975-12-29 12:14:16***********");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^30%F %T}", t), "*****1975-12-29 12:14:16******");
|
|
|
|
EXPECT_EQ(fmt::format("{:*>30%F %T}", t), "***********1975-12-29 12:14:16");
|
2023-01-09 13:25:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(chrono_test, tp_align) {
|
|
|
|
auto tp = std::chrono::time_point_cast<std::chrono::microseconds>(
|
|
|
|
std::chrono::system_clock::from_time_t(0));
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", tp), "00:00.000000");
|
|
|
|
EXPECT_EQ(fmt::format("{:15%M:%S}", tp), "00:00.000000 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:{}%M:%S}", tp, 15), "00:00.000000 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:<15%M:%S}", tp), "00:00.000000 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:^15%M:%S}", tp), " 00:00.000000 ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>15%M:%S}", tp), " 00:00.000000");
|
2023-01-09 13:25:31 -06:00
|
|
|
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:*<15%M:%S}", tp), "00:00.000000***");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^15%M:%S}", tp), "*00:00.000000**");
|
|
|
|
EXPECT_EQ(fmt::format("{:*>15%M:%S}", tp), "***00:00.000000");
|
2023-01-09 13:25:31 -06:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_specs) {
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%%}", std::chrono::seconds(0)), "%");
|
|
|
|
EXPECT_EQ(fmt::format("{:%n}", std::chrono::seconds(0)), "\n");
|
|
|
|
EXPECT_EQ(fmt::format("{:%t}", std::chrono::seconds(0)), "\t");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(0)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(60)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::seconds(42)), "42");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds(1234)), "01.234");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(0)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(60)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M}", std::chrono::minutes(42)), "42");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M}", std::chrono::seconds(61)), "01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(0)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(24)), "00");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H}", std::chrono::hours(14)), "14");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H}", std::chrono::minutes(61)), "01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(0)), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(12)), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(24)), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(4)), "04");
|
|
|
|
EXPECT_EQ(fmt::format("{:%I}", std::chrono::hours(14)), "02");
|
2024-09-18 09:27:45 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%j}", days(12)), "12");
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%j}", days(12345)), "12345");
|
|
|
|
EXPECT_EQ(fmt::format("{:%j}", std::chrono::hours(12345 * 24 + 12)), "12345");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H:%M:%S}", std::chrono::seconds(12345)),
|
|
|
|
"03:25:45");
|
|
|
|
EXPECT_EQ(fmt::format("{:%R}", std::chrono::seconds(12345)), "03:25");
|
|
|
|
EXPECT_EQ(fmt::format("{:%T}", std::chrono::seconds(12345)), "03:25:45");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Q}", std::chrono::seconds(12345)), "12345");
|
|
|
|
EXPECT_EQ(fmt::format("{:%q}", std::chrono::seconds(12345)), "s");
|
2018-12-09 09:11:52 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, invalid_specs) {
|
2018-12-09 09:11:52 -08:00
|
|
|
auto sec = std::chrono::seconds(0);
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%a}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%A}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%c}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%x}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ex}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%X}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%EX}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%D}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%F}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Ec}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%w}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%u}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%b}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%B}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%z}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Z}"), sec), fmt::format_error,
|
2021-05-18 19:38:52 -07:00
|
|
|
"no date");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Eq}"), sec), fmt::format_error,
|
2018-12-09 09:11:52 -08:00
|
|
|
"invalid format");
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%Oq}"), sec), fmt::format_error,
|
2018-12-09 09:11:52 -08:00
|
|
|
"invalid format");
|
2022-12-17 03:52:20 -06:00
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:abc}"), sec), fmt::format_error,
|
|
|
|
"invalid format");
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:.2f}"), sec), fmt::format_error,
|
|
|
|
"invalid format");
|
2018-12-07 10:19:44 -08:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
auto format_tm(const std::tm& time, fmt::string_view spec,
|
|
|
|
const std::locale& loc) -> std::string {
|
|
|
|
auto& facet = std::use_facet<std::time_put<char>>(loc);
|
|
|
|
std::ostringstream os;
|
|
|
|
os.imbue(loc);
|
|
|
|
facet.put(os, os, ' ', &time, spec.begin(), spec.end());
|
|
|
|
return os.str();
|
|
|
|
}
|
|
|
|
|
2021-05-24 07:23:56 -07:00
|
|
|
TEST(chrono_test, locale) {
|
|
|
|
auto loc = get_locale("ja_JP.utf8");
|
|
|
|
if (loc == std::locale::classic()) return;
|
2025-04-12 09:00:43 -07:00
|
|
|
#define EXPECT_TIME(spec, time, duration) \
|
|
|
|
{ \
|
|
|
|
auto jp_loc = std::locale("ja_JP.utf8"); \
|
|
|
|
EXPECT_EQ(format_tm(time, spec, jp_loc), \
|
|
|
|
fmt::format(jp_loc, "{:L" spec "}", duration)); \
|
|
|
|
}
|
2018-12-07 10:19:44 -08:00
|
|
|
EXPECT_TIME("%OH", make_hour(14), std::chrono::hours(14));
|
|
|
|
EXPECT_TIME("%OI", make_hour(14), std::chrono::hours(14));
|
|
|
|
EXPECT_TIME("%OM", make_minute(42), std::chrono::minutes(42));
|
|
|
|
EXPECT_TIME("%OS", make_second(42), std::chrono::seconds(42));
|
|
|
|
auto time = make_tm();
|
|
|
|
time.tm_hour = 3;
|
|
|
|
time.tm_min = 25;
|
|
|
|
time.tm_sec = 45;
|
2018-12-09 09:11:52 -08:00
|
|
|
auto sec = std::chrono::seconds(12345);
|
|
|
|
EXPECT_TIME("%r", time, sec);
|
|
|
|
EXPECT_TIME("%p", time, sec);
|
2018-12-07 10:19:44 -08:00
|
|
|
}
|
2019-01-19 07:16:05 -08:00
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
using dms = std::chrono::duration<double, std::milli>;
|
2019-01-12 14:31:55 +01:00
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_default_fp) {
|
2023-12-30 16:07:35 -08:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float>(1.234)), "1.234s");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::milli>(1.234)),
|
|
|
|
"1.234ms");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<double>(1.234)), "1.234s");
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{}", dms(1.234)), "1.234ms");
|
2019-01-12 14:31:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_precision) {
|
2021-11-25 11:52:21 -05:00
|
|
|
EXPECT_THROW_MSG(
|
2022-11-02 21:58:51 +03:00
|
|
|
(void)fmt::format(runtime("{:.2%Q}"), std::chrono::seconds(42)),
|
2021-11-25 11:52:21 -05:00
|
|
|
fmt::format_error, "precision not allowed for this argument type");
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:.0}", dms(1.234)), "1ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:.1}", dms(1.234)), "1.2ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:.{}}", dms(1.234), 2), "1.23ms");
|
2021-11-06 15:57:22 +00:00
|
|
|
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:.0}", dms(12.56)), "13ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:.1}", dms(12.56)), "12.6ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:.2}", dms(12.56)), "12.56ms");
|
2019-01-12 14:31:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_full_specs) {
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:6.0}", dms(1.234)), "1ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:6.1}", dms(1.234)), "1.2ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>8.{}}", dms(1.234), 2), " 1.23ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:^{}.{}}", dms(1.234), 7, 1), " 1.2ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{0:^{2}.{1}}", dms(1.234), 2, 8), " 1.23ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:=^{}.{}}", dms(1.234), 9, 3), "=1.234ms=");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^10.4}", dms(1.234)), "*1.2340ms*");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:6.0}", dms(12.56)), "13ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>8.{}}", dms(12.56), 0), " 13ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:^{}.{}}", dms(12.56), 6, 0), " 13ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{0:^{2}.{1}}", dms(12.56), 0, 8), " 13ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:=^{}.{}}", dms(12.56), 9, 0), "==13ms===");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^10.0}", dms(12.56)), "***13ms***");
|
2019-01-25 16:27:35 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_simple_q) {
|
2023-12-30 16:07:35 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%Q %q}", std::chrono::duration<float>(1.234)),
|
|
|
|
"1.234 s");
|
|
|
|
EXPECT_EQ(
|
|
|
|
fmt::format("{:%Q %q}", std::chrono::duration<float, std::milli>(1.234)),
|
|
|
|
"1.234 ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Q %q}", std::chrono::duration<double>(1.234)),
|
|
|
|
"1.234 s");
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%Q %q}", dms(1.234)), "1.234 ms");
|
2019-01-25 16:27:35 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_precision_q) {
|
2021-11-25 11:52:21 -05:00
|
|
|
EXPECT_THROW_MSG(
|
|
|
|
(void)fmt::format(runtime("{:.2%Q %q}"), std::chrono::seconds(42)),
|
|
|
|
fmt::format_error, "precision not allowed for this argument type");
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:.1%Q %q}", dms(1.234)), "1.2 ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:.{}%Q %q}", dms(1.234), 2), "1.23 ms");
|
2019-01-25 16:27:35 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, format_full_specs_q) {
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:7.0%Q %q}", dms(1.234)), "1 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:7.1%Q %q}", dms(1.234)), "1.2 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>8.{}%Q %q}", dms(1.234), 2), " 1.23 ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:^{}.{}%Q %q}", dms(1.234), 8, 1), " 1.2 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{0:^{2}.{1}%Q %q}", dms(1.234), 2, 9), " 1.23 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:=^{}.{}%Q %q}", dms(1.234), 10, 3), "=1.234 ms=");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^11.4%Q %q}", dms(1.234)), "*1.2340 ms*");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:7.0%Q %q}", dms(12.56)), "13 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:>8.{}%Q %q}", dms(12.56), 0), " 13 ms");
|
|
|
|
EXPECT_EQ(fmt::format("{:^{}.{}%Q %q}", dms(12.56), 8, 0), " 13 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{0:^{2}.{1}%Q %q}", dms(12.56), 0, 9), " 13 ms ");
|
|
|
|
EXPECT_EQ(fmt::format("{:=^{}.{}%Q %q}", dms(12.56), 9, 0), "==13 ms==");
|
|
|
|
EXPECT_EQ(fmt::format("{:*^11.0%Q %q}", dms(12.56)), "***13 ms***");
|
2019-01-12 14:31:55 +01:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, invalid_width_id) {
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW((void)fmt::format(runtime("{:{o}"), std::chrono::seconds(0)),
|
2019-04-28 21:34:09 +02:00
|
|
|
fmt::format_error);
|
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, invalid_colons) {
|
2021-11-24 17:09:41 -05:00
|
|
|
EXPECT_THROW((void)fmt::format(runtime("{0}=:{0::"), std::chrono::seconds(0)),
|
2019-04-28 21:34:09 +02:00
|
|
|
fmt::format_error);
|
|
|
|
}
|
2019-04-28 21:35:21 +02:00
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, negative_durations) {
|
2023-12-28 16:56:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%Q}", std::chrono::seconds(-12345)), "-12345");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H:%M:%S}", std::chrono::seconds(-12345)),
|
|
|
|
"-03:25:45");
|
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration<double>(-1)),
|
|
|
|
"-00:01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%q}", std::chrono::seconds(-12345)), "s");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}",
|
|
|
|
std::chrono::duration<signed char, std::milli>(-127)),
|
|
|
|
"-00.127");
|
2019-06-01 09:07:49 -07:00
|
|
|
auto min = std::numeric_limits<int>::min();
|
|
|
|
EXPECT_EQ(fmt::format("{}", min),
|
|
|
|
fmt::format("{:%Q}", std::chrono::duration<int>(min)));
|
2019-05-29 18:02:26 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, special_durations) {
|
2023-12-28 16:56:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<double>(1e20)), "40");
|
2019-05-05 08:34:54 -07:00
|
|
|
auto nan = std::numeric_limits<double>::quiet_NaN();
|
|
|
|
EXPECT_EQ(
|
2023-12-28 16:56:42 -08:00
|
|
|
fmt::format("{:%I %H %M %S %R %r}", std::chrono::duration<double>(nan)),
|
|
|
|
"nan nan nan nan nan:nan nan");
|
2019-05-11 08:42:02 -07:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::exa>(1)),
|
|
|
|
"1Es");
|
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<float, std::atto>(1)),
|
|
|
|
"1as");
|
2019-05-29 18:02:26 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%R}", std::chrono::duration<char, std::mega>{2}),
|
|
|
|
"03:33");
|
|
|
|
EXPECT_EQ(fmt::format("{:%T}", std::chrono::duration<char, std::mega>{2}),
|
|
|
|
"03:33:20");
|
2023-12-28 16:56:42 -08:00
|
|
|
EXPECT_EQ(
|
|
|
|
fmt::format("{:.3%S}", std::chrono::duration<float, std::pico>(1.234e12)),
|
|
|
|
"01.234");
|
2019-05-04 16:33:17 -07:00
|
|
|
}
|
|
|
|
|
2021-04-29 15:25:02 -07:00
|
|
|
TEST(chrono_test, unsigned_duration) {
|
2023-12-28 16:56:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{}", std::chrono::duration<unsigned>(42)), "42s");
|
2020-11-12 07:00:11 -08:00
|
|
|
}
|
|
|
|
|
2021-05-25 06:40:21 -07:00
|
|
|
TEST(chrono_test, weekday) {
|
2023-10-08 08:45:37 -07:00
|
|
|
auto loc = get_locale("es_ES.UTF-8");
|
2021-05-24 07:23:56 -07:00
|
|
|
std::locale::global(loc);
|
2021-10-30 20:25:45 +05:00
|
|
|
|
2024-04-03 18:35:53 -04:00
|
|
|
auto sat = fmt::weekday(6);
|
2024-08-25 09:20:40 -07:00
|
|
|
|
2024-04-05 08:36:45 +09:00
|
|
|
auto tm = std::tm();
|
|
|
|
tm.tm_wday = static_cast<int>(sat.c_encoding());
|
2021-10-30 20:25:45 +05:00
|
|
|
|
2023-10-08 08:45:37 -07:00
|
|
|
EXPECT_EQ(fmt::format("{}", sat), "Sat");
|
2024-04-03 18:35:53 -04:00
|
|
|
EXPECT_EQ(fmt::format("{:%a}", sat), "Sat");
|
|
|
|
EXPECT_EQ(fmt::format("{:%A}", sat), "Saturday");
|
2024-04-05 08:36:45 +09:00
|
|
|
EXPECT_EQ(fmt::format("{:%a}", tm), "Sat");
|
2021-10-30 20:25:45 +05:00
|
|
|
|
2021-05-24 07:23:56 -07:00
|
|
|
if (loc != std::locale::classic()) {
|
2024-09-03 12:50:03 -07:00
|
|
|
auto saturdays = std::vector<std::string>{"sáb", "sá.", "sáb."};
|
2023-10-08 08:45:37 -07:00
|
|
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
|
2025-04-13 09:17:48 -07:00
|
|
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L%a}", sat)));
|
|
|
|
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L%a}", tm)));
|
2021-05-24 07:23:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-09 15:45:13 +01:00
|
|
|
TEST(chrono_test, cpp20_duration_subsecond_support) {
|
|
|
|
using attoseconds = std::chrono::duration<long long, std::atto>;
|
|
|
|
// Check that 18 digits of subsecond precision are supported.
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", attoseconds{999999999999999999}),
|
|
|
|
"00.999999999999999999");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", attoseconds{673231113420148734}),
|
|
|
|
"00.673231113420148734");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", attoseconds{-673231113420148734}),
|
|
|
|
"-00.673231113420148734");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{13420148734}),
|
|
|
|
"13.420148734");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::nanoseconds{-13420148734}),
|
|
|
|
"-13.420148734");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::milliseconds{1234}), "01.234");
|
2022-11-02 21:58:51 +03:00
|
|
|
// Check subsecond presision modifier.
|
|
|
|
EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::nanoseconds{1234}),
|
|
|
|
"00.000001");
|
|
|
|
EXPECT_EQ(fmt::format("{:.18%S}", std::chrono::nanoseconds{1234}),
|
|
|
|
"00.000001234000000000");
|
|
|
|
EXPECT_EQ(fmt::format("{:.{}%S}", std::chrono::nanoseconds{1234}, 6),
|
|
|
|
"00.000001");
|
|
|
|
EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{1234}),
|
|
|
|
"01.234000");
|
|
|
|
EXPECT_EQ(fmt::format("{:.6%S}", std::chrono::milliseconds{-1234}),
|
|
|
|
"-01.234000");
|
2024-08-25 09:20:40 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{12345}), "12.34");
|
|
|
|
EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{12375}), "12.37");
|
2024-02-07 12:47:32 -05:00
|
|
|
EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{-12375}),
|
|
|
|
"-12.37");
|
2024-08-25 09:20:40 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:.0%S}", std::chrono::milliseconds{12054}), "12");
|
|
|
|
EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{99999}), "39.99");
|
|
|
|
EXPECT_EQ(fmt::format("{:.2%S}", std::chrono::milliseconds{1000}), "01.00");
|
|
|
|
EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::milliseconds{1}), "00.001");
|
2022-11-02 21:58:51 +03:00
|
|
|
EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::seconds{1234}), "34.000");
|
|
|
|
EXPECT_EQ(fmt::format("{:.3%S}", std::chrono::hours{1234}), "00.000");
|
|
|
|
EXPECT_EQ(fmt::format("{:.5%S}", dms(1.234)), "00.00123");
|
|
|
|
EXPECT_EQ(fmt::format("{:.8%S}", dms(1.234)), "00.00123400");
|
2021-12-09 15:45:13 +01:00
|
|
|
{
|
|
|
|
// Check that {:%H:%M:%S} is equivalent to {:%T}.
|
|
|
|
auto dur = std::chrono::milliseconds{3601234};
|
|
|
|
auto formatted_dur = fmt::format("{:%T}", dur);
|
|
|
|
EXPECT_EQ(formatted_dur, "01:00:01.234");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
|
2022-11-02 21:58:51 +03:00
|
|
|
EXPECT_EQ(fmt::format("{:.6%H:%M:%S}", dur), "01:00:01.234000");
|
2021-12-09 15:45:13 +01:00
|
|
|
}
|
|
|
|
using nanoseconds_dbl = std::chrono::duration<double, std::nano>;
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl(-123456789)), "-00.123456789");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl(9123456789)), "09.123456789");
|
2021-12-09 15:45:13 +01:00
|
|
|
// Verify that only the seconds part is extracted and printed.
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl(99123456789)), "39.123456789");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", nanoseconds_dbl(99123000000)), "39.123000000");
|
2021-12-09 15:45:13 +01:00
|
|
|
{
|
|
|
|
// Now the hour is printed, and we also test if negative doubles work.
|
|
|
|
auto dur = nanoseconds_dbl{-99123456789};
|
|
|
|
auto formatted_dur = fmt::format("{:%T}", dur);
|
|
|
|
EXPECT_EQ(formatted_dur, "-00:01:39.123456789");
|
|
|
|
EXPECT_EQ(fmt::format("{:%H:%M:%S}", dur), formatted_dur);
|
2022-11-02 21:58:51 +03:00
|
|
|
EXPECT_EQ(fmt::format("{:.3%H:%M:%S}", dur), "-00:01:39.123");
|
2021-12-09 15:45:13 +01:00
|
|
|
}
|
|
|
|
// Check that durations with precision greater than std::chrono::seconds have
|
|
|
|
// fixed precision, and print zeros even if there is no fractional part.
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::microseconds(7000000)),
|
2021-12-09 15:45:13 +01:00
|
|
|
"07.000000");
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}",
|
|
|
|
std::chrono::duration<long long, std::ratio<1, 3>>(1)),
|
2022-01-17 21:14:59 +01:00
|
|
|
"00.333333");
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}",
|
|
|
|
std::chrono::duration<long long, std::ratio<1, 7>>(1)),
|
2022-01-17 21:14:59 +01:00
|
|
|
"00.142857");
|
2022-10-12 23:33:53 +02:00
|
|
|
|
2023-03-22 23:34:59 +05:00
|
|
|
EXPECT_EQ(
|
|
|
|
fmt::format("{:%S}",
|
|
|
|
std::chrono::duration<signed char, std::ratio<1, 100>>(0x80)),
|
|
|
|
"-01.28");
|
2022-10-17 02:04:55 +05:00
|
|
|
|
|
|
|
EXPECT_EQ(
|
|
|
|
fmt::format("{:%M:%S}",
|
|
|
|
std::chrono::duration<short, std::ratio<1, 100>>(0x8000)),
|
|
|
|
"-05:27.68");
|
|
|
|
|
2022-10-12 23:33:53 +02:00
|
|
|
// Check that floating point seconds with ratio<1,1> are printed.
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", std::chrono::duration<double>(1.5)),
|
2022-10-12 23:33:53 +02:00
|
|
|
"01.500000");
|
2025-04-12 10:14:57 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", std::chrono::duration<double>(-61.25)),
|
2022-10-12 23:33:53 +02:00
|
|
|
"-01:01.250000");
|
2021-12-09 15:45:13 +01:00
|
|
|
}
|
|
|
|
|
2022-09-28 16:31:53 +02:00
|
|
|
// Disable the utc_clock test for windows, as the icu.dll used for tzdb
|
|
|
|
// (time zone database) is not shipped with many windows versions.
|
|
|
|
#if FMT_USE_UTC_TIME && !defined(_WIN32)
|
|
|
|
TEST(chrono_test, utc_clock) {
|
|
|
|
auto t1 = std::chrono::system_clock::now();
|
|
|
|
auto t1_utc = std::chrono::utc_clock::from_sys(t1);
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%m-%d %H:%M:%S}", t1),
|
|
|
|
fmt::format("{:%Y-%m-%d %H:%M:%S}", t1_utc));
|
|
|
|
}
|
|
|
|
#endif
|
2022-10-12 23:33:53 +02:00
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
TEST(chrono_test, timestamp_ratios) {
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t1 =
|
|
|
|
sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(67890));
|
2023-05-12 00:25:21 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", t1), "01:07.890");
|
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
auto t2 = sys_time<std::chrono::minutes>(std::chrono::minutes(7));
|
2023-05-12 00:25:21 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", t2), "07:00");
|
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
auto t3 = sys_time<std::chrono::duration<int, std::ratio<9>>>(
|
|
|
|
std::chrono::duration<int, std::ratio<9>>(7));
|
2023-05-12 00:25:21 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", t3), "01:03");
|
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
auto t4 = sys_time<std::chrono::duration<int, std::ratio<63>>>(
|
|
|
|
std::chrono::duration<int, std::ratio<63>>(1));
|
2023-05-12 00:25:21 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%M:%S}", t4), "01:03");
|
2023-11-25 16:36:55 +00:00
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
if (sizeof(time_t) > 4) {
|
2023-12-28 16:56:42 -08:00
|
|
|
auto tp =
|
|
|
|
sys_time<std::chrono::milliseconds>(std::chrono::seconds(32503680000));
|
2023-12-28 16:14:32 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%m-%d}", tp), "3000-01-01");
|
|
|
|
}
|
2023-11-25 16:36:55 +00:00
|
|
|
|
2023-12-28 16:14:32 -08:00
|
|
|
if (FMT_SAFE_DURATION_CAST) {
|
|
|
|
using years = std::chrono::duration<std::int64_t, std::ratio<31556952>>;
|
|
|
|
auto tp = sys_time<years>(years(std::numeric_limits<std::int64_t>::max()));
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format("{:%Y-%m-%d}", tp), fmt::format_error,
|
|
|
|
"cannot format duration");
|
|
|
|
}
|
2023-05-12 00:25:21 +02:00
|
|
|
}
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
TEST(chrono_test, timestamp_sub_seconds) {
|
|
|
|
auto t1 = sys_time<std::chrono::duration<long long, std::ratio<1, 3>>>(
|
|
|
|
std::chrono::duration<long long, std::ratio<1, 3>>(4));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t1), "01.333333");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t2 = sys_time<std::chrono::duration<double, std::ratio<1, 3>>>(
|
|
|
|
std::chrono::duration<double, std::ratio<1, 3>>(4));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t2), "01.333333");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t3 = sys_time<std::chrono::seconds>(std::chrono::seconds(2));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t3), "02");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t4 = sys_time<std::chrono::duration<double>>(
|
|
|
|
std::chrono::duration<double, std::ratio<1, 1>>(9.5));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t4), "09.500000");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t5 = sys_time<std::chrono::duration<double>>(
|
|
|
|
std::chrono::duration<double, std::ratio<1, 1>>(9));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t5), "09");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t6 = sys_time<std::chrono::milliseconds>(std::chrono::seconds(1) +
|
|
|
|
std::chrono::milliseconds(120));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t6), "01.120");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t7 =
|
|
|
|
sys_time<std::chrono::microseconds>(std::chrono::microseconds(1234567));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t7), "01.234567");
|
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto t8 =
|
|
|
|
sys_time<std::chrono::nanoseconds>(std::chrono::nanoseconds(123456789));
|
2022-10-12 23:33:53 +02:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t8), "00.123456789");
|
2025-04-12 09:00:43 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%T}", t8), "00:00:00.123456789");
|
2022-10-12 23:33:53 +02:00
|
|
|
|
2025-04-12 09:00:43 -07:00
|
|
|
auto t9 =
|
2023-12-28 16:56:42 -08:00
|
|
|
sys_time<std::chrono::milliseconds>(std::chrono::milliseconds(2000));
|
2025-04-12 09:00:43 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", t9), "02.000");
|
2023-01-11 13:36:50 -06:00
|
|
|
|
2023-12-28 16:56:42 -08:00
|
|
|
auto epoch = sys_time<std::chrono::milliseconds>();
|
|
|
|
auto d = std::chrono::milliseconds(250);
|
2023-12-29 16:34:42 -08:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", epoch - d), "59.750");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", epoch), "00.000");
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", epoch + d), "00.250");
|
2022-10-12 23:33:53 +02:00
|
|
|
}
|
2023-02-08 11:22:58 -06:00
|
|
|
|
|
|
|
TEST(chrono_test, glibc_extensions) {
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%0}"), std::chrono::seconds()),
|
|
|
|
fmt::format_error, "invalid format");
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%_}"), std::chrono::seconds()),
|
|
|
|
fmt::format_error, "invalid format");
|
|
|
|
EXPECT_THROW_MSG((void)fmt::format(runtime("{:%-}"), std::chrono::seconds()),
|
|
|
|
fmt::format_error, "invalid format");
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto d = std::chrono::hours(1) + std::chrono::minutes(2) +
|
|
|
|
std::chrono::seconds(3);
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", d), "01,01,02,03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", d), " 1, 1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", d), "1,1,2,3");
|
2024-01-17 02:54:19 +05:00
|
|
|
EXPECT_EQ(fmt::format("{:%-I,%H,%M,%S}", d), "1,01,02,03");
|
2023-02-08 11:22:58 -06:00
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", d), "01,01,02,03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", d), " 1, 1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", d), "1,1,2,3");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto tm = make_tm(1970, 1, 1, 1, 2, 3);
|
|
|
|
EXPECT_EQ(fmt::format("{:%I,%H,%M,%S}", tm), "01,01,02,03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_I,%_H,%_M,%_S}", tm), " 1, 1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-I,%-H,%-M,%-S}", tm), "1,1,2,3");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:%OI,%OH,%OM,%OS}", tm), "01,01,02,03");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", tm), " 1, 1, 2, 3");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", tm), "1,1,2,3");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const auto d = std::chrono::seconds(3) + std::chrono::milliseconds(140);
|
|
|
|
EXPECT_EQ(fmt::format("{:%S}", d), "03.140");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-S}", d), "3.140");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2024-05-31 03:20:56 +10:00
|
|
|
auto d = std::chrono::duration<double>(3.14);
|
2023-02-08 11:22:58 -06:00
|
|
|
EXPECT_EQ(fmt::format("{:%S}", d), "03.140000");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140000");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-S}", d), "3.140000");
|
|
|
|
}
|
2024-05-31 03:20:56 +10:00
|
|
|
|
|
|
|
{
|
|
|
|
auto t = std::tm();
|
|
|
|
t.tm_yday = 7;
|
|
|
|
EXPECT_EQ(fmt::format("{:%U,%W,%V}", t), "02,01,01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_U,%_W,%_V}", t), " 2, 1, 1");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-U,%-W,%-V}", t), "2,1,1");
|
2024-09-18 09:27:45 -07:00
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:%j}", t), "008");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_j}", t), " 8");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-j}", t), "8");
|
2024-05-31 03:20:56 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto t = std::tm();
|
|
|
|
t.tm_mday = 7;
|
|
|
|
EXPECT_EQ(fmt::format("{:%d}", t), "07");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_d}", t), " 7");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-d}", t), "7");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{:%e}", t), " 7");
|
|
|
|
}
|
2024-09-18 09:27:45 -07:00
|
|
|
|
|
|
|
{
|
|
|
|
auto t = std::tm();
|
|
|
|
t.tm_year = 7 - 1900;
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", t), "0007");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_Y}", t), " 7");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-Y}", t), "7");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto t = std::tm();
|
|
|
|
t.tm_year = -5 - 1900;
|
2025-03-23 09:49:23 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", t), "-005");
|
2024-09-18 09:27:45 -07:00
|
|
|
EXPECT_EQ(fmt::format("{:%_Y}", t), " -5");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-Y}", t), "-5");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto t = std::tm();
|
|
|
|
t.tm_mon = 7 - 1;
|
|
|
|
EXPECT_EQ(fmt::format("{:%m}", t), "07");
|
|
|
|
EXPECT_EQ(fmt::format("{:%_m}", t), " 7");
|
|
|
|
EXPECT_EQ(fmt::format("{:%-m}", t), "7");
|
|
|
|
}
|
2023-02-08 11:22:58 -06:00
|
|
|
}
|
2023-12-10 07:26:45 -08:00
|
|
|
|
|
|
|
TEST(chrono_test, out_of_range) {
|
|
|
|
auto d = std::chrono::duration<unsigned long, std::giga>(538976288);
|
|
|
|
EXPECT_THROW((void)fmt::format("{:%j}", d), fmt::format_error);
|
2024-03-27 20:10:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(chrono_test, year_month_day) {
|
2024-04-05 08:36:45 +09:00
|
|
|
auto loc = get_locale("es_ES.UTF-8");
|
|
|
|
std::locale::global(loc);
|
2024-08-25 09:20:40 -07:00
|
|
|
|
2024-03-27 20:10:30 -04:00
|
|
|
auto year = fmt::year(2024);
|
|
|
|
auto month = fmt::month(1);
|
|
|
|
auto day = fmt::day(1);
|
2024-04-03 18:35:53 -04:00
|
|
|
auto ymd = fmt::year_month_day(year, month, day);
|
2024-03-27 20:10:30 -04:00
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", year), "2024");
|
2024-04-03 18:35:53 -04:00
|
|
|
EXPECT_EQ(fmt::format("{:%Y}", year), "2024");
|
|
|
|
EXPECT_EQ(fmt::format("{:%y}", year), "24");
|
|
|
|
|
2024-03-27 20:10:30 -04:00
|
|
|
EXPECT_EQ(fmt::format("{}", month), "Jan");
|
2024-04-03 18:35:53 -04:00
|
|
|
EXPECT_EQ(fmt::format("{:%m}", month), "01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%b}", month), "Jan");
|
|
|
|
EXPECT_EQ(fmt::format("{:%B}", month), "January");
|
|
|
|
|
2024-03-27 20:10:30 -04:00
|
|
|
EXPECT_EQ(fmt::format("{}", day), "01");
|
2024-04-03 18:35:53 -04:00
|
|
|
EXPECT_EQ(fmt::format("{:%d}", day), "01");
|
|
|
|
|
|
|
|
EXPECT_EQ(fmt::format("{}", ymd), "2024-01-01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%m-%d}", ymd), "2024-01-01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%b-%d}", ymd), "2024-Jan-01");
|
|
|
|
EXPECT_EQ(fmt::format("{:%Y-%B-%d}", ymd), "2024-January-01");
|
|
|
|
|
|
|
|
if (loc != std::locale::classic()) {
|
|
|
|
auto months = std::vector<std::string>{"ene.", "ene"};
|
|
|
|
EXPECT_THAT(months, Contains(fmt::format(loc, "{:L}", month)));
|
2025-04-13 09:17:48 -07:00
|
|
|
EXPECT_THAT(months, Contains(fmt::format(loc, "{:L%b}", month)));
|
2024-04-03 18:35:53 -04:00
|
|
|
}
|
2024-03-27 20:10:30 -04:00
|
|
|
}
|