Skip to content

[libc++][chrono] Improves date formatting. #86127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions libcxx/include/__chrono/formatter.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,13 @@ _LIBCPP_HIDE_FROM_ABI void __format_chrono_using_chrono_specs(
__formatter::__format_year(__sstr, __t.tm_year + 1900);
break;

case _CharT('F'): {
int __year = __t.tm_year + 1900;
if (__year < 1000) {
__formatter::__format_year(__sstr, __year);
__sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "-{:02}-{:02}"), __t.tm_mon + 1, __t.tm_mday);
} else
__facet.put(
{__sstr}, __sstr, _CharT(' '), std::addressof(__t), std::to_address(__s), std::to_address(__it + 1));
} break;
case _CharT('F'):
// Depending on the platform's libc the range of supported years is
// limited. Instead of testing all conditions use the internal
// implementation unconditionally.
__formatter::__format_year(__sstr, __t.tm_year + 1900);
__sstr << std::format(_LIBCPP_STATICALLY_WIDEN(_CharT, "-{:02}-{:02}"), __t.tm_mon + 1, __t.tm_mday);
break;

case _CharT('z'):
__formatter::__format_zone_offset(__sstr, __z.__offset, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// TODO FMT This test should not require std::to_chars(floating-point)
// XFAIL: availability-fp_to_chars-missing

// TODO FMT Investigate Windows issues.
// XFAIL: msvc

// REQUIRES: locale.fr_FR.UTF-8
// REQUIRES: locale.ja_JP.UTF-8

Expand Down Expand Up @@ -89,20 +86,9 @@ static void test() {
TEST_EQUAL(stream_c_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}),
SV("2000-02-29"));

#if defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("32767-12-31"));
#endif // defined(_AIX)

TEST_EQUAL(stream_fr_FR_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{-32'768}, std::chrono::month{1}, std::chrono::day{1}}),
Expand All @@ -122,19 +108,9 @@ static void test() {
TEST_EQUAL(stream_fr_FR_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}),
SV("2000-02-29"));
#if defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("32767-12-31"));
#endif // defined(_AIX)

TEST_EQUAL(stream_ja_JP_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{-32'768}, std::chrono::month{1}, std::chrono::day{1}}),
Expand All @@ -154,19 +130,9 @@ static void test() {
TEST_EQUAL(stream_ja_JP_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}),
SV("2000-02-29"));
#if defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}),
SV("32767-12-31"));
#endif // defined(_AIX)
}

int main(int, char**) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
// TODO FMT This test should not require std::to_chars(floating-point)
// XFAIL: availability-fp_to_chars-missing

// TODO FMT Investigate Windows issues.
// XFAIL: msvc

// REQUIRES: locale.fr_FR.UTF-8
// REQUIRES: locale.ja_JP.UTF-8

Expand Down Expand Up @@ -81,20 +78,9 @@ static void test() {
TEST_EQUAL(stream_c_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}}),
SV("2000-02-29"));

#if defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_c_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("32767-12-31"));
#endif // defined(_AIX)

// multiples of days are considered days.
TEST_EQUAL(stream_c_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{3}}),
Expand All @@ -112,19 +98,9 @@ static void test() {
TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}}),
SV("2000-02-29"));
#if defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("32767-12-31"));
#endif // defined(_AIX)

// multiples of days are considered days.
TEST_EQUAL(stream_fr_FR_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{3}}),
Expand All @@ -142,19 +118,9 @@ static void test() {
TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{2000}, std::chrono::month{2}, std::chrono::day{29}}}),
SV("2000-02-29"));
#if defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("+32767-12-31"));
#elif defined(_WIN32) // defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV(""));
#else // defined(_AIX)
TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::sys_days{
std::chrono::year_month_day{std::chrono::year{32'767}, std::chrono::month{12}, std::chrono::day{31}}}),
SV("32767-12-31"));
#endif // defined(_AIX)

// multiples of days are considered days.
TEST_EQUAL(stream_ja_JP_locale<CharT>(std::chrono::sys_time<std::chrono::weeks>{std::chrono::weeks{3}}),
Expand Down
24 changes: 0 additions & 24 deletions libcxx/test/std/time/time.syn/formatter.year_month_day.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,6 @@ static void test_no_chrono_specs() {
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{2}, std::chrono::day{31}});

// Valid year, invalid month, valid day
#ifdef _WIN32
check(SV(" is not a valid date"),
SV("{}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{31}});
check(SV("****** is not a valid date******"),
SV("{:*^32}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{31}});
check(SV("*********** is not a valid date"),
SV("{:*>31}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{31}});
#else // _WIN32
check(SV("1970-00-31 is not a valid date"),
SV("{}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{31}});
Expand All @@ -82,20 +71,8 @@ static void test_no_chrono_specs() {
check(SV("*1970-00-31 is not a valid date"),
SV("{:*>31}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{31}});
#endif // _WIN32

// Valid year, invalid month, invalid day
#ifdef _WIN32
check(SV(" is not a valid date"),
SV("{}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{32}});
check(SV("****** is not a valid date******"),
SV("{:*^32}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{32}});
check(SV("*********** is not a valid date"),
SV("{:*>31}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{32}});
#else // _WIN32
check(SV("1970-00-32 is not a valid date"),
SV("{}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{32}});
Expand All @@ -105,7 +82,6 @@ static void test_no_chrono_specs() {
check(SV("*1970-00-32 is not a valid date"),
SV("{:*>31}"),
std::chrono::year_month_day{std::chrono::year{1970}, std::chrono::month{0}, std::chrono::day{32}});
#endif // _WIN32

// Invalid year, valid month, valid day
check(SV("-32768-01-31 is not a valid date"),
Expand Down