1 // 2001-09-21 Benjamin Kosnik <bkoz@redhat.com>
3 // Copyright (C) 2001, 2002 Free Software Foundation
5 // This file is part of the GNU ISO C++ Library. This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 // 22.2.5.1.1 time_get members
25 #include <testsuite_hooks.h>
27 // XXX This test is not working for non-glibc locale models.
28 // { dg-do run { xfail *-*-* } }
33 typedef time_base::dateorder dateorder;
34 typedef istreambuf_iterator<char> iterator_type;
38 // basic construction and sanity checks.
39 locale loc_c = locale::classic();
40 locale loc_hk("en_HK");
41 locale loc_fr("fr_FR@euro");
42 locale loc_de("de_DE");
43 VERIFY( loc_hk != loc_c );
44 VERIFY( loc_hk != loc_fr );
45 VERIFY( loc_hk != loc_de );
46 VERIFY( loc_de != loc_fr );
48 // cache the __timepunct facets, for quicker gdb inspection
49 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
50 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
51 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
52 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
56 // create an ostream-derived object, cache the time_get facet
60 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
62 const ios_base::iostate good = ios_base::goodbit;
63 ios_base::iostate errorstate = good;
65 // create "C" time objects
66 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
67 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
68 "%w %W %x %X %y %Y %Z %%";
69 const char* date = "%A, the second of %B";
70 const char* date_ex = "%Ex";
73 // dateorder date_order() const
75 dateorder do1 = tim_get.date_order();
76 // VERIFY( do1 == time_base::mdy );
77 VERIFY( do1 == time_base::no_order );
81 // get_time(iter_type, iter_type, ios_base&, ios_base::iostate&, tm*) const
83 // sanity checks for "C" locale
85 iterator_type is_it01(iss);
88 tim_get.get_time(is_it01, end, iss, errorstate, &time01);
89 VERIFY( time01.tm_sec == time_bday.tm_sec );
90 VERIFY( time01.tm_min == time_bday.tm_min );
91 VERIFY( time01.tm_hour == time_bday.tm_hour );
92 VERIFY( errorstate == ios_base::eofbit );
95 iterator_type is_it02(iss);
98 tim_get.get_time(is_it02, end, iss, errorstate, &time02);
99 VERIFY( time01.tm_sec == time_bday.tm_sec );
100 VERIFY( time01.tm_min == time_bday.tm_min );
101 VERIFY( time01.tm_hour == time_bday.tm_hour );
102 VERIFY( errorstate == good );
104 iss.str("12:61:00 ");
105 iterator_type is_it03(iss);
108 tim_get.get_time(is_it03, end, iss, errorstate, &time03);
109 VERIFY( time01.tm_hour == time_bday.tm_hour );
110 VERIFY( errorstate == ios_base::failbit );
113 iterator_type is_it04(iss);
116 tim_get.get_time(is_it04, end, iss, errorstate, &time04);
117 VERIFY( time01.tm_hour == time_bday.tm_hour );
118 VERIFY( *is_it04 == 'a');
119 VERIFY( errorstate == ios_base::failbit );
121 // inspection of named locales, de_DE
124 iterator_type is_it10(iss);
127 tim_get.get_time(is_it10, end, iss, errorstate, &time10);
128 VERIFY( time10.tm_sec == time_bday.tm_sec );
129 VERIFY( time10.tm_min == time_bday.tm_min );
130 VERIFY( time10.tm_hour == time_bday.tm_hour );
131 VERIFY( errorstate == ios_base::eofbit );
133 // inspection of named locales, en_HK
135 iss.str("12:00:00 PST");
136 // Hong Kong in California! Well, they have Paris in Vegas... this
137 // is all a little disney-esque anyway. Besides, you can get decent
138 // Dim Sum in San Francisco.
139 iterator_type is_it20(iss);
142 tim_get.get_time(is_it20, end, iss, errorstate, &time20);
143 VERIFY( time10.tm_sec == time_bday.tm_sec );
144 VERIFY( time10.tm_min == time_bday.tm_min );
145 VERIFY( time10.tm_hour == time_bday.tm_hour );
146 VERIFY( errorstate == ios_base::eofbit );
152 typedef time_base::dateorder dateorder;
153 typedef istreambuf_iterator<char> iterator_type;
157 // basic construction and sanity checks.
158 locale loc_c = locale::classic();
159 locale loc_hk("en_HK");
160 locale loc_fr("fr_FR@euro");
161 locale loc_de("de_DE");
162 VERIFY( loc_hk != loc_c );
163 VERIFY( loc_hk != loc_fr );
164 VERIFY( loc_hk != loc_de );
165 VERIFY( loc_de != loc_fr );
167 // cache the __timepunct facets, for quicker gdb inspection
168 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
169 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
170 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
171 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
175 // create an ostream-derived object, cache the time_get facet
179 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
181 const ios_base::iostate good = ios_base::goodbit;
182 ios_base::iostate errorstate = good;
184 // create "C" time objects
185 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
186 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
187 "%w %W %x %X %y %Y %Z %%";
188 const char* date = "%A, the second of %B";
189 const char* date_ex = "%Ex";
192 // get_weekday(iter_type, iter_type, ios_base&,
193 // ios_base::iostate&, tm*) const
195 // sanity checks for "C" locale
197 iterator_type is_it01(iss);
200 tim_get.get_weekday(is_it01, end, iss, errorstate, &time01);
201 VERIFY( time01.tm_wday == time_bday.tm_wday );
202 VERIFY( errorstate == ios_base::eofbit );
205 iterator_type is_it02(iss);
208 tim_get.get_weekday(is_it02, end, iss, errorstate, &time02);
209 VERIFY( time02.tm_wday == time_bday.tm_wday );
210 VERIFY( errorstate == ios_base::eofbit );
213 iterator_type is_it03(iss);
216 tim_get.get_weekday(is_it03, end, iss, errorstate, &time03);
217 VERIFY( time03.tm_wday == time_bday.tm_wday );
218 VERIFY( errorstate == good );
219 VERIFY( *is_it03 == ' ');
222 iterator_type is_it04(iss);
226 tim_get.get_weekday(is_it04, end, iss, errorstate, &time04);
227 VERIFY( time04.tm_wday == 4 );
228 VERIFY( *is_it04 == 'n');
229 VERIFY( errorstate == ios_base::failbit );
232 iterator_type is_it05(iss);
235 tim_get.get_weekday(is_it05, end, iss, errorstate, &time05);
236 VERIFY( time05.tm_wday == 2 );
237 VERIFY( errorstate == good );
238 VERIFY( *is_it05 == ' ');
240 iss.str("Tuesducky "); // Kind of like Fryday, without the swirls.
241 iterator_type is_it06(iss);
245 tim_get.get_weekday(is_it06, end, iss, errorstate, &time06);
246 VERIFY( time06.tm_wday == 4 );
247 VERIFY( errorstate == ios_base::failbit );
248 VERIFY( *is_it05 == 'u');
250 // inspection of named locales, de_DE
253 iterator_type is_it10(iss);
256 tim_get.get_weekday(is_it10, end, iss, errorstate, &time10);
257 VERIFY( time10.tm_wday == time_bday.tm_wday );
258 VERIFY( errorstate == ios_base::eofbit );
260 // inspection of named locales, en_HK
263 iterator_type is_it20(iss);
266 tim_get.get_weekday(is_it20, end, iss, errorstate, &time20);
267 VERIFY( time20.tm_wday == time_bday.tm_wday );
268 VERIFY( errorstate == ios_base::eofbit );
274 typedef time_base::dateorder dateorder;
275 typedef istreambuf_iterator<char> iterator_type;
279 // basic construction and sanity checks.
280 locale loc_c = locale::classic();
281 locale loc_hk("en_HK");
282 locale loc_fr("fr_FR@euro");
283 locale loc_de("de_DE");
284 VERIFY( loc_hk != loc_c );
285 VERIFY( loc_hk != loc_fr );
286 VERIFY( loc_hk != loc_de );
287 VERIFY( loc_de != loc_fr );
289 // cache the __timepunct facets, for quicker gdb inspection
290 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
291 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
292 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
293 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
297 // create an ostream-derived object, cache the time_get facet
301 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
303 const ios_base::iostate good = ios_base::goodbit;
304 ios_base::iostate errorstate = good;
306 // create "C" time objects
307 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
308 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
309 "%w %W %x %X %y %Y %Z %%";
310 const char* date = "%A, the second of %B";
311 const char* date_ex = "%Ex";
314 // get_monthname(iter_type, iter_type, ios_base&,
315 // ios_base::iostate&, tm*) const
317 // sanity checks for "C" locale
319 iterator_type is_it01(iss);
322 tim_get.get_monthname(is_it01, end, iss, errorstate, &time01);
323 VERIFY( time01.tm_wday == time_bday.tm_wday );
324 VERIFY( errorstate == ios_base::eofbit );
327 iterator_type is_it02(iss);
330 tim_get.get_monthname(is_it02, end, iss, errorstate, &time02);
331 VERIFY( time02.tm_mon == time_bday.tm_mon );
332 VERIFY( errorstate == ios_base::eofbit );
335 iterator_type is_it03(iss);
338 tim_get.get_monthname(is_it03, end, iss, errorstate, &time03);
339 VERIFY( time03.tm_mon == time_bday.tm_mon );
340 VERIFY( errorstate == good );
341 VERIFY( *is_it03 == ' ');
344 iterator_type is_it04(iss);
348 tim_get.get_monthname(is_it04, end, iss, errorstate, &time04);
349 VERIFY( time04.tm_mon == 5 );
350 VERIFY( *is_it04 == 'a');
351 VERIFY( errorstate == ios_base::failbit );
353 iss.str("December ");
354 iterator_type is_it05(iss);
357 tim_get.get_monthname(is_it05, end, iss, errorstate, &time05);
358 VERIFY( time05.tm_mon == 11 );
359 VERIFY( errorstate == good );
360 VERIFY( *is_it05 == ' ');
362 iss.str("Decelember ");
363 iterator_type is_it06(iss);
367 tim_get.get_monthname(is_it06, end, iss, errorstate, &time06);
368 VERIFY( time06.tm_mon == 4 );
369 VERIFY( errorstate == ios_base::failbit );
370 VERIFY( *is_it05 == 'l');
372 // inspection of named locales, de_DE
375 iterator_type is_it10(iss);
378 tim_get.get_monthname(is_it10, end, iss, errorstate, &time10);
379 VERIFY( time10.tm_mon == time_bday.tm_mon );
380 VERIFY( errorstate == ios_base::eofbit );
382 // inspection of named locales, en_HK
385 iterator_type is_it20(iss);
388 tim_get.get_monthname(is_it20, end, iss, errorstate, &time20);
389 VERIFY( time20.tm_mon == time_bday.tm_mon );
390 VERIFY( errorstate == ios_base::eofbit );
396 typedef time_base::dateorder dateorder;
397 typedef istreambuf_iterator<char> iterator_type;
401 // basic construction and sanity checks.
402 locale loc_c = locale::classic();
403 locale loc_hk("en_HK");
404 locale loc_fr("fr_FR@euro");
405 locale loc_de("de_DE");
406 VERIFY( loc_hk != loc_c );
407 VERIFY( loc_hk != loc_fr );
408 VERIFY( loc_hk != loc_de );
409 VERIFY( loc_de != loc_fr );
411 // cache the __timepunct facets, for quicker gdb inspection
412 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
413 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
414 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
415 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
419 // create an ostream-derived object, cache the time_get facet
423 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
425 const ios_base::iostate good = ios_base::goodbit;
426 ios_base::iostate errorstate = good;
428 // create "C" time objects
429 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
430 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
431 "%w %W %x %X %y %Y %Z %%";
432 const char* date = "%A, the second of %B";
433 const char* date_ex = "%Ex";
436 // get_year(iter_type, iter_type, ios_base&, ios_base::iostate&, tm*) const
438 // sanity checks for "C" locale
440 iterator_type is_it01(iss);
443 tim_get.get_year(is_it01, end, iss, errorstate, &time01);
444 VERIFY( time01.tm_year == time_bday.tm_year );
445 VERIFY( errorstate == ios_base::eofbit );
448 iterator_type is_it02(iss);
451 tim_get.get_year(is_it02, end, iss, errorstate, &time02);
452 VERIFY( time02.tm_year == time_bday.tm_year );
453 VERIFY( errorstate == good );
454 VERIFY( *is_it02 == ' ');
457 iterator_type is_it03(iss);
461 tim_get.get_year(is_it03, end, iss, errorstate, &time03);
462 VERIFY( time03.tm_year == 3 );
463 VERIFY( errorstate == ios_base::failbit );
464 VERIFY( *is_it03 == 'd');
467 iterator_type is_it04(iss);
470 tim_get.get_year(is_it04, end, iss, errorstate, &time04);
471 VERIFY( time04.tm_year == time_bday.tm_year );
472 VERIFY( errorstate == good );
473 VERIFY( *is_it03 == 'd');
476 iterator_type is_it05(iss);
479 tim_get.get_year(is_it05, end, iss, errorstate, &time05);
480 VERIFY( time05.tm_year == time_bday.tm_year );
481 VERIFY( errorstate == ios_base::eofbit );
487 typedef time_base::dateorder dateorder;
488 typedef istreambuf_iterator<char> iterator_type;
492 // basic construction and sanity checks.
493 locale loc_c = locale::classic();
494 locale loc_hk("en_HK");
495 locale loc_fr("fr_FR@euro");
496 locale loc_de("de_DE");
497 VERIFY( loc_hk != loc_c );
498 VERIFY( loc_hk != loc_fr );
499 VERIFY( loc_hk != loc_de );
500 VERIFY( loc_de != loc_fr );
502 // cache the __timepunct facets, for quicker gdb inspection
503 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
504 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
505 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
506 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
510 // create an ostream-derived object, cache the time_get facet
514 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
516 const ios_base::iostate good = ios_base::goodbit;
517 ios_base::iostate errorstate = good;
519 // create "C" time objects
520 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
521 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
522 "%w %W %x %X %y %Y %Z %%";
523 const char* date = "%A, the second of %B";
524 const char* date_ex = "%Ex";
527 // get_date(iter_type, iter_type, ios_base&, ios_base::iostate&, tm*) const
529 // sanity checks for "C" locale
531 iterator_type is_it01(iss);
534 tim_get.get_date(is_it01, end, iss, errorstate, &time01);
535 VERIFY( time01.tm_year == time_bday.tm_year );
536 VERIFY( time01.tm_mon == time_bday.tm_mon );
537 VERIFY( time01.tm_mday == time_bday.tm_mday );
538 VERIFY( errorstate == ios_base::eofbit );
540 iss.str("04/04/71 ");
541 iterator_type is_it02(iss);
544 tim_get.get_date(is_it02, end, iss, errorstate, &time02);
545 VERIFY( time02.tm_year == time_bday.tm_year );
546 VERIFY( time02.tm_mon == time_bday.tm_mon );
547 VERIFY( time02.tm_mday == time_bday.tm_mday );
548 VERIFY( errorstate == good );
549 VERIFY( *is_it02 == ' ');
551 iss.str("04/04d/71 ");
552 iterator_type is_it03(iss);
556 tim_get.get_date(is_it03, end, iss, errorstate, &time03);
557 VERIFY( time03.tm_year == 3 );
558 VERIFY( time03.tm_mon == time_bday.tm_mon );
559 VERIFY( time03.tm_mday == time_bday.tm_mday );
560 VERIFY( errorstate == ios_base::failbit );
561 VERIFY( *is_it03 == 'd');
563 // inspection of named locales, de_DE
565 iss.str("04.04.1971");
566 iterator_type is_it10(iss);
569 tim_get.get_date(is_it10, end, iss, errorstate, &time10);
570 VERIFY( time10.tm_mon == time_bday.tm_mon );
571 VERIFY( time10.tm_mday == time_bday.tm_mday );
572 VERIFY( time10.tm_year == time_bday.tm_year );
573 VERIFY( errorstate == ios_base::eofbit );
575 // inspection of named locales, en_HK
577 iss.str("Sunday, April 04, 1971");
578 iterator_type is_it20(iss);
581 tim_get.get_date(is_it20, end, iss, errorstate, &time20);
582 VERIFY( time20.tm_mon == time_bday.tm_mon );
583 VERIFY( time20.tm_mday == time_bday.tm_mday );
584 VERIFY( time20.tm_year == time_bday.tm_year );
585 VERIFY( errorstate == ios_base::eofbit );
593 // Check time_get works with other iterators besides streambuf
595 typedef string::const_iterator iter_type;
596 typedef time_get<char, iter_type> time_get_type;
597 const ios_base::iostate goodbit = ios_base::goodbit;
598 const ios_base::iostate eofbit = ios_base::eofbit;
599 ios_base::iostate err = goodbit;
600 const locale loc_c = locale::classic();
601 // Cindy Sherman's Untitled Film Stills
602 // June 26-September 2, 1997
603 const string str = "12:00:00 06/26/97 Tuesday September 1997 Cindy Sherman";
605 // Create "C" time objects
606 const tm time_sanity = { 0, 0, 12, 26, 5, 97, 2 };
610 iss.imbue(locale(loc_c, new time_get_type));
612 // Iterator advanced, state, output.
613 const time_get_type& tg = use_facet<time_get_type>(iss.getloc());
624 iter_type end1 = tg.get_time(str.begin(), str.end(), iss, err, &tm1);
625 string rem1(end1, str.end());
626 VERIFY( err == goodbit );
627 VERIFY( tm1.tm_sec == time_sanity.tm_sec );
628 VERIFY( tm1.tm_min == time_sanity.tm_min );
629 VERIFY( tm1.tm_hour == time_sanity.tm_hour );
630 VERIFY( rem1 == " 06/26/97 Tuesday September 1997 Cindy Sherman" );
635 // White space is not eaten, so manually increment past it.
636 iter_type end2 = tg.get_date(++end1, str.end(), iss, err, &tm1);
637 string rem2(end2, str.end());
638 VERIFY( err == goodbit );
639 VERIFY( tm1.tm_year == time_sanity.tm_year );
640 VERIFY( tm1.tm_mon == time_sanity.tm_mon );
641 VERIFY( tm1.tm_mday == time_sanity.tm_mday );
642 VERIFY( rem2 == " Tuesday September 1997 Cindy Sherman" );
647 // White space is not eaten, so manually increment past it.
648 iter_type end3 = tg.get_weekday(++end2, str.end(), iss, err, &tm1);
649 string rem3(end3, str.end());
650 VERIFY( err == goodbit );
651 VERIFY( tm1.tm_wday == time_sanity.tm_wday );
652 VERIFY( rem3 == " September 1997 Cindy Sherman" );
657 // White space is not eaten, so manually increment past it.
658 iter_type end4 = tg.get_monthname(++end3, str.end(), iss, err, &tm1);
659 string rem4(end4, str.end());
660 VERIFY( err == goodbit );
661 VERIFY( tm1.tm_mon == 8 );
662 VERIFY( rem4 == " 1997 Cindy Sherman" );
667 // White space is not eaten, so manually increment past it.
668 iter_type end5 = tg.get_year(++end4, str.end(), iss, err, &tm1);
669 string rem5(end5, str.end());
670 VERIFY( err == goodbit );
671 VERIFY( tm1.tm_year == time_sanity.tm_year );
672 VERIFY( rem5 == " Cindy Sherman" );