libzypp 17.37.10

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-core/base/Exception.h>
20#include <zypp-core/base/EnumClass.h>
21
23 namespace zypp
24{
25
27 //
28 // CLASS NAME : Date
29 //
32 class ZYPP_API Date
33 {
34 friend std::ostream & operator<<( std::ostream & str, const Date & obj );
35
36 public:
37
38 using ValueType = time_t;
39 using Duration = time_t;
40
41 static const ValueType second = 1;
42 static const ValueType minute = 60;
43 static const ValueType hour = 3600;
44 static const ValueType day = 86400;
45 static const ValueType month28 = 2419200;
46 static const ValueType month29 = 2505600;
47 static const ValueType month30 = 2592000;
48 static const ValueType month31 = 2678400;
49 static const ValueType month = month30;
50 static const ValueType year365 = 31536000;
51 static const ValueType year366 = 31622400;
52 static const ValueType year = year365;
53
54 enum TimeBase { TB_LOCALTIME, TB_UTC };
55
57 Date()
58 : _date( 0 )
59 {}
60
61 Date( ValueType date_r )
62 : _date( date_r )
63 {}
64
65 explicit Date( const std::string & seconds_r );
66
73 Date( const std::string & date_str, const std::string & format );
75 Date( const std::string & date_str, const std::string & format, TimeBase base_r );
76
78 static Date now()
79 { return ::time( 0 ); }
80
81 public:
83 operator ValueType() const
84 { return _date; }
85
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 Date operator/( const time_t rhs ) const { return _date / rhs; }
92
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 Date & operator/=( const time_t rhs ) { _date /= rhs; return *this; }
97
98 Date & operator++(/*prefix*/) { _date += 1; return *this; }
99 Date & operator--(/*prefix*/) { _date -= 1; return *this; }
100
101 Date operator++(int/*postfix*/) { return _date++; }
102 Date operator--(int/*postfix*/) { return _date--; }
104
105 public:
112 std::string form( const std::string & format_r ) const
113 { return form( format_r, TB_LOCALTIME ); }
114
115 std::string form( const std::string & format_r, TimeBase base_r ) const;
116
120 std::string asString() const
121 { return form( "%c" ); }
122
126 std::string asSeconds() const
127 { return form( "%s" ); }
128
129 public:
132
133 struct EDateFormatDef { enum Enum {
134 none,
135 calendar,
136 month,
137 year,
138 week,
139 weekday,
140 ordinal,
141 };};
142 using DateFormat = base::EnumClass<EDateFormatDef>;
143
145 struct ETimeFormatDef { enum Enum {
146 none,
147 seconds,
148 minutes,
149 hours,
150 };};
151 using TimeFormat = base::EnumClass<ETimeFormatDef>;
152
154 struct ETimeZoneFormatDef { enum Enum {
155 none,
156 name,
157 offset,
158 };};
159 using TimeZoneFormat = base::EnumClass<ETimeZoneFormatDef>;
160
166 std::string print( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
168 std::string print( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
169 { return print( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
170
171 std::string print( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
172 { return print( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
173
174 std::string print( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
175 { return print( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
176
177 std::string print( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
178 { return print( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
179
180 std::string print( TimeFormat timeFormat_r, TimeBase base_r ) const
181 { return print( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
182
183 std::string print( DateFormat dateFormat_r, TimeBase base_r ) const
184 { return print( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
185
186 std::string print( TimeBase base_r ) const
187 { return print( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
188
192 std::string printDate( DateFormat dateFormat_r = DateFormat::calendar, TimeBase base_r = TB_LOCALTIME ) const
193 { return print( dateFormat_r, TimeFormat::none, TimeZoneFormat::none, base_r ); }
194
195 std::string printDate( TimeBase base_r ) const
196 { return printDate( DateFormat::calendar, base_r ); }
197
201 std::string printTime( TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
202 { return print( DateFormat::none, timeFormat_r, timeZoneFormat_r, base_r ); }
203
204 std::string printTime( TimeZoneFormat timeZoneFormat_r , TimeBase base_r = TB_LOCALTIME ) const
205 { return printTime( TimeFormat::seconds, timeZoneFormat_r, base_r ); }
206
207 std::string printTime( TimeFormat timeFormat_r , TimeBase base_r ) const
208 { return printTime( timeFormat_r, TimeZoneFormat::name, base_r ); }
209
210 std::string printTime( TimeBase base_r ) const
211 { return printTime( TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
212
218 std::string printISO( DateFormat dateFormat_r = DateFormat::calendar, TimeFormat timeFormat_r = TimeFormat::seconds, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const;
220 std::string printISO( TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r = TimeZoneFormat::name, TimeBase base_r = TB_LOCALTIME ) const
221 { return printISO( DateFormat::calendar, timeFormat_r, timeZoneFormat_r, base_r ); }
222
223 std::string printISO( DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
224 { return printISO( dateFormat_r, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
225
226 std::string printISO( DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r ) const
227 { return printISO( dateFormat_r, timeFormat_r, TimeZoneFormat::name, base_r ); }
228
229 std::string printISO( TimeZoneFormat timeZoneFormat_r, TimeBase base_r = TB_LOCALTIME ) const
230 { return printISO( DateFormat::calendar, TimeFormat::seconds, timeZoneFormat_r, base_r ); }
231
232 std::string printISO( TimeFormat timeFormat_r, TimeBase base_r ) const
233 { return printISO( DateFormat::calendar, timeFormat_r, TimeZoneFormat::name, base_r ); }
234
235 std::string printISO( DateFormat dateFormat_r, TimeBase base_r ) const
236 { return printISO( dateFormat_r, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
237
238 std::string printISO( TimeBase base_r ) const
239 { return printISO( DateFormat::calendar, TimeFormat::seconds, TimeZoneFormat::name, base_r ); }
240
241
242 private:
247 ValueType _date;
248 };
249
250
252 inline std::ostream & operator<<( std::ostream & str, const Date & obj )
253 { return str << obj.asString(); }
254
258 std::ostream & dumpAsXmlOn( std::ostream & str, const Date & obj, const std::string & name_r = "date" ) ZYPP_API;
259
261 class ZYPP_API DateFormatException : public Exception
262 {
263 public:
264 DateFormatException( const std::string & msg ) : Exception( msg )
265 {}
266 };
267
269} // namespace zypp
271#endif // ZYPP_DATE_H
zypp::DateFormatException::DateFormatException
DateFormatException(const std::string &msg)
Definition Date.h:264
zypp::Date
Store and operate on date (time_t).
Definition Date.h:33
zypp::Date::_date
ValueType _date
Calendar time.
Definition Date.h:247
zypp::Date::operator+=
Date & operator+=(const time_t rhs)
Definition Date.h:93
zypp::Date::printTime
std::string printTime(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:204
zypp::Date::print
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:193
zypp::Date::year365
static const ValueType year365
Definition Date.h:50
zypp::Date::printTime
std::string printTime(TimeFormat timeFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:207
zypp::Date::printISO
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:209
zypp::Date::Date
Date()
Default ctor: 0.
Definition Date.h:57
zypp::Date::TimeBase
TimeBase
Definition Date.h:54
zypp::Date::TB_LOCALTIME
@ TB_LOCALTIME
Definition Date.h:54
zypp::Date::TB_UTC
@ TB_UTC
Definition Date.h:54
zypp::Date::hour
static const ValueType hour
Definition Date.h:43
zypp::Date::operator*=
Date & operator*=(const time_t rhs)
Definition Date.h:95
zypp::Date::printISO
std::string printISO(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:229
zypp::Date::operator++
Date operator++(int)
Definition Date.h:101
zypp::Date::day
static const ValueType day
Definition Date.h:44
zypp::Date::operator+
Date operator+(const time_t rhs) const
Definition Date.h:88
zypp::Date::printISO
std::string printISO(DateFormat dateFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:235
zypp::Date::month31
static const ValueType month31
Definition Date.h:48
zypp::Date::minute
static const ValueType minute
Definition Date.h:42
zypp::Date::printISO
std::string printISO(TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:220
zypp::Date::operator/
Date operator/(const time_t rhs) const
Definition Date.h:91
zypp::Date::print
std::string print(TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:186
zypp::Date::printISO
std::string printISO(TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:238
zypp::Date::print
std::string print(TimeFormat timeFormat_r, TimeZoneFormat timeZoneFormat_r=TimeZoneFormat::name, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:168
zypp::Date::print
std::string print(DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:174
zypp::Date::DateFormat
base::EnumClass< EDateFormatDef > DateFormat
'enum class DateFormat'
Definition Date.h:142
zypp::Date::month30
static const ValueType month30
Definition Date.h:47
zypp::Date::printISO
std::string printISO(TimeFormat timeFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:232
zypp::Date::month
static const ValueType month
Definition Date.h:49
zypp::Date::operator-
Date operator-(const time_t rhs) const
Definition Date.h:89
zypp::Date::operator-=
Date & operator-=(const time_t rhs)
Definition Date.h:94
zypp::Date::print
std::string print(TimeFormat timeFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:180
zypp::Date::year366
static const ValueType year366
Definition Date.h:51
zypp::Date::Duration
time_t Duration
Definition Date.h:39
zypp::Date::printTime
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:201
zypp::Date::TimeZoneFormat
base::EnumClass< ETimeZoneFormatDef > TimeZoneFormat
'enum class TimeZoneFormat'
Definition Date.h:159
zypp::Date::operator--
Date & operator--()
Definition Date.h:99
zypp::Date::ValueType
time_t ValueType
Definition Date.h:38
zypp::Date::asString
std::string asString() const
Default string representation of Date.
Definition Date.h:120
zypp::Date::print
std::string print(DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:171
zypp::Date::form
std::string form(const std::string &format_r) const
Return string representation according to format as localtime.
Definition Date.h:112
zypp::Date::printISO
std::string printISO(DateFormat dateFormat_r, TimeFormat timeFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:226
zypp::Date::Date
Date(ValueType date_r)
Ctor taking time_t value.
Definition Date.h:61
zypp::Date::month28
static const ValueType month28
Definition Date.h:45
zypp::Date::TimeFormat
base::EnumClass< ETimeFormatDef > TimeFormat
'enum class TimeFormat'
Definition Date.h:151
zypp::Date::month29
static const ValueType month29
Definition Date.h:46
zypp::Date::operator--
Date operator--(int)
Definition Date.h:102
zypp::Date::asSeconds
std::string asSeconds() const
Convert to string representation of calendar time in numeric form (like "1029255142").
Definition Date.h:126
zypp::Date::operator/=
Date & operator/=(const time_t rhs)
Definition Date.h:96
zypp::Date::second
static const ValueType second
Definition Date.h:41
zypp::Date::operator<<
friend std::ostream & operator<<(std::ostream &str, const Date &obj)
zypp::Date::now
static Date now()
Return the current time.
Definition Date.h:78
zypp::Date::print
std::string print(TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:177
zypp::Date::printDate
std::string printDate(TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:195
zypp::Date::printTime
std::string printTime(TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:210
zypp::Date::operator*
Date operator*(const time_t rhs) const
Definition Date.h:90
zypp::Date::printISO
std::string printISO(DateFormat dateFormat_r, TimeZoneFormat timeZoneFormat_r, TimeBase base_r=TB_LOCALTIME) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:223
zypp::Date::print
std::string print(DateFormat dateFormat_r, TimeBase base_r) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition Date.h:183
zypp::Date::year
static const ValueType year
Definition Date.h:52
zypp::Date::printDate
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:192
zypp::Date::operator++
Date & operator++()
Definition Date.h:98
zypp::Exception::Exception
Exception()
Default ctor.
Definition Exception.cc:94
zypp::Exception::msg
const std::string & msg() const
Return the message string provided to the ctor.
Definition Exception.h:206
form
std::string form(const char *format,...) __attribute__((format(printf
Printf style construction of std::string.
Definition String.cc:39
str
String related utilities and Regular expression matching.
zypp
Easy-to use interface to the ZYPP dependency resolver.
Definition Application.cc:20
zypp::dumpAsXmlOn
std::ostream & dumpAsXmlOn(std::ostream &str, const Repository &obj)
Definition Repository.cc:407
zypp::ZYPP_API
const Arch Arch_armv7hnl Arch_armv7nhl ZYPP_API
Definition Arch.h:247
zypp::Date::EDateFormatDef
Date formats for printing (use like 'enum class DateFormat')
Definition Date.h:133
zypp::Date::EDateFormatDef::Enum
Enum
Definition Date.h:133
zypp::Date::EDateFormatDef::calendar
@ calendar
2014-02-07
Definition Date.h:135
zypp::Date::EDateFormatDef::month
@ month
2014-02
Definition Date.h:136
zypp::Date::EDateFormatDef::none
@ none
""
Definition Date.h:134
zypp::Date::EDateFormatDef::weekday
@ weekday
2014-W06-5 (1 is Monday)
Definition Date.h:139
zypp::Date::EDateFormatDef::year
@ year
2014
Definition Date.h:137
zypp::Date::EDateFormatDef::ordinal
@ ordinal
2014-038
Definition Date.h:140
zypp::Date::EDateFormatDef::week
@ week
2014-W06
Definition Date.h:138
zypp::Date::ETimeFormatDef
Time formats for printing (use like 'enum class TimeFormat')
Definition Date.h:145
zypp::Date::ETimeFormatDef::Enum
Enum
Definition Date.h:145
zypp::Date::ETimeFormatDef::seconds
@ seconds
07:06:41
Definition Date.h:147
zypp::Date::ETimeFormatDef::none
@ none
""
Definition Date.h:146
zypp::Date::ETimeFormatDef::hours
@ hours
07
Definition Date.h:149
zypp::Date::ETimeFormatDef::minutes
@ minutes
07:06
Definition Date.h:148
zypp::Date::ETimeZoneFormatDef
Timezone indicator for printing (use like 'enum class TimeZoneFormat')
Definition Date.h:154
zypp::Date::ETimeZoneFormatDef::Enum
Enum
Definition Date.h:154
zypp::Date::ETimeZoneFormatDef::none
@ none
""
Definition Date.h:155
zypp::Date::ETimeZoneFormatDef::name
@ name
UTC, CET, ...
Definition Date.h:156
zypp::Date::ETimeZoneFormatDef::offset
@ offset
+00[:00]
Definition Date.h:157
EnumClass.h
Exception.h