1 /*---------------------------------------------------------------------\
3 | |__ / \ / / . \ . \ |
8 \---------------------------------------------------------------------*/
13 //#include "zypp/base/Logger.h"
15 #include "zypp/base/String.h"
17 #include "zypp/Date.h"
21 ///////////////////////////////////////////////////////////////////
23 { /////////////////////////////////////////////////////////////////
25 ///////////////////////////////////////////////////////////////////
27 // METHOD NAME : Date::Date
28 // METHOD TYPE : Constructor
30 Date::Date( const std::string & seconds_r )
31 { str::strtonum( seconds_r, _date ); }
33 Date::Date( const std::string & date_str, const std::string & format )
36 if ( ::strptime( date_str.c_str(), format.c_str(), &tm ) != NULL )
37 _date = ::timelocal( &tm );
39 throw DateFormatException(
40 str::form( "Invalid date format: '%s'", date_str.c_str() ) );
43 ///////////////////////////////////////////////////////////////////
45 // METHOD NAME : Date::form
46 // METHOD TYPE : std::string
48 std::string Date::form( const std::string & format_r ) const
50 static char buf[1024];
52 const char * tmp = ::setlocale( LC_TIME, NULL );
53 std::string thisLocale( tmp ? tmp : "" );
55 if ( thisLocale.find( "UTF-8" ) == std::string::npos
56 && thisLocale.find( "utf-8" ) == std::string::npos
57 && thisLocale != "POSIX"
61 // language[_territory][.codeset][@modifier]
62 // add/exchange codeset with UTF-8
63 std::string needLocale = ".UTF-8";
64 std::string::size_type loc = thisLocale.find_first_of( ".@" );
65 if ( loc != std::string::npos )
67 // prepend language[_territory]
68 needLocale = thisLocale.substr( 0, loc ) + needLocale;
69 loc = thisLocale.find_last_of( "@" );
70 if ( loc != std::string::npos )
73 needLocale += thisLocale.substr( loc );
79 needLocale = thisLocale + needLocale;
81 ::setlocale( LC_TIME, needLocale.c_str() );
85 // no need to change the locale
89 if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
92 if ( ! thisLocale.empty() )
94 ::setlocale( LC_TIME, thisLocale.c_str() );
100 /////////////////////////////////////////////////////////////////
102 ///////////////////////////////////////////////////////////////////