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/api.h>
34 #include <cxxtools/time.h>
35 #include <cxxtools/date.h>
36 #include <string>
37 #include <map>
38 #include <stdint.h>
39 
40 namespace cxxtools
41 {
42 
43 class SerializationInfo;
44 
48 class DateTime
49 {
50  public:
52  { }
53 
54  DateTime(int year, unsigned month, unsigned day,
55  unsigned hour = 0, unsigned minute = 0,
56  unsigned second = 0, unsigned msec = 0)
57  : _date(year, month, day)
58  , _time(hour, minute, second, msec)
59  { }
60 
61  DateTime(const DateTime& dateTime)
62  : _date( dateTime.date() )
63  , _time( dateTime.time() )
64  { }
65 
66  DateTime& operator=(const DateTime& dateTime);
67 
69  {}
70 
71  static DateTime fromJulianDays(unsigned julianDays)
72  {
73  return DateTime(julianDays);
74  }
75 
87  static inline DateTime fromMSecsSinceEpoch(const int64_t msecsSinceEpoch)
88  {
89  static const DateTime dt(1970, 1, 1);
90  Timespan ts(msecsSinceEpoch*1000);
91  return dt + ts;
92  }
93 
94  //DateTime& operator=(const DateTime& dateTime);
95 
96  DateTime& operator=(unsigned julianDay);
97 
98  void set(int year, unsigned month, unsigned day,
99  unsigned hour = 0, unsigned min = 0, unsigned sec = 0, unsigned msec = 0);
100 
101  void get(int& year, unsigned& month, unsigned& day,
102  unsigned& hour, unsigned& min, unsigned& sec, unsigned& msec) const;
103 
104  const Date& date() const
105  { return _date; }
106 
107  const Date& date()
108  { return _date; }
109 
111  { _date = date; return *this; }
112 
113  const Time& time() const
114  { return _time; }
115 
116  const Time& time()
117  { return _time; }
118 
120  { _time = time; return *this; }
121 
124  unsigned day() const
125  { return date().day(); }
126 
129  unsigned month() const
130  { return date().month(); }
131 
134  int year() const
135  { return date().year(); }
136 
139  unsigned hour() const
140  { return time().hour(); }
141 
144  unsigned minute() const
145  { return time().minute(); }
146 
149  unsigned second() const
150  { return time().second(); }
151 
154  unsigned msec() const
155  { return time().msec(); }
156 
166  int64_t msecsSinceEpoch() const;
167 
168  std::string toIsoString() const;
169 
170  static DateTime fromIsoString(const std::string& s);
171 
172  static bool isValid(int year, unsigned month, unsigned day,
173  unsigned hour, unsigned minute, unsigned second, unsigned msec);
174 
175 
176  bool operator==(const DateTime& rhs) const
177  {
178  return _date == rhs._date && _time == rhs._time ;
179  }
180 
181  bool operator!=(const DateTime& rhs) const
182  {
183  return !operator==(rhs);
184  }
185 
188  DateTime& operator+=(const Timespan& ts);
189 
192  DateTime& operator-=(const Timespan& ts);
193 
194  friend Timespan operator-(const DateTime& first, const DateTime& second);
195 
196  friend DateTime operator+(const DateTime& dt, const Timespan& ts);
197 
198  friend DateTime operator-(const DateTime& dt, const Timespan& ts);
199 
200  bool operator< (const DateTime& dt) const
201  {
202  return _date < dt._date
203  || (_date == dt._date
204  && _time < dt._time);
205  }
206 
207  bool operator<= (const DateTime& dt) const
208  {
209  return _date < dt._date
210  || (_date == dt._date
211  && _time <= dt._time);
212  }
213 
214  bool operator> (const DateTime& dt) const
215  {
216  return _date > dt._date
217  || (_date == dt._date
218  && _time > dt._time);
219  }
220 
221  bool operator>= (const DateTime& dt) const
222  {
223  return _date > dt._date
224  || (_date == dt._date
225  && _time >= dt._time);
226  }
227 
228  private:
229  DateTime(unsigned jd)
230  : _date(jd)
231  {}
232 
233  private:
234  Date _date;
235  Time _time;
236 };
237 
238 CXXTOOLS_API void operator >>=(const SerializationInfo& si, DateTime& dt);
239 
240 CXXTOOLS_API void operator <<=(SerializationInfo& si, const DateTime& dt);
241 
242 CXXTOOLS_API void convert(DateTime& dt, const std::string& s);
243 
244 CXXTOOLS_API void convert(std::string& str, const DateTime& dt);
245 
246 
247 inline DateTime DateTime::fromIsoString(const std::string& s)
248 {
249  DateTime dt;
250  convert(dt, s);
251  return dt;
252 }
253 
254 
255 inline std::string DateTime::toIsoString() const
256 {
257  std::string str;
258  convert(str, *this);
259  return str;
260 }
261 
262 
263 inline DateTime& DateTime::operator=(const DateTime& dateTime)
264 {
265  _date = dateTime.date();
266  _time = dateTime.time();
267  return *this;
268 }
269 
270 
271 inline DateTime& DateTime::operator=(unsigned julianDay)
272 {
273  _time = Time(0, 0, 0, 0);
274  _date.setJulian(julianDay);
275  return *this;
276 }
277 
278 
279 inline void DateTime::set(int year, unsigned month, unsigned day,
280  unsigned hour, unsigned minute, unsigned second, unsigned msec)
281 {
282  _date.set(year, month, day);
283  _time.set(hour, minute, second, msec);
284 }
285 
286 
287 inline void DateTime::get(int& y, unsigned& month, unsigned& d,
288  unsigned& h, unsigned& min, unsigned& s, unsigned& ms) const
289 {
290  _date.get(y, month, d);
291  _time.get(h, min, s, ms);
292 }
293 
294 
295 inline bool DateTime::isValid(int year, unsigned month, unsigned day,
296  unsigned hour, unsigned minute, unsigned second, unsigned msec)
297 {
298  return Date::isValid(year, month, day) && Time::isValid(hour, minute, second, msec);
299 }
300 
301 }
302 
303 #endif // CXXTOOLS_DATETIME_H