Move chrono-specific code to a separate header

This commit is contained in:
Victor Zverovich
2018-12-07 10:19:44 -08:00
parent bf1f1c73e3
commit f54f3d0fb7
8 changed files with 443 additions and 302 deletions

View File

@@ -66,61 +66,3 @@ TEST(TimeTest, GMTime) {
std::tm tm = *std::gmtime(&t);
EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t)));
}
#ifdef __cpp_lib_chrono
TEST(TimeTest, Chrono) {
EXPECT_EQ("00", fmt::format("{:%S}", std::chrono::seconds(0)));
EXPECT_EQ("00", fmt::format("{:%S}", std::chrono::seconds(60)));
EXPECT_EQ("42", fmt::format("{:%S}", std::chrono::seconds(42)));
EXPECT_EQ("01.234", fmt::format("{:%S}", std::chrono::milliseconds(1234)));
EXPECT_EQ("00", fmt::format("{:%M}", std::chrono::minutes(0)));
EXPECT_EQ("00", fmt::format("{:%M}", std::chrono::minutes(60)));
EXPECT_EQ("42", fmt::format("{:%M}", std::chrono::minutes(42)));
EXPECT_EQ("01", fmt::format("{:%M}", std::chrono::seconds(61)));
EXPECT_EQ("00", fmt::format("{:%H}", std::chrono::hours(0)));
EXPECT_EQ("00", fmt::format("{:%H}", std::chrono::hours(24)));
EXPECT_EQ("14", fmt::format("{:%H}", std::chrono::hours(14)));
EXPECT_EQ("01", fmt::format("{:%H}", std::chrono::minutes(61)));
EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(0)));
EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(12)));
EXPECT_EQ("12", fmt::format("{:%I}", std::chrono::hours(24)));
EXPECT_EQ("04", fmt::format("{:%I}", std::chrono::hours(4)));
EXPECT_EQ("02", fmt::format("{:%I}", std::chrono::hours(14)));
EXPECT_EQ("03:25:45",
fmt::format("{:%H:%M:%S}", std::chrono::seconds(12345)));
}
std::string format_tm(const std::tm &time, const char *spec,
const std::locale &loc) {
std::ostringstream os;
os.imbue(loc);
os << std::put_time(&time, spec);
return os.str();
}
#define EXPECT_TIME(spec, field, value, duration) { \
auto time = std::tm(); \
time.field = value; \
std::locale("ja_JP.utf8"); \
EXPECT_EQ(format_tm(time, spec, loc), \
fmt::format(loc, "{:" spec "}", std::chrono::duration(value))); \
}
TEST(TimeTest, ChronoLocale) {
const char *loc_name = "ja_JP.utf8";
bool has_locale = false;
std::locale loc;
try {
loc = std::locale(loc_name);
has_locale = true;
} catch (const std::runtime_error &) {}
if (!has_locale) {
fmt::print("{} locale is missing.\n", loc_name);
return;
}
EXPECT_TIME("%OH", tm_hour, 14, hours);
EXPECT_TIME("%OI", tm_hour, 14, hours);
EXPECT_TIME("%OM", tm_min, 42, minutes);
EXPECT_TIME("%OS", tm_sec, 42, seconds);
}
#endif