DateTime.h
1 /*
2  * Copyright (C) 2006 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef PT_DATETIME_H
30 #define PT_DATETIME_H
31 
32 #include <Pt/Api.h>
33 #include <Pt/Time.h>
34 #include <Pt/Date.h>
35 #include <string>
36 
37 namespace Pt {
38 
40 PT_API std::string dateTimeToString(const DateTime& dt);
41 
43 PT_API DateTime dateTimeFromString(const std::string& s);
44 
59 class DateTime
60 {
61  public:
65  { }
66 
69  DateTime(int y, unsigned mon, unsigned d,
70  unsigned h = 0, unsigned min = 0,
71  unsigned s = 0, unsigned ms = 0)
72  : _date(y, mon, d)
73  , _time(h, min, s, ms)
74  { }
75 
78  DateTime(const DateTime& dateTime)
79  : _date( dateTime.date() )
80  , _time( dateTime.time() )
81  { }
82 
85  DateTime& operator=(const DateTime& dateTime);
86 
89  void set(int year, unsigned month, unsigned day,
90  unsigned hour = 0, unsigned min = 0, unsigned sec = 0, unsigned msec = 0);
91 
94  void get(int& year, unsigned& month, unsigned& day,
95  unsigned& hour, unsigned& min, unsigned& sec, unsigned& msec) const;
96 
99  const Date& date() const
100  { return _date; }
101 
105  { return _date; }
106 
109  DateTime& setDate(const Date& dt)
110  { _date = dt; return *this; }
111 
114  const Time& time() const
115  { return _time; }
116 
120  { return _time; }
121 
124  DateTime& setTime(const Time& t)
125  { _time = t; return *this; }
126 
129  unsigned day() const
130  { return date().day(); }
131 
134  unsigned month() const
135  { return date().month(); }
136 
139  int year() const
140  { return date().year(); }
141 
144  unsigned hour() const
145  { return time().hour(); }
146 
149  unsigned minute() const
150  { return time().minute(); }
151 
154  unsigned second() const
155  { return time().second(); }
156 
159  unsigned msec() const
160  { return time().msec(); }
161 
164  std::string toIsoString() const
165  { return dateTimeToString(*this); }
166 
169  static DateTime fromIsoString(const std::string& s)
170  { return dateTimeFromString(s); }
171 
175  {
176  Pt::int64_t totalMSecs = ts.toMSecs();
177  Pt::int64_t days = totalMSecs / Time::MSecsPerDay;
178  Pt::int64_t overrun = totalMSecs % Time::MSecsPerDay;
179 
180  if( (-overrun) > _time.toMSecs() )
181  {
182  days -= 1;
183  }
184  else if( overrun + _time.toMSecs() > Time::MSecsPerDay)
185  {
186  days += 1;
187  }
188 
189  _date += static_cast<int>(days);
190  _time += Timespan(overrun * 1000);
191  return *this;
192  }
193 
197  {
198  Pt::int64_t totalMSecs = ts.toMSecs();
199  Pt::int64_t days = totalMSecs / Time::MSecsPerDay;
200  Pt::int64_t overrun = totalMSecs % Time::MSecsPerDay;
201 
202  if( overrun > _time.toMSecs() )
203  {
204  days += 1;
205  }
206  else if(_time.toMSecs() - overrun > Time::MSecsPerDay)
207  {
208  days -= 1;
209  }
210 
211  _date -= static_cast<int>(days);
212  _time -= Timespan( overrun * 1000 );
213  return *this;
214  }
215 
217  static bool isValid(int year, unsigned month, unsigned day,
218  unsigned hour, unsigned minute, unsigned second, unsigned msec);
219 
220  private:
222  DateTime(unsigned jd)
223  : _date(jd)
224  {}
225 
226  private:
227  Date _date;
228  Time _time;
229 };
230 
231 
236 PT_API void operator >>=(const SerializationInfo& si, DateTime& dt);
237 
242 PT_API void operator <<=(SerializationInfo& si, const DateTime& dt);
243 
248 inline DateTime operator+(const DateTime& dt, const Timespan& ts)
249 {
250  DateTime tmp = dt;
251  tmp += ts;
252  return tmp;
253 }
254 
259 inline Timespan operator-(const DateTime& first, const DateTime& second)
260 {
261  Pt::int64_t dayDiff = Pt::int64_t( first.date().julian() ) -
262  Pt::int64_t( second.date().julian() );
263 
264  Pt::int64_t milliSecDiff = Pt::int64_t( first.time().toMSecs() ) -
265  Pt::int64_t( second.time().toMSecs() );
266 
267  Pt::int64_t result = (dayDiff * Time::MSecsPerDay + milliSecDiff) * 1000;
268 
269  return Timespan(result);
270 }
271 
276 inline DateTime operator-(const DateTime& dt, const Timespan& ts)
277 {
278  DateTime tmp = dt;
279  tmp -= ts;
280  return tmp;
281 }
282 
287 inline bool operator< (const DateTime& a, const DateTime& b)
288 {
289  return a.date() < b.date()
290  || (a.date() == b.date()
291  && a.time() < b.time());
292 }
293 
298 inline bool operator<= (const DateTime& a, const DateTime& b)
299 {
300  return a.date() < b.date()
301  || (a.date() == b.date()
302  && a.time() <= b.time());
303 }
304 
309 inline bool operator> (const DateTime& a, const DateTime& b)
310 {
311  return a.date() > b.date()
312  || (a.date() == b.date()
313  && a.time() > b.time());
314 }
315 
320 inline bool operator>= (const DateTime& a, const DateTime& b)
321 {
322  return a.date() > b.date()
323  || (a.date() == b.date()
324  && a.time() >= b.time());
325 }
326 
331 inline bool operator==(const DateTime& a, const DateTime& b)
332 {
333  return a.date() == b.date() && a.time() == b.time();
334 }
335 
340 inline bool operator!=(const DateTime& a, const DateTime& b)
341 {
342  return a.date() != b.date() || a.time() != b.time();
343 }
344 
345 
346 inline DateTime& DateTime::operator=(const DateTime& dateTime)
347 {
348  _date = dateTime.date();
349  _time = dateTime.time();
350  return *this;
351 }
352 
353 
354 inline void DateTime::set(int y, unsigned mon, unsigned d,
355  unsigned h, unsigned min, unsigned s, unsigned ms)
356 {
357  _date.set(y, mon, d);
358  _time.set(h, min, s, ms);
359 }
360 
361 
362 inline void DateTime::get(int& y, unsigned& mon, unsigned& d,
363  unsigned& h, unsigned& min, unsigned& s, unsigned& ms) const
364 {
365  _date.get(y, mon, d);
366  _time.get(h, min, s, ms);
367 }
368 
369 
370 inline bool DateTime::isValid(int year, unsigned month, unsigned day,
371  unsigned hour, unsigned minute, unsigned second, unsigned msec)
372 {
373  return Date::isValid(year, month, day) && Time::isValid(hour, minute, second, msec);
374 }
375 
376 } // namespace Pt
377 
378 #endif // PT_DATETIME_H
DateTime(const DateTime &dateTime)
Copy Constructor.
Definition: DateTime.h:78
Represents time spans in microsecond resolution.
Definition: Timespan.h:62
DateTime()
Default Constructor.
Definition: DateTime.h:64
uint32_t toMSecs() const
Converts to milliseconds.
Definition: Time.h:173
Date expressed in year, month, and day.
Definition: Date.h:102
Timespan operator-(const DateTime &first, const DateTime &second)
Subtract two DateTimes.
Definition: DateTime.h:259
void get(int &year, unsigned &month, unsigned &day) const
Gets the year, month and day.
Definition: Date.h:448
unsigned month() const
Returns the month-part of the date.
Definition: Date.h:471
int year() const
Returns the year-part of the date.
Definition: DateTime.h:139
DateTime & setDate(const Date &dt)
Sets the date.
Definition: DateTime.h:109
static bool isValid(int y, int m, int d)
Returns true if values describe a valid date.
Definition: Date.h:529
unsigned julian() const
Returns the Date as a julian day.
Definition: Date.h:235
void set(int y, unsigned m, unsigned d)
Sets the date to a year, month and day.
Definition: Date.h:243
Combined Date and Time value.
Definition: DateTime.h:59
unsigned hour() const
Returns the hour-part.
Definition: Time.h:145
unsigned hour() const
Returns the hour-part of the Time.
Definition: DateTime.h:144
static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
Returns true if values are a valid time.
Definition: Time.h:298
const Time & time() const
Gets the time.
Definition: DateTime.h:114
unsigned msec() const
Returns the millisecond-part.
Definition: Time.h:166
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
DateTime & setTime(const Time &t)
Sets the time.
Definition: DateTime.h:124
unsigned month() const
Returns the month-part of the date.
Definition: DateTime.h:134
const Date & date() const
Gets the date.
Definition: DateTime.h:99
std::string toIsoString() const
Returns the date and time in ISO-format.
Definition: DateTime.h:164
void set(int year, unsigned month, unsigned day, unsigned hour=0, unsigned min=0, unsigned sec=0, unsigned msec=0)
Sets the date and time.
Definition: DateTime.h:354
unsigned day() const
Returns the day-part of the date.
Definition: Date.h:463
static bool isValid(int year, unsigned month, unsigned day, unsigned hour, unsigned minute, unsigned second, unsigned msec)
Returns true if values are a valid date and time.
Definition: DateTime.h:370
void get(unsigned &h, unsigned &m, unsigned &s, unsigned &ms) const
Get the time values.
Definition: Time.h:206
DateTime operator-(const DateTime &dt, const Timespan &ts)
Subtract a timespan.
Definition: DateTime.h:276
bool operator!=(const DateTime &a, const DateTime &b)
Returns true if not equal.
Definition: DateTime.h:340
unsigned minute() const
Returns the minute-part.
Definition: Time.h:152
static DateTime fromIsoString(const std::string &s)
Interprets a string as a date and time in ISO-format.
Definition: DateTime.h:169
void set(unsigned h, unsigned m, unsigned s, unsigned ms=0)
Sets the time.
Definition: Time.h:192
bool operator==(const DateTime &a, const DateTime &b)
Returns true if equal.
Definition: DateTime.h:331
unsigned msec() const
Returns the millisecond-part of the Time.
Definition: DateTime.h:159
void get(int &year, unsigned &month, unsigned &day, unsigned &hour, unsigned &min, unsigned &sec, unsigned &msec) const
Gets the date and time.
Definition: DateTime.h:362
int year() const
Returns the year-part of the date.
Definition: Date.h:479
unsigned second() const
Returns the second-part.
Definition: Time.h:159
DateTime & operator-=(const Timespan &ts)
Assignment by difference operator.
Definition: DateTime.h:196
Date & date()
Gets the date.
Definition: DateTime.h:104
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition: Timespan.h:291
DateTime & operator+=(const Timespan &ts)
Assignment by sum operator.
Definition: DateTime.h:174
DateTime operator+(const DateTime &dt, const Timespan &ts)
Add a timespan.
Definition: DateTime.h:248
Time & time()
Gets the time.
Definition: DateTime.h:119
DateTime(int y, unsigned mon, unsigned d, unsigned h=0, unsigned min=0, unsigned s=0, unsigned ms=0)
Construct to date and time.
Definition: DateTime.h:69
unsigned second() const
Returns the second-part of the Time.
Definition: DateTime.h:154
unsigned day() const
Returns the day-part of the date.
Definition: DateTime.h:129
unsigned minute() const
Returns the minute-part of the Time.
Definition: DateTime.h:149
Time expressed in hours, minutes, seconds and milliseconds.
Definition: Time.h:97
DateTime & operator=(const DateTime &dateTime)
Assignment operator.
Definition: DateTime.h:346