45a0aad40f657d1f4e6913cdd2effe5c36c0a731
[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   static std::string adjustLocale();
26   static void restoreLocale(const std::string & locale);
27
28   const Date::ValueType Date::second;
29   const Date::ValueType Date::minute;
30   const Date::ValueType Date::hour;
31   const Date::ValueType Date::day;
32   const Date::ValueType Date::month28;
33   const Date::ValueType Date::month29;
34   const Date::ValueType Date::month30;
35   const Date::ValueType Date::month31;
36   const Date::ValueType Date::month;
37   const Date::ValueType Date::year365;
38   const Date::ValueType Date::year366;
39   const Date::ValueType Date::year;
40
41   ///////////////////////////////////////////////////////////////////
42   //
43   //    METHOD NAME : Date::Date
44   //    METHOD TYPE : Constructor
45   //
46   Date::Date( const std::string & seconds_r )
47   { str::strtonum( seconds_r, _date ); }
48
49   Date::Date( const std::string & date_str, const std::string & format )
50     : _date(0)
51   {
52     struct tm tm = {0};
53     std::string thisLocale = adjustLocale();
54
55     char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
56     if ( res != NULL )
57       _date = ::timelocal( &tm );
58
59     restoreLocale(thisLocale);
60
61     if (res == NULL)
62       throw DateFormatException(
63           str::form( "Invalid date format: '%s'", date_str.c_str() ) );
64   }
65
66   ///////////////////////////////////////////////////////////////////
67   //
68   //    METHOD NAME : Date::form
69   //    METHOD TYPE : std::string
70   //
71   std::string Date::form( const std::string & format_r ) const
72   {
73     static char buf[1024];
74     std::string thisLocale = adjustLocale();
75
76     if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
77       *buf = '\0';
78
79     restoreLocale(thisLocale);
80
81     return buf;
82   }
83
84   static std::string adjustLocale()
85   {
86     const char * tmp = ::setlocale( LC_TIME, NULL );
87     std::string thisLocale( tmp ? tmp : "" );
88
89     if (    thisLocale.find( "UTF-8" ) == std::string::npos
90          && thisLocale.find( "utf-8" ) == std::string::npos
91          && thisLocale != "POSIX"
92          && thisLocale != "C"
93          && thisLocale != "" )
94     {
95       // language[_territory][.codeset][@modifier]
96       // add/exchange codeset with UTF-8
97       std::string needLocale = ".UTF-8";
98       std::string::size_type loc = thisLocale.find_first_of( ".@" );
99       if ( loc != std::string::npos )
100       {
101         // prepend language[_territory]
102         needLocale = thisLocale.substr( 0, loc ) + needLocale;
103         loc = thisLocale.find_last_of( "@" );
104         if ( loc != std::string::npos )
105         {
106           // append [@modifier]
107           needLocale += thisLocale.substr( loc );
108         }
109       }
110       else
111       {
112         // append ".UTF-8"
113         needLocale = thisLocale + needLocale;
114       }
115       ::setlocale( LC_TIME, needLocale.c_str() );
116     }
117     else
118     {
119       // no need to change the locale
120       thisLocale.clear();
121     }
122
123     return thisLocale;
124   }
125
126   static void restoreLocale(const std::string & locale)
127   {
128     if ( ! locale.empty() )
129       ::setlocale( LC_TIME, locale.c_str() );
130   }
131
132   /////////////////////////////////////////////////////////////////
133 } // namespace zypp
134 ///////////////////////////////////////////////////////////////////