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// NetBSD does not support LC_TIME at the moment
10// XFAIL: netbsd
11
12// XFAIL: LIBCXX-FREEBSD-FIXME
13
14// REQUIRES: locale.en_US.UTF-8
15// REQUIRES: locale.fr_FR.UTF-8
16// REQUIRES: locale.ru_RU.UTF-8
17// REQUIRES: locale.zh_CN.UTF-8
18
19// <locale>
20
21// class time_get_byname<charT, InputIterator>
22
23// iter_type get(iter_type s, iter_type end, ios_base& f,
24// ios_base::iostate& err, tm *t, char format, char modifier = 0) const;
25
26#include <locale>
27#include <cassert>
28#include "test_macros.h"
29#include "test_iterators.h"
30
31#include "platform_support.h" // locale name macros
32
33typedef cpp17_input_iterator<const char*> I;
34
35typedef std::time_get_byname<char, I> F;
36
37class my_facet
38 : public F
39{
40public:
41 explicit my_facet(const std::string& nm, std::size_t refs = 0)
42 : F(nm, refs) {}
43};
44
45int main(int, char**)
46{
47 std::ios ios(0);
48 std::ios_base::iostate err;
49 std::tm t;
50 {
51 const my_facet f(LOCALE_en_US_UTF_8, 1);
52#ifdef _WIN32
53 // On Windows, the "%c" format lacks the leading week day, which
54 // means that t.tm_wday doesn't get set when parsing the string.
55 const char in[] = "12/31/2061 11:55:59 PM";
56#elif defined(TEST_HAS_GLIBC)
57 const char in[] = "Sat 31 Dec 2061 11:55:59 PM";
58#elif defined(_AIX)
59 const char in[] = "Dec 31, 2061 at 11:55:59 PM";
60#else
61 const char in[] = "Sat Dec 31 23:55:59 2061";
62#endif
63 err = std::ios_base::goodbit;
64 t = std::tm();
65 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'c');
66 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
67 assert(t.tm_sec == 59);
68 assert(t.tm_min == 55);
69 assert(t.tm_hour == 23);
70 assert(t.tm_mday == 31);
71 assert(t.tm_mon == 11);
72 assert(t.tm_year == 161);
73#if !defined(_WIN32) && !defined(_AIX)
74 assert(t.tm_wday == 6);
75#endif
76 assert(err == std::ios_base::eofbit);
77 }
78 {
79 const my_facet f(LOCALE_en_US_UTF_8, 1);
80#if defined(_WIN32) || defined(TEST_HAS_GLIBC) || defined(_AIX)
81 const char in[] = "11:55:59 PM";
82#else
83 const char in[] = "23:55:59";
84#endif
85 err = std::ios_base::goodbit;
86 t = std::tm();
87 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'X');
88 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
89 assert(t.tm_sec == 59);
90 assert(t.tm_min == 55);
91 assert(t.tm_hour == 23);
92 assert(err == std::ios_base::eofbit);
93 }
94 {
95 const my_facet f(LOCALE_fr_FR_UTF_8, 1);
96#ifdef _WIN32
97 const char in[] = "31/12/2061 23:55:59";
98#elif defined(TEST_HAS_GLIBC)
99 const char in[] = "sam. 31 d""\xC3\xA9""c. 2061 23:55:59";
100#elif defined(_AIX)
101 const char in[] = "31 d""\xC3\xA9""c. 2061 ""\xC3\xA0"" 23:55:59";
102#else
103 const char in[] = "Sam 31 d""\xC3\xA9""c 23:55:59 2061";
104#endif
105 err = std::ios_base::goodbit;
106 t = std::tm();
107 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'c');
108 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
109 assert(t.tm_sec == 59);
110 assert(t.tm_min == 55);
111 assert(t.tm_hour == 23);
112 assert(t.tm_mday == 31);
113 assert(t.tm_mon == 11);
114 assert(t.tm_year == 161);
115#if !defined(_WIN32) && !defined(_AIX)
116 assert(t.tm_wday == 6);
117#endif
118 assert(err == std::ios_base::eofbit);
119 }
120 {
121 const my_facet f(LOCALE_fr_FR_UTF_8, 1);
122 const char in[] = "23:55:59";
123 err = std::ios_base::goodbit;
124 t = std::tm();
125 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'X');
126 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
127 assert(t.tm_sec == 59);
128 assert(t.tm_min == 55);
129 assert(t.tm_hour == 23);
130 assert(err == std::ios_base::eofbit);
131 }
132 {
133 const my_facet f(LOCALE_ru_RU_UTF_8, 1);
134#ifdef TEST_HAS_GLIBC
135 const char in[] = "\xD0\xA1\xD0\xB1 31 \xD0\xB4\xD0\xB5\xD0\xBA 2061 23:55:59";
136#elif defined(_WIN32)
137 const char in[] = "31.12.2061 23:55:59";
138#elif defined(_AIX)
139 const char in[] = "31 \xD0\xB4\xD0\xB5\xD0\xBA. 2061 \xD0\xB3., 23:55:59";
140#else
141 const char in[] = "\xD1\x81\xD1\x83\xD0\xB1\xD0\xB1"
142 "\xD0\xBE\xD1\x82\xD0\xB0"
143 ", 31 "
144 "\xD0\xB4\xD0\xB5\xD0\xBA\xD0\xB0"
145 "\xD0\xB1\xD1\x80\xD1\x8F"
146 " 2061 "
147 "\xD0\xB3"
148 ". 23:55:59";
149#endif
150 err = std::ios_base::goodbit;
151 t = std::tm();
152 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'c');
153 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
154 assert(t.tm_sec == 59);
155 assert(t.tm_min == 55);
156 assert(t.tm_hour == 23);
157 assert(t.tm_mday == 31);
158 assert(t.tm_mon == 11);
159 assert(t.tm_year == 161);
160#if !defined(_WIN32) && !defined(_AIX)
161 assert(t.tm_wday == 6);
162#endif
163 assert(err == std::ios_base::eofbit);
164 }
165 {
166 const my_facet f(LOCALE_ru_RU_UTF_8, 1);
167 const char in[] = "23:55:59";
168 err = std::ios_base::goodbit;
169 t = std::tm();
170 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'X');
171 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
172 assert(t.tm_sec == 59);
173 assert(t.tm_min == 55);
174 assert(t.tm_hour == 23);
175 assert(err == std::ios_base::eofbit);
176 }
177 {
178 const my_facet f(LOCALE_zh_CN_UTF_8, 1);
179#ifdef TEST_HAS_GLIBC
180 const char in[] = "2061" "\xE5\xB9\xB4" "12" "\xE6\x9C\x88" "31"
181 "\xE6\x97\xA5" " "
182 "\xE6\x98\x9F\xE6\x9c\x9F\xE5\x85\xAD"
183 " 23" "\xE6\x97\xB6" "55" "\xE5\x88\x86" "59"
184 "\xE7\xA7\x92";
185#elif defined(_WIN32)
186 const char in[] = "2061/12/31 23:55:59";
187#elif defined(_AIX)
188 // The time field is omitted in the definition below because in the
189 // date-time format of locale zh_CN.UTF-8 on AIX, there is %Z before
190 // the time field, i.e, "... %Z %p%I:%M:%S", and its value varies
191 // depending on the date of the year and the location of the machine
192 // where the test case is run.
193 const char in[] = "2061" "\xE5\xB9\xB4" "12" "\xE6\x9C\x88" "31"
194 "\xE6\x97\xA5";
195#else
196 const char in[] = "\xE5\x85\xAD 12/31 23:55:59 2061";
197#endif
198 err = std::ios_base::goodbit;
199 t = std::tm();
200 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'c');
201 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
202#ifndef _AIX
203 assert(t.tm_sec == 59);
204 assert(t.tm_min == 55);
205 assert(t.tm_hour == 23);
206#endif
207 assert(t.tm_mday == 31);
208 assert(t.tm_mon == 11);
209 assert(t.tm_year == 161);
210#if !defined(_WIN32) && !defined(_AIX)
211 assert(t.tm_wday == 6);
212#endif
213#if !defined(_AIX)
214 assert(err == std::ios_base::eofbit);
215#endif
216 }
217 {
218 const my_facet f(LOCALE_zh_CN_UTF_8, 1);
219#if defined(_WIN32)
220 const char in[] = "23:55:59";
221#elif defined(_AIX)
222 const char in[] = "\xE4\xB8\x8B\xE5\x8D\x88" "11:55:59";
223#else
224 const char in[] = "23""\xE6\x97\xB6""55""\xE5\x88\x86""59""\xE7\xA7\x92";
225#endif
226 err = std::ios_base::goodbit;
227 t = std::tm();
228 I i = f.get(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t, 'X');
229 assert(base(i) == in+sizeof(in)/sizeof(in[0])-1);
230 assert(t.tm_sec == 59);
231 assert(t.tm_min == 55);
232#if defined(_AIX)
233 assert(t.tm_hour == 11);
234#else
235 assert(t.tm_hour == 23);
236#endif
237 assert(err == std::ios_base::eofbit);
238 }
239
240 return 0;
241}
242

source code of libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_one.pass.cpp