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 ///////////////////////////////////////////////////////////////////
35 // METHOD NAME : Date::form
36 // METHOD TYPE : std::string
38 std::string Date::form( const std::string & format_r ) const
40 static char buf[1024];
42 static std::string lastLocale;
43 static std::string needLocale;
44 const char * tmp = ::setlocale( LC_TIME, NULL );
45 std::string thisLocale( tmp ? tmp : "" );
47 if ( thisLocale != lastLocale )
49 // locale changes since last call. compute new utf-8 locale
50 if ( thisLocale.find( "UTF-8" ) != std::string::npos
51 || thisLocale.find( "utf-8" ) != std::string::npos
52 || thisLocale == "POSIX"
56 // is UTF-8 or C or POSIX
57 needLocale = thisLocale;
61 // language[_territory][.codeset][@modifier]
62 // add/exchange codeset with UTF-8
63 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;
82 lastLocale = thisLocale;
85 if ( needLocale != lastLocale )
86 ::setlocale( LC_TIME, needLocale.c_str() );
87 if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
89 if ( needLocale != lastLocale )
90 ::setlocale( LC_TIME, lastLocale.c_str() );
94 /////////////////////////////////////////////////////////////////
96 ///////////////////////////////////////////////////////////////////