1 //
2 //  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
3 //
4 //  Distributed under the Boost Software License, Version 1.0. (See
5 //  accompanying file LICENSE_1_0.txt or copy at
6 //  http://www.boost.org/LICENSE_1_0.txt)
7 //
8 
9 #include <iostream>
10 #include <iomanip>
11 #include <stdlib.h>
12 #include <locale.h>
13 #include <locale>
14 #include <time.h>
15 #include <stdexcept>
16 
17 #include <boost/locale.hpp>
18 #ifdef BOOST_LOCALE_WITH_ICU
19 #include <unicode/uversion.h>
20 #endif
21 
22 #include "test_locale_tools.hpp"
23 
24 
env(char const * s)25 char const *env(char const *s)
26 {
27     char const *r=getenv(s);
28     if(r)
29         return r;
30     return "";
31 }
32 
check_locale(char const ** names)33 void check_locale(char const **names)
34 {
35     std::cout << "  " << std::setw(32) << "locale" << std::setw(4) << "C" << std::setw(4) << "C++" << std::endl;
36     while(*names) {
37         char const *name = *names;
38         std::cout << "  " << std::setw(32) << name << std::setw(4);
39         if(setlocale(LC_ALL,name)!=0)
40             std::cout << "Yes";
41         else
42             std::cout << "No";
43         std::cout << std::setw(4);
44         try {
45             std::locale l(name);
46             std::cout << "Yes";
47         }
48         catch(std::exception const &) {
49             std::cout << "No";
50         }
51         std::cout << std::endl;
52         names++;
53     }
54 }
55 
main()56 int main()
57 {
58     std::cout << "- Backends: ";
59     #ifdef BOOST_LOCALE_WITH_ICU
60     std::cout << "icu:" << U_ICU_VERSION << " ";
61     #endif
62     #ifndef BOOST_LOCALE_NO_STD_BACKEND
63     std::cout << "std ";
64     #endif
65     #ifndef BOOST_LOCALE_NO_POSIX_BACKEND
66     std::cout << "posix ";
67     #endif
68     #ifndef BOOST_LOCALE_NO_WINAPI_BACKEND
69     std::cout << "winapi";
70     #endif
71     std::cout << std::endl;
72     #ifdef BOOST_LOCALE_WITH_ICONV
73     std::cout << "- With iconv" << std::endl;
74     #else
75     std::cout << "- Without iconv" << std::endl;
76     #endif
77     std::cout << "- Environment " << std::endl;
78     std::cout << "  LANG="<< env("LANG") << std::endl;
79     std::cout << "  LC_ALL="<< env("LC_ALL") << std::endl;
80     std::cout << "  LC_CTYPE="<< env("LC_CTYPE") << std::endl;
81     std::cout << "  TZ="<< env("TZ") << std::endl;
82 
83     char const *clocale=setlocale(LC_ALL,"");
84     if(!clocale)
85         clocale= "undetected";
86     std::cout <<"- C locale: " << clocale << std::endl;
87 
88     try {
89         std::locale loc("");
90         std::cout << "- C++ locale: " << loc.name() << std::endl;
91     }
92     catch(std::exception const &) {
93         std::cout << "- C++ locale: is not supported" << std::endl;
94     }
95 
96     char const *locales_to_check[] = {
97         "en_US.UTF-8", "en_US.ISO8859-1", "English_United States.1252",
98         "he_IL.UTF-8", "he_IL.ISO8859-8", "Hebrew_Israel.1255",
99         "ru_RU.UTF-8", "Russian_Russia.1251",
100         "tr_TR.UTF-8", "Turkish_Turkey.1254",
101         "ja_JP.UTF-8", "ja_JP.SJIS", "Japanese_Japan.932",
102         0
103     };
104     std::cout << "- Testing locales availability on the operation system:" << std::endl;
105     check_locale(locales_to_check);
106     std::cout << "--- Testing Japanese_Japan.932 is working: " << test_std_supports_SJIS_codecvt("Japanese_Japan.932") << std::endl;
107 
108     std::cout << "- Testing timezone and time " << std::endl;
109     {
110         setlocale(LC_ALL,"C");
111         time_t now = time(0);
112         char buf[1024];
113         strftime(buf,sizeof(buf),"%%c=%c; %%Z=%Z; %%z=%z",localtime(&now));
114         std::cout << "  Local Time    :" << buf << std::endl;
115         strftime(buf,sizeof(buf),"%%c=%c; %%Z=%Z; %%z=%z",gmtime(&now));
116         std::cout << "  Universal Time:" << buf << std::endl;
117     }
118     std::cout << "- Boost.Locale's locale: ";
119     try {
120         boost::locale::generator gen;
121         std::locale l = gen("");
122         std::cout << std::use_facet<boost::locale::info>(l).name() << std::endl;
123     }
124     catch(std::exception const &) {
125         std::cout << " undetected" << std::endl;
126         return EXIT_FAILURE;
127     }
128     return EXIT_SUCCESS;
129 
130 }
131 
132 // vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
133 // boostinspect:noascii
134