Output date strings in UTF-8.
[platform/upstream/libzypp.git] / zypp / Date.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file       zypp/Date.cc
10  *
11 */
12 #include <iostream>
13 //#include "zypp/base/Logger.h"
14
15 #include "zypp/base/String.h"
16
17 #include "zypp/Date.h"
18
19 using std::endl;
20
21 ///////////////////////////////////////////////////////////////////
22 namespace zypp
23 { /////////////////////////////////////////////////////////////////
24
25   ///////////////////////////////////////////////////////////////////
26   //
27   //    METHOD NAME : Date::Date
28   //    METHOD TYPE : Constructor
29   //
30   Date::Date( const std::string & seconds_r )
31   { str::strtonum( seconds_r, _date ); }
32
33   ///////////////////////////////////////////////////////////////////
34   //
35   //    METHOD NAME : Date::form
36   //    METHOD TYPE : std::string
37   //
38   std::string Date::form( const std::string & format_r ) const
39   {
40     static char buf[1024];
41
42     static std::string lastLocale;
43     static std::string needLocale;
44     const char * tmp = ::setlocale( LC_TIME, NULL );
45     std::string thisLocale( tmp ? tmp : "" );
46
47     if ( thisLocale != lastLocale )
48     {
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"
53            || thisLocale == "C"
54            || thisLocale == "" )
55       {
56         // is UTF-8 or C or POSIX
57         needLocale = thisLocale;
58       }
59       else
60       {
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 )
66         {
67           // prepend language[_territory]
68           needLocale = thisLocale.substr( 0, loc ) + needLocale;
69           loc = thisLocale.find_last_of( "@" );
70           if ( loc != std::string::npos )
71           {
72             // append [@modifier]
73             needLocale += thisLocale.substr( loc );
74           }
75         }
76         else
77         {
78           // append ".UTF-8"
79           needLocale = thisLocale + needLocale;
80         }
81       }
82       lastLocale = thisLocale;
83     }
84
85     if ( needLocale != lastLocale )
86       ::setlocale( LC_TIME, needLocale.c_str() );
87     if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
88       *buf = '\0';
89     if ( needLocale != lastLocale )
90       ::setlocale( LC_TIME, lastLocale.c_str() );
91     return buf;
92   }
93
94   /////////////////////////////////////////////////////////////////
95 } // namespace zypp
96 ///////////////////////////////////////////////////////////////////