Adjust locale in Date(str, format) c-tor, like in Date::format().
[platform/upstream/libzypp.git] / zypp / Date.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Date.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/base/String.h"
16
17 #include "zypp/Date.h"
18
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   static std::string adjustLocale();
26   static void restoreLocale(const std::string & locale);
27
28   ///////////////////////////////////////////////////////////////////
29   //
30   //    METHOD NAME : Date::Date
31   //    METHOD TYPE : Constructor
32   //
33   Date::Date( const std::string & seconds_r )
34   { str::strtonum( seconds_r, _date ); }
35
36   Date::Date( const std::string & date_str, const std::string & format )
37   {
38     struct tm tm;
39     std::string thisLocale = adjustLocale();
40
41     char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
42     if ( res != NULL )
43       _date = ::timelocal( &tm );
44
45     restoreLocale(thisLocale);
46
47     if (res == NULL)
48       throw DateFormatException(
49           str::form( "Invalid date format: '%s'", date_str.c_str() ) );
50   }
51
52   ///////////////////////////////////////////////////////////////////
53   //
54   //    METHOD NAME : Date::form
55   //    METHOD TYPE : std::string
56   //
57   std::string Date::form( const std::string & format_r ) const
58   {
59     static char buf[1024];
60     std::string thisLocale = adjustLocale();
61
62     if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
63       *buf = '\0';
64
65     restoreLocale(thisLocale);
66
67     return buf;
68   }
69
70   static std::string adjustLocale()
71   {
72     const char * tmp = ::setlocale( LC_TIME, NULL );
73     std::string thisLocale( tmp ? tmp : "" );
74
75     if (    thisLocale.find( "UTF-8" ) == std::string::npos
76          && thisLocale.find( "utf-8" ) == std::string::npos
77          && thisLocale != "POSIX"
78          && thisLocale != "C"
79          && thisLocale != "" )
80     {
81       // language[_territory][.codeset][@modifier]
82       // add/exchange codeset with UTF-8
83       std::string needLocale = ".UTF-8";
84       std::string::size_type loc = thisLocale.find_first_of( ".@" );
85       if ( loc != std::string::npos )
86       {
87         // prepend language[_territory]
88         needLocale = thisLocale.substr( 0, loc ) + needLocale;
89         loc = thisLocale.find_last_of( "@" );
90         if ( loc != std::string::npos )
91         {
92           // append [@modifier]
93           needLocale += thisLocale.substr( loc );
94         }
95       }
96       else
97       {
98         // append ".UTF-8"
99         needLocale = thisLocale + needLocale;
100       }
101       ::setlocale( LC_TIME, needLocale.c_str() );
102     }
103     else
104     {
105       // no need to change the locale
106       thisLocale.clear();
107     }
108
109     return thisLocale;
110   }
111
112   static void restoreLocale(const std::string & locale)
113   {
114     if ( ! locale.empty() )
115     {
116       ::setlocale( LC_TIME, locale.c_str() );
117     }
118   }
119
120   /////////////////////////////////////////////////////////////////
121 } // namespace zypp
122 ///////////////////////////////////////////////////////////////////