libzypp  13.10.6
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 
22 namespace zypp
23 {
24 
26  //
27  // CLASS NAME : Date
28  //
31  class Date
32  {
33  friend std::ostream & operator<<( std::ostream & str, const Date & obj );
34 
35  public:
36 
37  typedef time_t ValueType;
38 
39  static const ValueType second = 1;
40  static const ValueType minute = 60;
41  static const ValueType hour = 3600;
42  static const ValueType day = 86400;
43  static const ValueType month28 = 2419200;
44  static const ValueType month29 = 2505600;
45  static const ValueType month30 = 2592000;
46  static const ValueType month31 = 2678400;
47  static const ValueType month = month30;
48  static const ValueType year365 = 31536000;
49  static const ValueType year366 = 31622400;
50  static const ValueType year = year365;
51 
53 
55  Date()
56  : _date( 0 )
57  {}
59  Date( ValueType date_r )
60  : _date( date_r )
61  {}
63  Date( const std::string & seconds_r );
64 
71  Date( const std::string & date_str, const std::string & format );
73  Date( const std::string & date_str, const std::string & format, TimeBase base_r );
74 
76  static Date now()
77  { return ::time( 0 ); }
78 
79  public:
81  operator ValueType() const
82  { return _date; }
83 
88  Date & operator+=( const time_t rhs ) { _date += rhs; return *this; }
89  Date & operator-=( const time_t rhs ) { _date -= rhs; return *this; }
90  Date & operator*=( const time_t rhs ) { _date *= rhs; return *this; }
91  Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
92 
93  Date & operator++(/*prefix*/) { _date += 1; return *this; }
94  Date & operator--(/*prefix*/) { _date -= 1; return *this; }
95 
96  Date operator++(int/*postfix*/) { return _date++; }
97  Date operator--(int/*postfix*/) { return _date--; }
99 
100  public:
107  std::string form( const std::string & format_r ) const
108  { return form( format_r, TB_LOCALTIME ); }
110  std::string form( const std::string & format_r, TimeBase base_r ) const;
111 
115  std::string asString() const
116  { return form( "%c" ); }
117 
121  std::string asSeconds() const
122  { return form( "%s" ); }
123 
124  private:
130  };
132 
134  inline std::ostream & operator<<( std::ostream & str, const Date & obj )
135  { return str << obj.asString(); }
136 
138  {
139  public:
140  DateFormatException( const std::string & msg ) : Exception( msg )
141  {}
142  };
143 
145 } // namespace zypp
147 #endif // ZYPP_DATE_H
static const ValueType day
Definition: Date.h:42
Date & operator+=(const time_t rhs)
Definition: Date.h:88
friend std::ostream & operator<<(std::ostream &str, const Date &obj)
static const ValueType minute
Definition: Date.h:40
static const ValueType month31
Definition: Date.h:46
const std::string & msg() const
Return the message string provided to the ctor.
Definition: Exception.h:185
Date()
Default ctor: 0.
Definition: Date.h:55
Date(ValueType date_r)
Ctor taking time_t value.
Definition: Date.h:59
DateFormatException(const std::string &msg)
Definition: Date.h:140
Date operator--(int)
Definition: Date.h:97
static const ValueType month30
Definition: Date.h:45
Date & operator/=(const time_t rhs)
Definition: Date.h:91
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
std::ostream & operator<<(std::ostream &str, const Date &obj)
Definition: Date.h:134
static const ValueType month28
Definition: Date.h:43
static const ValueType year
Definition: Date.h:50
Date & operator--()
Definition: Date.h:94
Date & operator*=(const time_t rhs)
Definition: Date.h:90
Date & operator++()
Definition: Date.h:93
static const ValueType hour
Definition: Date.h:41
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:107
static Date now()
Return the current time.
Definition: Date.h:76
std::string asString() const
Default string representation of Date.
Definition: Date.h:115
Date operator++(int)
Definition: Date.h:96
std::string asSeconds() const
Convert to string representation of calendar time in numeric form (like &quot;1029255142&quot;).
Definition: Date.h:121
time_t ValueType
Definition: Date.h:37
static const ValueType year366
Definition: Date.h:49
Date & operator-=(const time_t rhs)
Definition: Date.h:89
TimeBase
Definition: Date.h:52
static const ValueType second
Definition: Date.h:39