mirror of
https://github.com/fmtlib/fmt.git
synced 2025-11-25 11:49:52 +01:00
implement year_month_day (#3913)
* implement year_month_day, also changed weekday, day, month, year's formatter to use formatter<std::tm, Char> so they all support the format strings * support ":L" for month and weekday
This commit is contained in:
@@ -751,18 +751,17 @@ TEST(chrono_test, unsigned_duration) {
|
||||
TEST(chrono_test, weekday) {
|
||||
auto loc = get_locale("es_ES.UTF-8");
|
||||
std::locale::global(loc);
|
||||
|
||||
auto sat = fmt::weekday(6);
|
||||
|
||||
auto tm = std::tm();
|
||||
tm.tm_wday = static_cast<int>(sat.c_encoding());
|
||||
|
||||
EXPECT_EQ(fmt::format("{}", sat), "Sat");
|
||||
EXPECT_EQ(fmt::format("{:%a}", tm), "Sat");
|
||||
EXPECT_EQ(fmt::format("{:%a}", sat), "Sat");
|
||||
EXPECT_EQ(fmt::format("{:%A}", sat), "Saturday");
|
||||
|
||||
if (loc != std::locale::classic()) {
|
||||
auto saturdays = std::vector<std::string>{"sáb", "sá."};
|
||||
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:L}", sat)));
|
||||
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", tm)));
|
||||
EXPECT_THAT(saturdays, Contains(fmt::format(loc, "{:%a}", sat)));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1014,13 +1013,33 @@ TEST(chrono_test, out_of_range) {
|
||||
}
|
||||
|
||||
TEST(chrono_test, year_month_day) {
|
||||
auto loc = get_locale("es_ES.UTF-8");
|
||||
std::locale::global(loc);
|
||||
auto year = fmt::year(2024);
|
||||
auto month = fmt::month(1);
|
||||
auto day = fmt::day(1);
|
||||
auto ymd = fmt::year_month_day(year, month, day);
|
||||
|
||||
EXPECT_EQ(fmt::format("{}", year), "2024");
|
||||
EXPECT_EQ(fmt::format("{:%Y}", year), "2024");
|
||||
EXPECT_EQ(fmt::format("{:%y}", year), "24");
|
||||
|
||||
EXPECT_EQ(fmt::format("{}", month), "Jan");
|
||||
EXPECT_EQ(fmt::format("{:%m}", month), "01");
|
||||
EXPECT_EQ(fmt::format("{:%b}", month), "Jan");
|
||||
EXPECT_EQ(fmt::format("{:%B}", month), "January");
|
||||
|
||||
EXPECT_EQ(fmt::format("{}", day), "01");
|
||||
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");
|
||||
|
||||
auto loc = get_locale("es_ES.UTF-8");
|
||||
std::locale::global(loc);
|
||||
if (loc != std::locale::classic()) {
|
||||
auto months = std::vector<std::string>{"ene.", "ene"};
|
||||
EXPECT_THAT(months, Contains(fmt::format(loc, "{:L}", month)));
|
||||
EXPECT_THAT(months, Contains(fmt::format(loc, "{:%b}", month)));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user