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