libzypp  14.48.5
Date.h
Go to the documentation of this file.
1 /*---------------------------------------------------------------------\
2 | ____ _ __ __ ___ |
3 | |__ / \ / / . \ . \ |
4 | / / \ V /| _/ _/ |
5 | / /__ | | | | | | |
6 | /_____||_| |_| |_| |
7 | |
8 \---------------------------------------------------------------------*/
12 #ifndef ZYPP_DATE_H
13 #define ZYPP_DATE_H
14 
15 #include <ctime>
16 #include <iosfwd>
17 #include <string>
18 
19 #include "zypp/base/Exception.h"
20 #include "zypp/base/EnumClass.h"
21 
23 namespace zypp
24 {
25 
27  //
28  // CLASS NAME : Date
29  //
32  class Date
33  {
34  friend std::ostream & operator<<( std::ostream & str, const Date & obj );
35 
36  public:
37 
38  typedef time_t ValueType;
39 
40  static const ValueType second = 1;
41  static const ValueType minute = 60;
42  static const ValueType hour = 3600;
43  static const ValueType day = 86400;
44  static const ValueType month28 = 2419200;
45  static const ValueType month29 = 2505600;
46  static const ValueType month30 = 2592000;
47  static const ValueType month31 = 2678400;
48  static const ValueType month = month30;
49  static const ValueType year365 = 31536000;
50  static const ValueType year366 = 31622400;
51  static const ValueType year = year365;
52 
54 
56  Date()
57  : _date( 0 )
58  {}
60  Date( ValueType date_r )
61  : _date( date_r )
62  {}
64  explicit Date( const std::string & seconds_r );
65 
72  Date( const std::string & date_str, const std::string & format );
74  Date( const std::string & date_str, const std::string & format, TimeBase base_r );
75 
77  static Date now()
78  { return ::time( 0 ); }
79 
80  public:
82  operator ValueType() const
83  { return _date; }
84 
87  Date operator+( const time_t rhs ) const { return _date + rhs; }
88  Date operator-( const time_t rhs ) const { return _date - rhs; }
89  Date operator*( const time_t rhs ) const { return _date * rhs; }
90  Date operator/( const time_t rhs ) const { return _date / rhs; }
91 
92  Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
93  Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
94  Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
95  Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
96 
97  Date & operator++(/*prefix*/) { _date += 1; return *this; }
98  Date & operator--(/*prefix*/) { _date -= 1; return *this; }
99 
100  Date operator++(int/*postfix*/) { return _date++; }
101  Date operator--(int/*postfix*/) { return _date--; }
103 
104  public:
111  std::string form( const std::string & format_r ) const
112  { return form( format_r, TB_LOCALTIME ); }
114  std::string form( const std::string & format_r, TimeBase base_r ) const;
115 
119  std::string asString() const
120  { return form( "%c" ); }
121 
125  std::string asSeconds() const
126  { return form( "%s" ); }
127 
128  public:
132  struct _DateFormatDef { enum Enum {
140  };};
141  typedef base::EnumClass<_DateFormatDef> DateFormat;
142 
144  struct _TimeFormatDef { enum Enum {
149  };};
150  typedef base::EnumClass<_TimeFormatDef> TimeFormat;
151 
153  struct _TimeZoneFormatDef { enum Enum {
157  };};
158  typedef base::EnumClass<_TimeZoneFormatDef> TimeZoneFormat;
159 
165  std::string print( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
167  std::string print( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
168  { return print( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
170  std::string print( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
171  { return print( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
173  std::string print( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
174  { return print( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
176  std::string print( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
177  { return print( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
179  std::string print( TimeFormat timeFormat_r, TimeBase base_r ) const
180  { return print( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
182  std::string print( DateFormat dateFormat_r, TimeBase base_r ) const
183  { return print( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
185  std::string print( TimeBase base_r ) const
186  { return print( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
187 
191  std::string printDate( DateFormat dateFormat_r = DateFormat::calendar, TimeBase base_r = TB_LOCALTIME ) const
192  { return print( dateFormat_r, TimeFormat::none, TimeZoneFormat::none, base_r ); }
194  std::string printDate( TimeBase base_r ) const
195  { return printDate( DateFormat::calendar, base_r ); }
196 
200  std::string printTime( TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
201  { return print( DateFormat::none, timeFormat_r, timeZoneFormat_r, base_r ); }
203  std::string printTime( TimeZoneFormat timeZoneFormat_r , TimeBase base_r = TB_LOCALTIME ) const
204  { return printTime( TimeFormat::seconds, timeZoneFormat_r, base_r ); }
206  std::string printTime( TimeFormat timeFormat_r , TimeBase base_r ) const
207  { return printTime( timeFormat_r, TimeZoneFormat::name, base_r ); }
209  std::string printTime( TimeBase base_r ) const
210  { return printTime( TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
211 
217  std::string printISO( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
219  std::string printISO( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
220  { return printISO( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
222  std::string printISO( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
223  { return printISO( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
225  std::string printISO( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
226  { return printISO( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
228  std::string printISO( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
229  { return printISO( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
231  std::string printISO( TimeFormat timeFormat_r, TimeBase base_r ) const
232  { return printISO( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
234  std::string printISO( DateFormat dateFormat_r, TimeBase base_r ) const
235  { return printISO( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
237  std::string printISO( TimeBase base_r ) const
238  { return printISO( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
240 
241  private:
247  };
249 
251  inline std::ostream & operator<<( std::ostream & str, const Date & obj )
252  { return str << obj.asString(); }
253 
257  std::ostream & dumpAsXmlOn( std::ostream & str, const Date & obj, const std::string & name_r = "date" );
258 
261  {
262  public:
263  DateFormatException( const std::string & msg ) : Exception( msg )
264  {}
265  };
266 
268 } // namespace zypp
270 #endif // ZYPP_DATE_H
std::string printISO(TimeFormat timeFormat_r, TimeBase base_r) const
Definition: Date.h:231
static const ValueType day
Definition: Date.h:43
Date & operator+=(const time_t rhs)
Definition: Date.h:92
friend std::ostream & operator<<(std::ostream &str, const Date &obj)
std::string printISO(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:228
Date operator+(const time_t rhs) const
Definition: Date.h:87
static const ValueType minute
Definition: Date.h:41
static const ValueType month31
Definition: Date.h:47
std::string printISO(DateFormat dateFormat_r=DateFormat::calendar, TimeFormat timeFormat_r=TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
Default ISO 8601 format is '2014-02-07T07:06:41+01'
Definition: Date.cc:204
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:185
std::string printDate(DateFormat dateFormat_r=DateFormat::calendar, TimeBase base_r=TB_LOCALTIME) const
Convenience for printing the date only ['2014-02-07'] The default is DateFormat::calendar and TB_LOCA...
Definition: Date.h:191
Time formats for printing (use like 'enum class TimeFormat')
Definition: Date.h:144
std::string print(DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r) const
Definition: Date.h:173
std::string printISO(TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:219
std::string printTime(TimeBase base_r) const
Definition: Date.h:209
base::EnumClass< _DateFormatDef > DateFormat
'enum class DateFormat'
Definition: Date.h:141
Date()
Default ctor: 0.
Definition: Date.h:56
Date(ValueType date_r)
Ctor taking time_t value.
Definition: Date.h:60
DateFormatException(const std::string &msg)
Definition: Date.h:263
std::string print(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:176
Date operator--(int)
Definition: Date.h:101
static const ValueType month30
Definition: Date.h:46
Date & operator/=(const time_t rhs)
Definition: Date.h:95
static const ValueType month
Definition: Date.h:48
Date operator*(const time_t rhs) const
Definition: Date.h:89
static const ValueType month29
Definition: Date.h:45
std::string print(DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:170
ValueType _date
Calendar time.
Definition: Date.h:246
2014-W06-5 (1 is Monday)
Definition: Date.h:138
Store and operate on date (time_t).
Definition: Date.h:32
base::EnumClass< _TimeZoneFormatDef > TimeZoneFormat
'enum class TimeZoneFormat'
Definition: Date.h:158
std::string print(TimeBase base_r) const
Definition: Date.h:185
Date formats for printing (use like 'enum class DateFormat')
Definition: Date.h:132
static const ValueType year365
Definition: Date.h:49
std::ostream & operator<<(std::ostream &str, const Date &obj)
Definition: Date.h:251
std::string print(DateFormat dateFormat_r, TimeBase base_r) const
Definition: Date.h:182
static const ValueType month28
Definition: Date.h:44
static const ValueType year
Definition: Date.h:51
std::string print(TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:167
Date & operator--()
Definition: Date.h:98
std::string printISO(DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r) const
Definition: Date.h:225
Date & operator*=(const time_t rhs)
Definition: Date.h:94
std::string printTime(TimeFormat timeFormat_r, TimeBase base_r) const
Definition: Date.h:206
base::EnumClass< _TimeFormatDef > TimeFormat
'enum class TimeFormat'
Definition: Date.h:150
Date & operator++()
Definition: Date.h:97
std::string printTime(TimeFormat timeFormat_r=TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
Convenience for printing the time only ['07:06:41 CET'] The default is DateFormat::calendar and TB_LO...
Definition: Date.h:200
static const ValueType hour
Definition: Date.h:42
std::string printISO(DateFormat dateFormat_r, TimeBase base_r) const
Definition: Date.h:234
Base class for Exception.
Definition: Exception.h:143
std::string form(const std::string &format_r) const
Return string representation according to format as localtime.
Definition: Date.h:111
static Date now()
Return the current time.
Definition: Date.h:77
std::string asString() const
Default string representation of Date.
Definition: Date.h:119
Timezone indicator for printing (use like 'enum class TimeZoneFormat')
Definition: Date.h:153
Date operator++(int)
Definition: Date.h:100
std::string asSeconds() const
Convert to string representation of calendar time in numeric form (like "1029255142").
Definition: Date.h:125
time_t ValueType
Definition: Date.h:38
Date operator-(const time_t rhs) const
Definition: Date.h:88
static const ValueType year366
Definition: Date.h:50
std::string print(DateFormat dateFormat_r=DateFormat::calendar, TimeFormat timeFormat_r=TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
Default format is '2014-02-07 07:06:41 CET' The default is DateFormat::calendar, TimeFormat::seconds...
Definition: Date.cc:188
std::string printISO(DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:222
Date & operator-=(const time_t rhs)
Definition: Date.h:93
std::string printTime(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
Definition: Date.h:203
TimeBase
Definition: Date.h:53
std::string printISO(TimeBase base_r) const
Definition: Date.h:237
std::ostream & dumpAsXmlOn(std::ostream &str, const _Tp &obj, const std::string &name_r)
Definition: Xml.h:185
static const ValueType second
Definition: Date.h:40
Date operator/(const time_t rhs) const
Definition: Date.h:90
std::string printDate(TimeBase base_r) const
Definition: Date.h:194
std::string print(TimeFormat timeFormat_r, TimeBase base_r) const
Definition: Date.h:179