Date.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00012 #include <iostream>
00013 //#include "zypp/base/Logger.h"
00014 
00015 #include "zypp/base/String.h"
00016 
00017 #include "zypp/Date.h"
00018 
00019 using std::endl;
00020 
00022 namespace zypp
00023 { 
00024 
00026   //
00027   //    METHOD NAME : Date::Date
00028   //    METHOD TYPE : Constructor
00029   //
00030   Date::Date( const std::string & seconds_r )
00031   { str::strtonum( seconds_r, _date ); }
00032 
00034   //
00035   //    METHOD NAME : Date::form
00036   //    METHOD TYPE : std::string
00037   //
00038   std::string Date::form( const std::string & format_r ) const
00039   {
00040     static char buf[1024];
00041 
00042     const char * tmp = ::setlocale( LC_TIME, NULL );
00043     std::string thisLocale( tmp ? tmp : "" );
00044 
00045     if (    thisLocale.find( "UTF-8" ) == std::string::npos
00046          && thisLocale.find( "utf-8" ) == std::string::npos
00047          && thisLocale != "POSIX"
00048          && thisLocale != "C"
00049          && thisLocale != "" )
00050     {
00051       // language[_territory][.codeset][@modifier]
00052       // add/exchange codeset with UTF-8
00053       std::string needLocale = ".UTF-8";
00054       std::string::size_type loc = thisLocale.find_first_of( ".@" );
00055       if ( loc != std::string::npos )
00056       {
00057         // prepend language[_territory]
00058         needLocale = thisLocale.substr( 0, loc ) + needLocale;
00059         loc = thisLocale.find_last_of( "@" );
00060         if ( loc != std::string::npos )
00061         {
00062           // append [@modifier]
00063           needLocale += thisLocale.substr( loc );
00064         }
00065       }
00066       else
00067       {
00068         // append ".UTF-8"
00069         needLocale = thisLocale + needLocale;
00070       }
00071       ::setlocale( LC_TIME, needLocale.c_str() );
00072     }
00073     else
00074     {
00075       // no need to change the locale
00076       thisLocale.clear();
00077     }
00078 
00079     if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
00080       *buf = '\0';
00081 
00082     if ( ! thisLocale.empty() )
00083     {
00084       ::setlocale( LC_TIME, thisLocale.c_str() );
00085     }
00086 
00087     return buf;
00088   }
00089 
00091 } // namespace zypp

Generated on Thu Apr 24 02:24:48 2008 for zypp by  doxygen 1.4.6