libzypp  13.10.6
Date.cc
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
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 
22 namespace zypp
23 {
24 
25  static std::string adjustLocale();
26  static void restoreLocale(const std::string & locale);
27 
28  namespace
29  {
30  inline bool isDST( struct tm & tm )
31  {
32  time_t t = ::mktime( &tm );
33  struct tm *tm2 = ::localtime( &t );
34  return ( tm2 && tm2->tm_isdst > 0 );
35  }
36  }
37 
50 
52  //
53  // METHOD NAME : Date::Date
54  // METHOD TYPE : Constructor
55  //
56  Date::Date( const std::string & seconds_r )
57  { str::strtonum( seconds_r, _date ); }
58 
59  Date::Date( const std::string & date_str, const std::string & format )
60  : _date( Date( date_str, format, TB_LOCALTIME ) )
61  {}
62 
63  Date::Date( const std::string & date_str, const std::string & format, Date::TimeBase base_r )
64  : _date(0)
65  {
66  struct tm tm = {0,0,0,0,0,0,0,0,0,0,0};
67  std::string thisLocale = adjustLocale();
68 
69  char * res = ::strptime( date_str.c_str(), format.c_str(), &tm );
70  if ( isDST(tm) )
71  tm.tm_isdst = 1;
72  if ( res != NULL )
73  _date = (base_r == TB_UTC ? ::timegm : ::timelocal)( &tm );
74 
75  restoreLocale(thisLocale);
76 
77  if (res == NULL)
78  throw DateFormatException(
79  str::form( "Invalid date format: '%s'", date_str.c_str() ) );
80  }
81 
82 
84  //
85  // METHOD NAME : Date::form
86  // METHOD TYPE : std::string
87  //
88  std::string Date::form( const std::string & format_r, Date::TimeBase base_r ) const
89  {
90  static char buf[1024];
91  std::string thisLocale = adjustLocale();
92 
93  if ( ! strftime( buf, 1024, format_r.c_str(), (base_r == TB_UTC ? gmtime : localtime)( &_date ) ) )
94  *buf = '\0';
95 
96  restoreLocale(thisLocale);
97 
98  return buf;
99  }
100 
101  static std::string adjustLocale()
102  {
103  const char * tmp = ::setlocale( LC_TIME, NULL );
104  std::string thisLocale( tmp ? tmp : "" );
105 
106  if ( thisLocale.find( "UTF-8" ) == std::string::npos
107  && thisLocale.find( "utf-8" ) == std::string::npos
108  && thisLocale != "POSIX"
109  && thisLocale != "C"
110  && thisLocale != "" )
111  {
112  // language[_territory][.codeset][@modifier]
113  // add/exchange codeset with UTF-8
114  std::string needLocale = ".UTF-8";
115  std::string::size_type loc = thisLocale.find_first_of( ".@" );
116  if ( loc != std::string::npos )
117  {
118  // prepend language[_territory]
119  needLocale = thisLocale.substr( 0, loc ) + needLocale;
120  loc = thisLocale.find_last_of( "@" );
121  if ( loc != std::string::npos )
122  {
123  // append [@modifier]
124  needLocale += thisLocale.substr( loc );
125  }
126  }
127  else
128  {
129  // append ".UTF-8"
130  needLocale = thisLocale + needLocale;
131  }
132  ::setlocale( LC_TIME, needLocale.c_str() );
133  }
134  else
135  {
136  // no need to change the locale
137  thisLocale.clear();
138  }
139 
140  return thisLocale;
141  }
142 
143  static void restoreLocale(const std::string & locale)
144  {
145  if ( ! locale.empty() )
146  ::setlocale( LC_TIME, locale.c_str() );
147  }
148 
150 } // namespace zypp
static const ValueType day
Definition: Date.h:42
static const ValueType minute
Definition: Date.h:40
static const ValueType month31
Definition: Date.h:46
static void restoreLocale(const std::string &locale)
Definition: Date.cc:143
Date()
Default ctor: 0.
Definition: Date.h:55
static const ValueType month30
Definition: Date.h:45
static const ValueType month
Definition: Date.h:47
static const ValueType month29
Definition: Date.h:44
ValueType _date
Calendar time.
Definition: Date.h:129
Store and operate on date (time_t).
Definition: Date.h:31
static const ValueType year365
Definition: Date.h:48
static const ValueType month28
Definition: Date.h:43
static const ValueType year
Definition: Date.h:50
_It strtonum(const C_Str &str)
Parsing numbers from string.
Definition: String.h:304
SolvableIdType size_type
Definition: PoolMember.h:99
std::string form(const char *format,...)
Printf style construction of std::string.
Definition: String.cc:34
static const ValueType hour
Definition: Date.h:41
std::string form(const std::string &format_r) const
Return string representation according to format as localtime.
Definition: Date.h:107
time_t ValueType
Definition: Date.h:37
static const ValueType year366
Definition: Date.h:49
static std::string adjustLocale()
Definition: Date.cc:101
TimeBase
Definition: Date.h:52
static const ValueType second
Definition: Date.h:39