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
198 iterator_type is_it01(iss);
201 tim_get.get_weekday(is_it01, end, iss, errorstate, &time01);
202 VERIFY( time01.tm_wday == time_bday.tm_wday );
203 VERIFY( errorstate == ios_base::eofbit );
206 iterator_type is_it02(iss);
209 tim_get.get_weekday(is_it02, end, iss, errorstate, &time02);
210 VERIFY( time02.tm_wday == time_bday.tm_wday );
211 VERIFY( errorstate == ios_base::eofbit );
214 iterator_type is_it03(iss);
217 tim_get.get_weekday(is_it03, end, iss, errorstate, &time03);
218 VERIFY( time03.tm_wday == time_bday.tm_wday );
219 VERIFY( errorstate == good );
220 VERIFY( *is_it03 == ' ');
223 iterator_type is_it04(iss);
227 tim_get.get_weekday(is_it04, end, iss, errorstate, &time04);
228 VERIFY( time04.tm_wday == 4 );
229 VERIFY( *is_it04 == 'n');
230 VERIFY( errorstate == ios_base::failbit );
233 iterator_type is_it05(iss);
236 tim_get.get_weekday(is_it05, end, iss, errorstate, &time05);
237 VERIFY( time05.tm_wday == 2 );
238 VERIFY( errorstate == good );
239 VERIFY( *is_it05 == ' ');
241 iss.str("Tuesducky "); // Kind of like Fryday, without the swirls.
242 iterator_type is_it06(iss);
246 tim_get.get_weekday(is_it06, end, iss, errorstate, &time06);
247 VERIFY( time06.tm_wday == 4 );
248 VERIFY( errorstate == ios_base::failbit );
249 VERIFY( *is_it05 == 'u');
251 // inspection of named locales, de_DE
254 iterator_type is_it10(iss);
257 tim_get.get_weekday(is_it10, end, iss, errorstate, &time10);
258 VERIFY( time10.tm_wday == time_bday.tm_wday );
259 VERIFY( errorstate == ios_base::eofbit );
261 // inspection of named locales, en_HK
264 iterator_type is_it20(iss);
267 tim_get.get_weekday(is_it20, end, iss, errorstate, &time20);
268 VERIFY( time20.tm_wday == time_bday.tm_wday );
269 VERIFY( errorstate == ios_base::eofbit );
275 typedef time_base::dateorder dateorder;
276 typedef istreambuf_iterator<char> iterator_type;
280 // basic construction and sanity checks.
281 locale loc_c = locale::classic();
282 locale loc_hk("en_HK");
283 locale loc_fr("fr_FR@euro");
284 locale loc_de("de_DE");
285 VERIFY( loc_hk != loc_c );
286 VERIFY( loc_hk != loc_fr );
287 VERIFY( loc_hk != loc_de );
288 VERIFY( loc_de != loc_fr );
290 // cache the __timepunct facets, for quicker gdb inspection
291 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
292 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
293 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
294 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
298 // create an ostream-derived object, cache the time_get facet
302 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
304 const ios_base::iostate good = ios_base::goodbit;
305 ios_base::iostate errorstate = good;
307 // create "C" time objects
308 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
309 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
310 "%w %W %x %X %y %Y %Z %%";
311 const char* date = "%A, the second of %B";
312 const char* date_ex = "%Ex";
315 // get_monthname(iter_type, iter_type, ios_base&,
316 // ios_base::iostate&, tm*) const
318 // sanity checks for "C" locale
321 iterator_type is_it01(iss);
324 tim_get.get_monthname(is_it01, end, iss, errorstate, &time01);
325 VERIFY( time01.tm_wday == time_bday.tm_wday );
326 VERIFY( errorstate == ios_base::eofbit );
329 iterator_type is_it02(iss);
332 tim_get.get_monthname(is_it02, end, iss, errorstate, &time02);
333 VERIFY( time02.tm_mon == time_bday.tm_mon );
334 VERIFY( errorstate == ios_base::eofbit );
337 iterator_type is_it03(iss);
340 tim_get.get_monthname(is_it03, end, iss, errorstate, &time03);
341 VERIFY( time03.tm_mon == time_bday.tm_mon );
342 VERIFY( errorstate == good );
343 VERIFY( *is_it03 == ' ');
346 iterator_type is_it04(iss);
350 tim_get.get_monthname(is_it04, end, iss, errorstate, &time04);
351 VERIFY( time04.tm_mon == 5 );
352 VERIFY( *is_it04 == 'a');
353 VERIFY( errorstate == ios_base::failbit );
355 iss.str("December ");
356 iterator_type is_it05(iss);
359 tim_get.get_monthname(is_it05, end, iss, errorstate, &time05);
360 VERIFY( time05.tm_mon == 11 );
361 VERIFY( errorstate == good );
362 VERIFY( *is_it05 == ' ');
364 iss.str("Decelember ");
365 iterator_type is_it06(iss);
369 tim_get.get_monthname(is_it06, end, iss, errorstate, &time06);
370 VERIFY( time06.tm_mon == 4 );
371 VERIFY( errorstate == ios_base::failbit );
372 VERIFY( *is_it05 == 'l');
374 // inspection of named locales, de_DE
377 iterator_type is_it10(iss);
380 tim_get.get_monthname(is_it10, end, iss, errorstate, &time10);
381 VERIFY( time10.tm_mon == time_bday.tm_mon );
382 VERIFY( errorstate == ios_base::eofbit );
384 // inspection of named locales, en_HK
387 iterator_type is_it20(iss);
390 tim_get.get_monthname(is_it20, end, iss, errorstate, &time20);
391 VERIFY( time20.tm_mon == time_bday.tm_mon );
392 VERIFY( errorstate == ios_base::eofbit );
398 typedef time_base::dateorder dateorder;
399 typedef istreambuf_iterator<char> iterator_type;
403 // basic construction and sanity checks.
404 locale loc_c = locale::classic();
405 locale loc_hk("en_HK");
406 locale loc_fr("fr_FR@euro");
407 locale loc_de("de_DE");
408 VERIFY( loc_hk != loc_c );
409 VERIFY( loc_hk != loc_fr );
410 VERIFY( loc_hk != loc_de );
411 VERIFY( loc_de != loc_fr );
413 // cache the __timepunct facets, for quicker gdb inspection
414 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
415 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
416 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
417 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
421 // create an ostream-derived object, cache the time_get facet
425 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
427 const ios_base::iostate good = ios_base::goodbit;
428 ios_base::iostate errorstate = good;
430 // create "C" time objects
431 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
432 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
433 "%w %W %x %X %y %Y %Z %%";
434 const char* date = "%A, the second of %B";
435 const char* date_ex = "%Ex";
438 // get_year(iter_type, iter_type, ios_base&, ios_base::iostate&, tm*) const
440 // sanity checks for "C" locale
443 iterator_type is_it01(iss);
446 tim_get.get_year(is_it01, end, iss, errorstate, &time01);
447 VERIFY( time01.tm_year == time_bday.tm_year );
448 VERIFY( errorstate == ios_base::eofbit );
451 iterator_type is_it02(iss);
454 tim_get.get_year(is_it02, end, iss, errorstate, &time02);
455 VERIFY( time02.tm_year == time_bday.tm_year );
456 VERIFY( errorstate == good );
457 VERIFY( *is_it02 == ' ');
460 iterator_type is_it03(iss);
464 tim_get.get_year(is_it03, end, iss, errorstate, &time03);
465 VERIFY( time03.tm_year == 3 );
466 VERIFY( errorstate == ios_base::failbit );
467 VERIFY( *is_it03 == 'd');
470 iterator_type is_it04(iss);
473 tim_get.get_year(is_it04, end, iss, errorstate, &time04);
474 VERIFY( time04.tm_year == time_bday.tm_year );
475 VERIFY( errorstate == good );
476 VERIFY( *is_it03 == 'd');
479 iterator_type is_it05(iss);
482 tim_get.get_year(is_it05, end, iss, errorstate, &time05);
483 VERIFY( time05.tm_year == time_bday.tm_year );
484 VERIFY( errorstate == ios_base::eofbit );
490 typedef time_base::dateorder dateorder;
491 typedef istreambuf_iterator<char> iterator_type;
495 // basic construction and sanity checks.
496 locale loc_c = locale::classic();
497 locale loc_hk("en_HK");
498 locale loc_fr("fr_FR@euro");
499 locale loc_de("de_DE");
500 VERIFY( loc_hk != loc_c );
501 VERIFY( loc_hk != loc_fr );
502 VERIFY( loc_hk != loc_de );
503 VERIFY( loc_de != loc_fr );
505 // cache the __timepunct facets, for quicker gdb inspection
506 const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
507 const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
508 const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
509 const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
513 // create an ostream-derived object, cache the time_get facet
517 const time_get<char>& tim_get = use_facet<time_get<char> >(iss.getloc());
519 const ios_base::iostate good = ios_base::goodbit;
520 ios_base::iostate errorstate = good;
522 // create "C" time objects
523 const tm time_bday = { 0, 0, 12, 4, 3, 71 };
524 const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
525 "%w %W %x %X %y %Y %Z %%";
526 const char* date = "%A, the second of %B";
527 const char* date_ex = "%Ex";
530 // get_date(iter_type, iter_type, ios_base&, ios_base::iostate&, tm*) const
532 // sanity checks for "C" locale
535 iterator_type is_it01(iss);
538 tim_get.get_date(is_it01, end, iss, errorstate, &time01);
539 VERIFY( time01.tm_year == time_bday.tm_year );
540 VERIFY( time01.tm_mon == time_bday.tm_mon );
541 VERIFY( time01.tm_mday == time_bday.tm_mday );
542 VERIFY( errorstate == ios_base::eofbit );
544 iss.str("04/04/71 ");
545 iterator_type is_it02(iss);
548 tim_get.get_date(is_it02, end, iss, errorstate, &time02);
549 VERIFY( time02.tm_year == time_bday.tm_year );
550 VERIFY( time02.tm_mon == time_bday.tm_mon );
551 VERIFY( time02.tm_mday == time_bday.tm_mday );
552 VERIFY( errorstate == good );
553 VERIFY( *is_it02 == ' ');
555 iss.str("04/04d/71 ");
556 iterator_type is_it03(iss);
560 tim_get.get_date(is_it03, end, iss, errorstate, &time03);
561 VERIFY( time03.tm_year == 3 );
562 VERIFY( time03.tm_mon == time_bday.tm_mon );
563 VERIFY( time03.tm_mday == time_bday.tm_mday );
564 VERIFY( errorstate == ios_base::failbit );
565 VERIFY( *is_it03 == 'd');
567 // inspection of named locales, de_DE
569 iss.str("04.04.1971");
570 iterator_type is_it10(iss);
573 tim_get.get_date(is_it10, end, iss, errorstate, &time10);
574 VERIFY( time10.tm_mon == time_bday.tm_mon );
575 VERIFY( time10.tm_mday == time_bday.tm_mday );
576 VERIFY( time10.tm_year == time_bday.tm_year );
577 VERIFY( errorstate == ios_base::eofbit );
579 // inspection of named locales, en_HK
581 iss.str("Sunday, April 04, 1971");
582 iterator_type is_it20(iss);
585 tim_get.get_date(is_it20, end, iss, errorstate, &time20);
586 VERIFY( time20.tm_mon == time_bday.tm_mon );
587 VERIFY( time20.tm_mday == time_bday.tm_mday );
588 VERIFY( time20.tm_year == time_bday.tm_year );
589 VERIFY( errorstate == ios_base::eofbit );
597 // Check time_get works with other iterators besides streambuf
599 typedef string::const_iterator iter_type;
600 typedef time_get<char, iter_type> time_get_type;
601 const ios_base::iostate goodbit = ios_base::goodbit;
602 const ios_base::iostate eofbit = ios_base::eofbit;
603 ios_base::iostate err = goodbit;
604 const locale loc_c = locale::classic();
605 // Cindy Sherman's Untitled Film Stills
606 // June 26-September 2, 1997
607 const string str = "12:00:00 06/26/97 Tuesday September 1997 Cindy Sherman";
609 // Create "C" time objects
610 const tm time_sanity = { 0, 0, 12, 26, 5, 97, 2 };
614 iss.imbue(locale(loc_c, new time_get_type));
616 // Iterator advanced, state, output.
617 const time_get_type& tg = use_facet<time_get_type>(iss.getloc());
628 iter_type end1 = tg.get_time(str.begin(), str.end(), iss, err, &tm1);
629 string rem1(end1, str.end());
630 VERIFY( err == goodbit );
631 VERIFY( tm1.tm_sec == time_sanity.tm_sec );
632 VERIFY( tm1.tm_min == time_sanity.tm_min );
633 VERIFY( tm1.tm_hour == time_sanity.tm_hour );
634 VERIFY( rem1 == " 06/26/97 Tuesday September 1997 Cindy Sherman" );
639 // White space is not eaten, so manually increment past it.
640 iter_type end2 = tg.get_date(++end1, str.end(), iss, err, &tm1);
641 string rem2(end2, str.end());
642 VERIFY( err == goodbit );
643 VERIFY( tm1.tm_year == time_sanity.tm_year );
644 VERIFY( tm1.tm_mon == time_sanity.tm_mon );
645 VERIFY( tm1.tm_mday == time_sanity.tm_mday );
646 VERIFY( rem2 == " Tuesday September 1997 Cindy Sherman" );
651 // White space is not eaten, so manually increment past it.
652 iter_type end3 = tg.get_weekday(++end2, str.end(), iss, err, &tm1);
653 string rem3(end3, str.end());
654 VERIFY( err == goodbit );
655 VERIFY( tm1.tm_wday == time_sanity.tm_wday );
656 VERIFY( rem3 == " September 1997 Cindy Sherman" );
661 // White space is not eaten, so manually increment past it.
662 iter_type end4 = tg.get_monthname(++end3, str.end(), iss, err, &tm1);
663 string rem4(end4, str.end());
664 VERIFY( err == goodbit );
665 VERIFY( tm1.tm_mon == 8 );
666 VERIFY( rem4 == " 1997 Cindy Sherman" );
671 // White space is not eaten, so manually increment past it.
672 iter_type end5 = tg.get_year(++end4, str.end(), iss, err, &tm1);
673 string rem5(end5, str.end());
674 VERIFY( err == goodbit );
675 VERIFY( tm1.tm_year == time_sanity.tm_year );
676 VERIFY( rem5 == " Cindy Sherman" );
682 #ifdef _GLIBCPP_HAVE_SETENV
683 // Set the global locale to non-"C".
684 std::locale loc_de("de_DE");
685 std::locale::global(loc_de);
687 // Set LANG environment variable to de_DE.
688 const char* oldLANG = getenv("LANG");
689 if (!setenv("LANG", "de_DE", 1))
697 setenv("LANG", oldLANG ? oldLANG : "", 1);