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 )
40 std::string thisLocale = adjustLocale();
42 char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
44 _date = ::timelocal( &tm );
46 restoreLocale(thisLocale);
49 throw DateFormatException(
50 str::form( "Invalid date format: '%s'", date_str.c_str() ) );
53 ///////////////////////////////////////////////////////////////////
55 // METHOD NAME : Date::form
56 // METHOD TYPE : std::string
58 std::string Date::form( const std::string & format_r ) const
60 static char buf[1024];
61 std::string thisLocale = adjustLocale();
63 if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
66 restoreLocale(thisLocale);
71 static std::string adjustLocale()
73 const char * tmp = ::setlocale( LC_TIME, NULL );
74 std::string thisLocale( tmp ? tmp : "" );
76 if ( thisLocale.find( "UTF-8" ) == std::string::npos
77 && thisLocale.find( "utf-8" ) == std::string::npos
78 && thisLocale != "POSIX"
82 // language[_territory][.codeset][@modifier]
83 // add/exchange codeset with UTF-8
84 std::string needLocale = ".UTF-8";
85 std::string::size_type loc = thisLocale.find_first_of( ".@" );
86 if ( loc != std::string::npos )
88 // prepend language[_territory]
89 needLocale = thisLocale.substr( 0, loc ) + needLocale;
90 loc = thisLocale.find_last_of( "@" );
91 if ( loc != std::string::npos )
94 needLocale += thisLocale.substr( loc );
100 needLocale = thisLocale + needLocale;
102 ::setlocale( LC_TIME, needLocale.c_str() );
106 // no need to change the locale
113 static void restoreLocale(const std::string & locale)
115 if ( ! locale.empty() )
116 ::setlocale( LC_TIME, locale.c_str() );
119 /////////////////////////////////////////////////////////////////
121 ///////////////////////////////////////////////////////////////////