xref: /llvm-project/libcxx/test/std/time/time.syn/formatter.local_info.pass.cpp (revision 6a54dfbfe534276d644d7f9c027f0deeb748dd53)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 // UNSUPPORTED: no-localization
11 // UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME
12 
13 // TODO FMT This test should not require std::to_chars(floating-point)
14 // XFAIL: availability-fp_to_chars-missing
15 
16 // XFAIL: libcpp-has-no-experimental-tzdb
17 
18 // REQUIRES: locale.fr_FR.UTF-8
19 
20 // <chrono>
21 //
22 // template<class charT> struct formatter<chrono::local_info, charT>;
23 
24 #include <chrono>
25 #include <format>
26 
27 #include <cassert>
28 #include <concepts>
29 #include <locale>
30 #include <iostream>
31 #include <type_traits>
32 
33 #include "formatter_tests.h"
34 #include "make_string.h"
35 #include "platform_support.h" // locale name macros
36 #include "test_macros.h"
37 
38 template <class CharT>
39 static void test_no_chrono_specs() {
40 // This test libc++ specific due to
41 // [time.zone.info.local]/3
42 //   Effects: Streams out the local_info object r in an unspecified format.
43 #ifdef _LIBCPP_VERSION
44   using namespace std::literals::chrono_literals;
45   namespace tz = std::chrono;
46 
47   std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
48 
49   // Non localized output
50 
51   // result values matching the "known" results
52   check(SV("unique: "
53            "{[-10484-10-16 15:30:08, 14423-03-17 15:30:07) 00:00:00 0min \"TZ\", "
54            "[1970-01-01 00:00:00, 1970-01-01 00:00:00) 00:00:00 0min \"\"}"),
55         SV("{}"),
56         tz::local_info{tz::local_info::unique,
57                        tz::sys_info{tz::sys_seconds::min(), tz::sys_seconds::max(), 0s, 0min, "TZ"},
58                        tz::sys_info{}});
59 
60   check(SV("non-existent: "
61            "{[1970-01-01 00:00:00, 2038-12-31 00:00:00) 12:23:45 -67min \"NEG\", "
62            "[1970-01-01 00:00:00, 2038-12-31 00:00:00) -12:23:45 67min \"POS\"}"),
63         SV("{}"),
64         tz::local_info{
65             tz::local_info::nonexistent,
66             tz::sys_info{static_cast<tz::sys_days>(tz::year_month_day{1970y, tz::January, 1d}),
67                          static_cast<tz::sys_days>(tz::year_month_day{2038y, tz::December, 31d}),
68                          12h + 23min + 45s,
69                          -67min,
70                          "NEG"},
71             tz::sys_info{static_cast<tz::sys_days>(tz::year_month_day{1970y, tz::January, 1d}),
72                          static_cast<tz::sys_days>(tz::year_month_day{2038y, tz::December, 31d}),
73                          -(12h + 23min + 45s),
74                          67min,
75                          "POS"}});
76 
77   check(SV("ambiguous: "
78            "{[1970-01-01 00:00:00, 2038-12-31 00:00:00) 12:23:45 -67min \"NEG\", "
79            "[1970-01-01 00:00:00, 2038-12-31 00:00:00) -12:23:45 67min \"POS\"}"),
80         SV("{}"),
81         tz::local_info{
82             tz::local_info::ambiguous,
83             tz::sys_info{static_cast<tz::sys_days>(tz::year_month_day{1970y, tz::January, 1d}),
84                          static_cast<tz::sys_days>(tz::year_month_day{2038y, tz::December, 31d}),
85                          12h + 23min + 45s,
86                          -67min,
87                          "NEG"},
88             tz::sys_info{static_cast<tz::sys_days>(tz::year_month_day{1970y, tz::January, 1d}),
89                          static_cast<tz::sys_days>(tz::year_month_day{2038y, tz::December, 31d}),
90                          -(12h + 23min + 45s),
91                          67min,
92                          "POS"}});
93 
94   // result values not matching the "known" results
95   check(
96       SV("unspecified result (-1): "
97          "{[-10484-10-16 15:30:08, 14423-03-17 15:30:07) 00:00:00 0min \"TZ\", "
98          "[1970-01-01 00:00:00, 1970-01-01 00:00:00) 00:00:00 0min \"\"}"),
99       SV("{}"),
100       tz::local_info{-1, tz::sys_info{tz::sys_seconds::min(), tz::sys_seconds::max(), 0s, 0min, "TZ"}, tz::sys_info{}});
101   check(
102       SV("unspecified result (3): "
103          "{[-10484-10-16 15:30:08, 14423-03-17 15:30:07) 00:00:00 0min \"TZ\", "
104          "[1970-01-01 00:00:00, 1970-01-01 00:00:00) 00:00:00 0min \"\"}"),
105       SV("{}"),
106       tz::local_info{3, tz::sys_info{tz::sys_seconds::min(), tz::sys_seconds::max(), 0s, 0min, "TZ"}, tz::sys_info{}});
107 
108   std::locale::global(std::locale::classic());
109 #endif // _LIBCPP_VERSION
110 }
111 
112 template <class CharT>
113 static void test() {
114   test_no_chrono_specs<CharT>();
115 
116   check_invalid_types<CharT>({}, std::chrono::local_info{0, {}, {}});
117 }
118 
119 int main(int, char**) {
120   test<char>();
121 
122 #ifndef TEST_HAS_NO_WIDE_CHARACTERS
123   test<wchar_t>();
124 #endif
125 
126   return 0;
127 }
128