datetime.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 by Tommi Maekitalo
3  * Copyright (C) 2006 by Marc Boris Duerner
4  * Copyright (C) 2006 by Stefan Bueder
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * As a special exception, you may use this file as part of a free
12  * software library without restriction. Specifically, if other files
13  * instantiate templates or use macros or inline functions from this
14  * file, or you compile this file and link it with other files to
15  * produce an executable, this file does not by itself cause the
16  * resulting executable to be covered by the GNU General Public
17  * License. This exception does not however invalidate any other
18  * reasons why the executable file might be covered by the GNU Library
19  * General Public License.
20  *
21  * This library is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24  * Lesser General Public License for more details.
25  *
26  * You should have received a copy of the GNU Lesser General Public
27  * License along with this library; if not, write to the Free Software
28  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29  */
30 #ifndef CXXTOOLS_DATETIME_H
31 #define CXXTOOLS_DATETIME_H
32 
33 #include <cxxtools/time.h>
34 #include <cxxtools/date.h>
35 #include <string>
36 
37 namespace cxxtools
38 {
39 
40 class SerializationInfo;
41 
45 class DateTime
46 {
47  public:
48  DateTime()
49  { }
50 
79  explicit DateTime(const std::string& d, const std::string& fmt = "%Y-%m-%d %H:%M:%S%j");
80 
81  DateTime(int year, unsigned month, unsigned day,
82  unsigned hour, unsigned minute,
83  unsigned second, unsigned msec = 0, unsigned usec = 0)
84  : _date(year, month, day)
85  , _time(hour, minute, second, msec, usec)
86  { }
87 
88  DateTime(const Date& date, const Time& time)
89  : _date(date),
90  _time(time)
91  { }
92 
105  {
106  static const DateTime dt(1970, 1, 1, 0, 0, 0);
107  return dt + sinceEpoch;
108  }
109 
111  static DateTime gmtime();
112 
114  static DateTime localtime();
115 
117  void set(int year, unsigned month, unsigned day,
118  unsigned hour, unsigned min, unsigned sec, unsigned msec = 0, unsigned usec = 0);
119 
121  void get(int& year, unsigned& month, unsigned& day,
122  unsigned& hour, unsigned& min, unsigned& sec, unsigned& msec) const;
123 
125  void get(int& year, unsigned& month, unsigned& day,
126  unsigned& hour, unsigned& min, unsigned& sec, unsigned& msec, unsigned& usec) const;
127 
129  const Date& date() const
130  { return _date; }
131 
133  DateTime& setDate(const Date& date)
134  { _date = date; return *this; }
135 
137  const Time& time() const
138  { return _time; }
139 
141  DateTime& setTime(const Time& time)
142  { _time = time; return *this; }
143 
146  unsigned day() const
147  { return date().day(); }
148 
151  unsigned month() const
152  { return date().month(); }
153 
156  int year() const
157  { return date().year(); }
158 
161  unsigned dayOfWeek() const
162  { return date().dayOfWeek(); }
163 
166  unsigned hour() const
167  { return time().hour(); }
168 
171  unsigned minute() const
172  { return time().minute(); }
173 
176  unsigned second() const
177  { return time().second(); }
178 
181  unsigned msec() const
182  { return time().msec(); }
183 
194 
224  std::string toString(const std::string& fmt = "%Y-%m-%d %H:%M:%S%j") const;
225 
226  std::string toIsoString() const
227  { return toString(); }
228 
229  static DateTime fromIsoString(const std::string& s)
230  { return DateTime(s); }
231 
232  static bool isValid(int year, unsigned month, unsigned day,
233  unsigned hour, unsigned minute, unsigned second, unsigned msec = 0);
234 
235 
236  bool operator==(const DateTime& rhs) const
237  {
238  return _date == rhs._date && _time == rhs._time ;
239  }
240 
241  bool operator!=(const DateTime& rhs) const
242  {
243  return !operator==(rhs);
244  }
245 
248  DateTime& operator+=(const Timespan& ts);
249 
252  DateTime& operator-=(const Timespan& ts)
253  { return operator+= (-ts); }
254 
255  friend Timespan operator-(const DateTime& first, const DateTime& second);
256 
257  friend DateTime operator+(const DateTime& dt, const Timespan& ts);
258 
259  friend DateTime operator-(const DateTime& dt, const Timespan& ts);
260 
261  bool operator< (const DateTime& dt) const
262  {
263  return _date < dt._date
264  || (_date == dt._date
265  && _time < dt._time);
266  }
267 
268  bool operator<= (const DateTime& dt) const
269  {
270  return _date < dt._date
271  || (_date == dt._date
272  && _time <= dt._time);
273  }
274 
275  bool operator> (const DateTime& dt) const
276  {
277  return _date > dt._date
278  || (_date == dt._date
279  && _time > dt._time);
280  }
281 
282  bool operator>= (const DateTime& dt) const
283  {
284  return _date > dt._date
285  || (_date == dt._date
286  && _time >= dt._time);
287  }
288 
289  private:
290  Date _date;
291  Time _time;
292 };
293 
294 void operator >>=(const SerializationInfo& si, DateTime& dt);
295 
296 void operator <<=(SerializationInfo& si, const DateTime& dt);
297 
298 
299 inline void DateTime::set(int year, unsigned month, unsigned day,
300  unsigned hour, unsigned minute, unsigned second, unsigned msec, unsigned usec)
301 {
302  _date.set(year, month, day);
303  _time.set(hour, minute, second, msec, usec);
304 }
305 
306 
307 inline void DateTime::get(int& y, unsigned& month, unsigned& d,
308  unsigned& h, unsigned& min, unsigned& s, unsigned& ms) const
309 {
310  _date.get(y, month, d);
311  _time.get(h, min, s, ms);
312 }
313 
314 
315 inline void DateTime::get(int& y, unsigned& month, unsigned& d,
316  unsigned& h, unsigned& min, unsigned& s, unsigned& ms, unsigned& usec) const
317 {
318  _date.get(y, month, d);
319  _time.get(h, min, s, ms, usec);
320 }
321 
322 
323 inline bool DateTime::isValid(int year, unsigned month, unsigned day,
324  unsigned hour, unsigned minute, unsigned second, unsigned msec)
325 {
326  return Date::isValid(year, month, day) && Time::isValid(hour, minute, second, msec);
327 }
328 
329 }
330 
331 #endif // CXXTOOLS_DATETIME_H