1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
13 //#include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
17 #include "zypp/Date.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 static std::string adjustLocale();
26 static void restoreLocale(const std::string & locale);
28 ///////////////////////////////////////////////////////////////////
30 // METHOD NAME : Date::Date
31 // METHOD TYPE : Constructor
33 Date::Date( const std::string & seconds_r )
34 { str::strtonum( seconds_r, _date ); }
36 Date::Date( const std::string & date_str, const std::string & format )
39 std::string thisLocale = adjustLocale();
41 char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
43 _date = ::timelocal( &tm );
45 restoreLocale(thisLocale);
48 throw DateFormatException(
49 str::form( "Invalid date format: '%s'", date_str.c_str() ) );
52 ///////////////////////////////////////////////////////////////////
54 // METHOD NAME : Date::form
55 // METHOD TYPE : std::string
57 std::string Date::form( const std::string & format_r ) const
59 static char buf[1024];
60 std::string thisLocale = adjustLocale();
62 if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
65 restoreLocale(thisLocale);
70 static std::string adjustLocale()
72 const char * tmp = ::setlocale( LC_TIME, NULL );
73 std::string thisLocale( tmp ? tmp : "" );
75 if ( thisLocale.find( "UTF-8" ) == std::string::npos
76 && thisLocale.find( "utf-8" ) == std::string::npos
77 && thisLocale != "POSIX"
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 )
87 // prepend language[_territory]
88 needLocale = thisLocale.substr( 0, loc ) + needLocale;
89 loc = thisLocale.find_last_of( "@" );
90 if ( loc != std::string::npos )
93 needLocale += thisLocale.substr( loc );
99 needLocale = thisLocale + needLocale;
101 ::setlocale( LC_TIME, needLocale.c_str() );
105 // no need to change the locale
112 static void restoreLocale(const std::string & locale)
114 if ( ! locale.empty() )
116 ::setlocale( LC_TIME, locale.c_str() );
120 /////////////////////////////////////////////////////////////////
122 ///////////////////////////////////////////////////////////////////