103a78d15Sespie // 2001-09-17 Benjamin Kosnik  <bkoz@redhat.com>
203a78d15Sespie 
3*19731d4fSespie // Copyright (C) 2001, 2002, 2004 Free Software Foundation
403a78d15Sespie //
503a78d15Sespie // This file is part of the GNU ISO C++ Library.  This library is free
603a78d15Sespie // software; you can redistribute it and/or modify it under the
703a78d15Sespie // terms of the GNU General Public License as published by the
803a78d15Sespie // Free Software Foundation; either version 2, or (at your option)
903a78d15Sespie // any later version.
1003a78d15Sespie 
1103a78d15Sespie // This library is distributed in the hope that it will be useful,
1203a78d15Sespie // but WITHOUT ANY WARRANTY; without even the implied warranty of
1303a78d15Sespie // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1403a78d15Sespie // GNU General Public License for more details.
1503a78d15Sespie 
1603a78d15Sespie // You should have received a copy of the GNU General Public License along
1703a78d15Sespie // with this library; see the file COPYING.  If not, write to the Free
1803a78d15Sespie // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1903a78d15Sespie // USA.
2003a78d15Sespie 
2103a78d15Sespie // 22.2.5.3.1 time_put members
2203a78d15Sespie 
2303a78d15Sespie #include <locale>
2403a78d15Sespie #include <sstream>
2503a78d15Sespie #include <testsuite_hooks.h>
2603a78d15Sespie 
2703a78d15Sespie // XXX This test is not working for non-glibc locale models.
2803a78d15Sespie // { dg-do run { xfail *-*-* } }
2903a78d15Sespie 
test01()3003a78d15Sespie void test01()
3103a78d15Sespie {
3203a78d15Sespie   using namespace std;
3303a78d15Sespie   typedef ostreambuf_iterator<char> iterator_type;
3403a78d15Sespie 
3503a78d15Sespie   bool test = true;
3603a78d15Sespie 
3703a78d15Sespie   // basic construction and sanity checks.
3803a78d15Sespie   locale loc_c = locale::classic();
3903a78d15Sespie   locale loc_hk("en_HK");
4003a78d15Sespie   locale loc_fr("fr_FR@euro");
4103a78d15Sespie   locale loc_de("de_DE");
4203a78d15Sespie   VERIFY( loc_hk != loc_c );
4303a78d15Sespie   VERIFY( loc_hk != loc_fr );
4403a78d15Sespie   VERIFY( loc_hk != loc_de );
4503a78d15Sespie   VERIFY( loc_de != loc_fr );
4603a78d15Sespie 
4703a78d15Sespie   // cache the __timepunct facets, for quicker gdb inspection
4803a78d15Sespie   const __timepunct<char>& time_c = use_facet<__timepunct<char> >(loc_c);
4903a78d15Sespie   const __timepunct<char>& time_de = use_facet<__timepunct<char> >(loc_de);
5003a78d15Sespie   const __timepunct<char>& time_hk = use_facet<__timepunct<char> >(loc_hk);
5103a78d15Sespie   const __timepunct<char>& time_fr = use_facet<__timepunct<char> >(loc_fr);
5203a78d15Sespie 
5303a78d15Sespie   // create an ostream-derived object, cache the time_put facet
5403a78d15Sespie   const string empty;
5503a78d15Sespie   ostringstream oss;
5603a78d15Sespie   const time_put<char>& tim_put = use_facet<time_put<char> >(oss.getloc());
5703a78d15Sespie 
5803a78d15Sespie   // create "C" time objects
5903a78d15Sespie   tm time1 = { 0, 0, 12, 4, 3, 71 };
6003a78d15Sespie   const char* all = "%a %A %b %B %c %d %H %I %j %m %M %p %s %U "
6103a78d15Sespie                     "%w %W %x %X %y %Y %Z %%";
6203a78d15Sespie   const char* date = "%A, the second of %B";
6303a78d15Sespie   const char* date_ex = "%Ex";
6403a78d15Sespie 
6503a78d15Sespie   // 1
6603a78d15Sespie   // iter_type
6703a78d15Sespie   // put(iter_type s, ios_base& str, char_type fill, const tm* t,
6803a78d15Sespie   //	 char format, char modifier = 0) const;
6903a78d15Sespie   oss.str(empty);
7003a78d15Sespie   oss.imbue(loc_c);
7103a78d15Sespie   iterator_type os_it01 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
7203a78d15Sespie   string result1 = oss.str();
7303a78d15Sespie   VERIFY( result1 == "Sun" );
7403a78d15Sespie 
7503a78d15Sespie   oss.str(empty);
7603a78d15Sespie   iterator_type os_it21 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
7703a78d15Sespie   string result21 = oss.str(); // "04/04/71"
7803a78d15Sespie   oss.str(empty);
7903a78d15Sespie   iterator_type os_it22 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
8003a78d15Sespie   string result22 = oss.str(); // "12:00:00"
8103a78d15Sespie   oss.str(empty);
8203a78d15Sespie   iterator_type os_it31 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
8303a78d15Sespie   string result31 = oss.str(); // "04/04/71"
8403a78d15Sespie   oss.str(empty);
8503a78d15Sespie   iterator_type os_it32 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
8603a78d15Sespie   string result32 = oss.str(); // "12:00:00"
8703a78d15Sespie 
8803a78d15Sespie   oss.str(empty);
8903a78d15Sespie   oss.imbue(loc_de);
9003a78d15Sespie   iterator_type os_it02 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
9103a78d15Sespie   string result2 = oss.str();
92*19731d4fSespie   VERIFY( result2 == "Son" || result2 == "So" );
9303a78d15Sespie 
9403a78d15Sespie   oss.str(empty); // "%d.%m.%Y"
9503a78d15Sespie   iterator_type os_it23 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
9603a78d15Sespie   string result23 = oss.str(); // "04.04.1971"
9703a78d15Sespie   oss.str(empty); // "%T"
9803a78d15Sespie   iterator_type os_it24 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
9903a78d15Sespie   string result24 = oss.str(); // "12:00:00"
10003a78d15Sespie   oss.str(empty);
10103a78d15Sespie   iterator_type os_it33 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
10203a78d15Sespie   string result33 = oss.str(); // "04.04.1971"
10303a78d15Sespie   oss.str(empty);
10403a78d15Sespie   iterator_type os_it34 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
10503a78d15Sespie   string result34 = oss.str(); // "12:00:00"
10603a78d15Sespie 
10703a78d15Sespie   oss.str(empty);
10803a78d15Sespie   oss.imbue(loc_hk);
10903a78d15Sespie   iterator_type os_it03 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
11003a78d15Sespie   string result3 = oss.str();
11103a78d15Sespie   VERIFY( result3 == "Sun" );
11203a78d15Sespie 
11303a78d15Sespie   oss.str(empty); // "%A, %B %d, %Y"
11403a78d15Sespie   iterator_type os_it25 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
11503a78d15Sespie   string result25 = oss.str(); // "Sunday, April 04, 1971"
11603a78d15Sespie   oss.str(empty); // "%I:%M:%S %Z"
11703a78d15Sespie   iterator_type os_it26 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
11803a78d15Sespie   string result26 = oss.str(); // "12:00:00 PST"
11903a78d15Sespie   oss.str(empty);
12003a78d15Sespie   iterator_type os_it35 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
12103a78d15Sespie   string result35 = oss.str(); // "Sunday, April 04, 1971"
12203a78d15Sespie   oss.str(empty);
12303a78d15Sespie   iterator_type os_it36 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
12403a78d15Sespie   string result36 = oss.str(); // "12:00:00 PST"
12503a78d15Sespie 
12603a78d15Sespie   oss.str(empty);
12703a78d15Sespie   oss.imbue(loc_fr);
12803a78d15Sespie   iterator_type os_it04 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'a');
12903a78d15Sespie   string result4 = oss.str();
13003a78d15Sespie   VERIFY( result4 == "dim" );
13103a78d15Sespie 
13203a78d15Sespie   oss.str(empty); // "%d.%m.%Y"
13303a78d15Sespie   iterator_type os_it27 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x');
13403a78d15Sespie   string result27 = oss.str(); // "04.04.1971"
13503a78d15Sespie   oss.str(empty); // "%T"
13603a78d15Sespie   iterator_type os_it28 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X');
13703a78d15Sespie   string result28 = oss.str(); // "12:00:00"
13803a78d15Sespie   oss.str(empty);
13903a78d15Sespie   iterator_type os_it37 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'x', 'E');
14003a78d15Sespie   string result37 = oss.str(); // "04.04.1971"
14103a78d15Sespie   oss.str(empty);
14203a78d15Sespie   iterator_type os_it38 = tim_put.put(oss.rdbuf(), oss, '*', &time1, 'X', 'E');
14303a78d15Sespie   string result38 = oss.str(); // "12:00:00"
14403a78d15Sespie 
14503a78d15Sespie   // 2
14603a78d15Sespie   oss.str(empty);
14703a78d15Sespie   oss.imbue(loc_c);
14803a78d15Sespie   iterator_type os_it05 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
14903a78d15Sespie 				      date, date + strlen(date));
15003a78d15Sespie   string result5 = oss.str();
15103a78d15Sespie   VERIFY( result5 == "Sunday, the second of April");
15203a78d15Sespie   iterator_type os_it06 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
15303a78d15Sespie 				      date_ex, date_ex + strlen(date));
15403a78d15Sespie   string result6 = oss.str();
15503a78d15Sespie   VERIFY( result6 != result5 );
15603a78d15Sespie 
15703a78d15Sespie   oss.str(empty);
15803a78d15Sespie   oss.imbue(loc_de);
15903a78d15Sespie   iterator_type os_it07 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
16003a78d15Sespie 				      date, date + strlen(date));
16103a78d15Sespie   string result7 = oss.str();
16203a78d15Sespie   VERIFY( result7 == "Sonntag, the second of April");
16303a78d15Sespie   iterator_type os_it08 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
16403a78d15Sespie 				      date_ex, date_ex + strlen(date));
16503a78d15Sespie   string result8 = oss.str();
16603a78d15Sespie   VERIFY( result8 != result7 );
16703a78d15Sespie 
16803a78d15Sespie   oss.str(empty);
16903a78d15Sespie   oss.imbue(loc_hk);
17003a78d15Sespie   iterator_type os_it09 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
17103a78d15Sespie 				      date, date + strlen(date));
17203a78d15Sespie   string result9 = oss.str();
17303a78d15Sespie   VERIFY( result9 == "Sunday, the second of April");
17403a78d15Sespie   iterator_type os_it10 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
17503a78d15Sespie 				      date_ex, date_ex + strlen(date));
17603a78d15Sespie   string result10 = oss.str();
17703a78d15Sespie   VERIFY( result10 != result9 );
17803a78d15Sespie 
17903a78d15Sespie   oss.str(empty);
18003a78d15Sespie   oss.imbue(loc_fr);
18103a78d15Sespie   iterator_type os_it11 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
18203a78d15Sespie 				      date, date + strlen(date));
18303a78d15Sespie   string result11 = oss.str();
18403a78d15Sespie   VERIFY( result11 == "dimanche, the second of avril");
18503a78d15Sespie   iterator_type os_it12 = tim_put.put(oss.rdbuf(), oss, '*', &time1,
18603a78d15Sespie 				      date_ex, date_ex + strlen(date));
18703a78d15Sespie   string result12 = oss.str();
18803a78d15Sespie   VERIFY( result12 != result11 );
18903a78d15Sespie }
19003a78d15Sespie 
test02()19103a78d15Sespie void test02()
19203a78d15Sespie {
19303a78d15Sespie   using namespace std;
19403a78d15Sespie   bool test = true;
19503a78d15Sespie 
19603a78d15Sespie   // Check time_put works with other iterators besides streambuf
19703a78d15Sespie   // output iterators. (As long as output_iterator requirements are met.)
19803a78d15Sespie   typedef string::iterator iter_type;
19903a78d15Sespie   typedef char_traits<char> traits;
20003a78d15Sespie   typedef time_put<char, iter_type> time_put_type;
20103a78d15Sespie   const ios_base::iostate goodbit = ios_base::goodbit;
20203a78d15Sespie   const ios_base::iostate eofbit = ios_base::eofbit;
20303a78d15Sespie   ios_base::iostate err = goodbit;
20403a78d15Sespie   const locale loc_c = locale::classic();
20503a78d15Sespie   const string x(50, 'x'); // have to have allocated string!
20603a78d15Sespie   string res;
20703a78d15Sespie   const tm time_sanity = { 0, 0, 12, 26, 5, 97, 2 };
20803a78d15Sespie   const char* date = "%X, %A, the second of %B, %Y";
20903a78d15Sespie 
21003a78d15Sespie   ostringstream oss;
21103a78d15Sespie   oss.imbue(locale(loc_c, new time_put_type));
21203a78d15Sespie 
21303a78d15Sespie   // Iterator advanced, state, output.
21403a78d15Sespie   const time_put_type& tp = use_facet<time_put_type>(oss.getloc());
21503a78d15Sespie 
21603a78d15Sespie   // 01 date format
21703a78d15Sespie   res = x;
21803a78d15Sespie   iter_type ret1 = tp.put(res.begin(), oss, ' ', &time_sanity,
21903a78d15Sespie 			  date, date + traits::length(date));
22003a78d15Sespie   string sanity1(res.begin(), ret1);
22103a78d15Sespie   VERIFY( err == goodbit );
22203a78d15Sespie   VERIFY( res == "12:00:00, Tuesday, the second of June, 1997xxxxxxx" );
22303a78d15Sespie   VERIFY( sanity1 == "12:00:00, Tuesday, the second of June, 1997" );
22403a78d15Sespie 
22503a78d15Sespie   // 02 char format
22603a78d15Sespie   res = x;
22703a78d15Sespie   iter_type ret2 = tp.put(res.begin(), oss, ' ', &time_sanity, 'A');
22803a78d15Sespie   string sanity2(res.begin(), ret2);
22903a78d15Sespie   VERIFY( err == goodbit );
23003a78d15Sespie   VERIFY( res == "Tuesdayxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" );
23103a78d15Sespie   VERIFY( sanity2 == "Tuesday" );
23203a78d15Sespie }
23303a78d15Sespie 
23403a78d15Sespie // libstdc++/5280
test03()23503a78d15Sespie void test03()
23603a78d15Sespie {
23703a78d15Sespie #ifdef _GLIBCPP_HAVE_SETENV
23803a78d15Sespie   // Set the global locale to non-"C".
23903a78d15Sespie   std::locale loc_de("de_DE");
24003a78d15Sespie   std::locale::global(loc_de);
24103a78d15Sespie 
24203a78d15Sespie   // Set LANG environment variable to de_DE.
24303a78d15Sespie   const char* oldLANG = getenv("LANG");
24403a78d15Sespie   if (!setenv("LANG", "de_DE", 1))
24503a78d15Sespie     {
24603a78d15Sespie       test01();
24703a78d15Sespie       test02();
24803a78d15Sespie       setenv("LANG", oldLANG ? oldLANG : "", 1);
24903a78d15Sespie     }
25003a78d15Sespie #endif
25103a78d15Sespie }
25203a78d15Sespie 
25303a78d15Sespie // http://gcc.gnu.org/ml/libstdc++/2002-05/msg00038.html
test04()25403a78d15Sespie void test04()
25503a78d15Sespie {
25603a78d15Sespie   bool test = true;
25703a78d15Sespie 
25803a78d15Sespie   const char* tentLANG = std::setlocale(LC_ALL, "ja_JP.eucjp");
25903a78d15Sespie   if (tentLANG != NULL)
26003a78d15Sespie     {
26103a78d15Sespie       std::string preLANG = tentLANG;
26203a78d15Sespie       test01();
26303a78d15Sespie       test02();
26403a78d15Sespie       std::string postLANG = std::setlocale(LC_ALL, NULL);
26503a78d15Sespie       VERIFY( preLANG == postLANG );
26603a78d15Sespie     }
26703a78d15Sespie }
26803a78d15Sespie 
main()26903a78d15Sespie int main()
27003a78d15Sespie {
27103a78d15Sespie   test01();
27203a78d15Sespie   test02();
27303a78d15Sespie   test03();
27403a78d15Sespie   test04();
27503a78d15Sespie   return 0;
27603a78d15Sespie }
277