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 const Date::ValueType Date::second;
29 const Date::ValueType Date::minute;
30 const Date::ValueType Date::hour;
31 const Date::ValueType Date::day;
32 const Date::ValueType Date::month28;
33 const Date::ValueType Date::month29;
34 const Date::ValueType Date::month30;
35 const Date::ValueType Date::month31;
36 const Date::ValueType Date::month;
37 const Date::ValueType Date::year365;
38 const Date::ValueType Date::year366;
39 const Date::ValueType Date::year;
41 ///////////////////////////////////////////////////////////////////
43 // METHOD NAME : Date::Date
44 // METHOD TYPE : Constructor
46 Date::Date( const std::string & seconds_r )
47 { str::strtonum( seconds_r, _date ); }
49 Date::Date( const std::string & date_str, const std::string & format )
53 std::string thisLocale = adjustLocale();
55 char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
57 _date = ::timelocal( &tm );
59 restoreLocale(thisLocale);
62 throw DateFormatException(
63 str::form( "Invalid date format: '%s'", date_str.c_str() ) );
66 ///////////////////////////////////////////////////////////////////
68 // METHOD NAME : Date::form
69 // METHOD TYPE : std::string
71 std::string Date::form( const std::string & format_r ) const
73 static char buf[1024];
74 std::string thisLocale = adjustLocale();
76 if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
79 restoreLocale(thisLocale);
84 static std::string adjustLocale()
86 const char * tmp = ::setlocale( LC_TIME, NULL );
87 std::string thisLocale( tmp ? tmp : "" );
89 if ( thisLocale.find( "UTF-8" ) == std::string::npos
90 && thisLocale.find( "utf-8" ) == std::string::npos
91 && thisLocale != "POSIX"
95 // language[_territory][.codeset][@modifier]
96 // add/exchange codeset with UTF-8
97 std::string needLocale = ".UTF-8";
98 std::string::size_type loc = thisLocale.find_first_of( ".@" );
99 if ( loc != std::string::npos )
101 // prepend language[_territory]
102 needLocale = thisLocale.substr( 0, loc ) + needLocale;
103 loc = thisLocale.find_last_of( "@" );
104 if ( loc != std::string::npos )
106 // append [@modifier]
107 needLocale += thisLocale.substr( loc );
113 needLocale = thisLocale + needLocale;
115 ::setlocale( LC_TIME, needLocale.c_str() );
119 // no need to change the locale
126 static void restoreLocale(const std::string & locale)
128 if ( ! locale.empty() )
129 ::setlocale( LC_TIME, locale.c_str() );
132 /////////////////////////////////////////////////////////////////
134 ///////////////////////////////////////////////////////////////////