483bafd2e078b6cb31130e6f551fa6b18321e572
[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   Date::Date( const std::string & date_str, const std::string & format )
34   {
35     struct tm tm;
36     if ( ::strptime( date_str.c_str(), format.c_str(), &tm ) != NULL )
37       _date = ::timelocal( &tm );
38     else
39       throw DateFormatException(
40           str::form( "Invalid date format: '%s'", date_str.c_str() ) );
41   }
42
43   ///////////////////////////////////////////////////////////////////
44   //
45   //    METHOD NAME : Date::form
46   //    METHOD TYPE : std::string
47   //
48   std::string Date::form( const std::string & format_r ) const
49   {
50     static char buf[1024];
51
52     const char * tmp = ::setlocale( LC_TIME, NULL );
53     std::string thisLocale( tmp ? tmp : "" );
54
55     if (    thisLocale.find( "UTF-8" ) == std::string::npos
56          && thisLocale.find( "utf-8" ) == std::string::npos
57          && thisLocale != "POSIX"
58          && thisLocale != "C"
59          && thisLocale != "" )
60     {
61       // language[_territory][.codeset][@modifier]
62       // add/exchange codeset with UTF-8
63       std::string 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       ::setlocale( LC_TIME, needLocale.c_str() );
82     }
83     else
84     {
85       // no need to change the locale
86       thisLocale.clear();
87     }
88
89     if ( ! strftime( buf, 1024, format_r.c_str(), localtime( &_date ) ) )
90       *buf = '\0';
91
92     if ( ! thisLocale.empty() )
93     {
94       ::setlocale( LC_TIME, thisLocale.c_str() );
95     }
96
97     return buf;
98   }
99
100   /////////////////////////////////////////////////////////////////
101 } // namespace zypp
102 ///////////////////////////////////////////////////////////////////