From 99c56fd811831ac02de1331a9de7c00befc26f92 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A1n=20Kupec?= Date: Thu, 22 Jan 2009 18:48:51 +0100 Subject: [PATCH] Adjust locale in Date(str, format) c-tor, like in Date::format(). --- zypp/Date.cc | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/zypp/Date.cc b/zypp/Date.cc index 483bafd..367ce34 100644 --- a/zypp/Date.cc +++ b/zypp/Date.cc @@ -22,6 +22,9 @@ using std::endl; namespace zypp { ///////////////////////////////////////////////////////////////// + static std::string adjustLocale(); + static void restoreLocale(const std::string & locale); + /////////////////////////////////////////////////////////////////// // // METHOD NAME : Date::Date @@ -33,9 +36,15 @@ namespace zypp Date::Date( const std::string & date_str, const std::string & format ) { struct tm tm; - if ( ::strptime( date_str.c_str(), format.c_str(), &tm ) != NULL ) + std::string thisLocale = adjustLocale(); + + char * res = ::strptime( date_str.c_str(), format.c_str(), &tm ); + if ( res != NULL ) _date = ::timelocal( &tm ); - else + + restoreLocale(thisLocale); + + if (res == NULL) throw DateFormatException( str::form( "Invalid date format: '%s'", date_str.c_str() ) ); } @@ -48,7 +57,18 @@ namespace zypp std::string Date::form( const std::string & format_r ) const { static char buf[1024]; + std::string thisLocale = adjustLocale(); + if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) ) + *buf = '\0'; + + restoreLocale(thisLocale); + + return buf; + } + + static std::string adjustLocale() + { const char * tmp = ::setlocale( LC_TIME, NULL ); std::string thisLocale( tmp ? tmp : "" ); @@ -86,15 +106,15 @@ namespace zypp thisLocale.clear(); } - if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) ) - *buf = '\0'; + return thisLocale; + } - if ( ! thisLocale.empty() ) + static void restoreLocale(const std::string & locale) + { + if ( ! locale.empty() ) { - ::setlocale( LC_TIME, thisLocale.c_str() ); + ::setlocale( LC_TIME, locale.c_str() ); } - - return buf; } ///////////////////////////////////////////////////////////////// -- 2.7.4