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 std::string dateTimeToString(const DateTime& dt, int* utcOffset);
44 
46 PT_API DateTime dateTimeFromString(const std::string& s);
47 
49 PT_API DateTime dateTimeFromString(const std::string& s, int* utcOffset);
50 
65 class DateTime
66 {
67  public:
71  { }
72 
75  DateTime(int y, unsigned mon, unsigned d,
76  unsigned h = 0, unsigned min = 0,
77  unsigned s = 0, unsigned ms = 0)
78  : _date(y, mon, d)
79  , _time(h, min, s, ms)
80  { }
81 
84  DateTime(const DateTime& dateTime)
85  : _date( dateTime.date() )
86  , _time( dateTime.time() )
87  { }
88 
91  DateTime& operator=(const DateTime& dateTime);
92 
95  void set(int year, unsigned month, unsigned day,
96  unsigned hour = 0, unsigned min = 0, unsigned sec = 0, unsigned msec = 0);
97 
100  void get(int& year, unsigned& month, unsigned& day,
101  unsigned& hour, unsigned& min, unsigned& sec, unsigned& msec) const;
102 
105  const Date& date() const
106  { return _date; }
107 
111  { return _date; }
112 
115  DateTime& setDate(const Date& dt)
116  { _date = dt; return *this; }
117 
120  const Time& time() const
121  { return _time; }
122 
126  { return _time; }
127 
130  DateTime& setTime(const Time& t)
131  { _time = t; return *this; }
132 
135  unsigned day() const
136  { return date().day(); }
137 
140  unsigned month() const
141  { return date().month(); }
142 
145  int year() const
146  { return date().year(); }
147 
150  unsigned hour() const
151  { return time().hour(); }
152 
155  unsigned minute() const
156  { return time().minute(); }
157 
160  unsigned second() const
161  { return time().second(); }
162 
165  unsigned msec() const
166  { return time().msec(); }
167 
170  std::string toIsoString() const
171  { return dateTimeToString(*this); }
172 
175  std::string toIsoString(int* utcOffset) const
176  { return dateTimeToString(*this, utcOffset); }
177 
180  static DateTime fromIsoString(const std::string& s)
181  { return dateTimeFromString(s); }
182 
185  static DateTime fromIsoString(const std::string& s, int* utcOffset)
186  { return dateTimeFromString(s, utcOffset); }
187 
191  {
192  Pt::int64_t totalMSecs = ts.toMSecs();
193  Pt::int64_t days = totalMSecs / Time::MSecsPerDay;
194  Pt::int64_t overrun = totalMSecs % Time::MSecsPerDay;
195 
196  if( (-overrun) > _time.toMSecs() )
197  {
198  days -= 1;
199  }
200  else if( overrun + _time.toMSecs() > Time::MSecsPerDay)
201  {
202  days += 1;
203  }
204 
205  _date += static_cast<int>(days);
206  _time += Timespan(overrun * 1000);
207  return *this;
208  }
209 
213  {
214  Pt::int64_t totalMSecs = ts.toMSecs();
215  Pt::int64_t days = totalMSecs / Time::MSecsPerDay;
216  Pt::int64_t overrun = totalMSecs % Time::MSecsPerDay;
217 
218  if( overrun > _time.toMSecs() )
219  {
220  days += 1;
221  }
222  else if(_time.toMSecs() - overrun > Time::MSecsPerDay)
223  {
224  days -= 1;
225  }
226 
227  _date -= static_cast<int>(days);
228  _time -= Timespan( overrun * 1000 );
229  return *this;
230  }
231 
233  static bool isValid(int year, unsigned month, unsigned day,
234  unsigned hour, unsigned minute, unsigned second, unsigned msec);
235 
236  private:
238  DateTime(unsigned jd)
239  : _date(jd)
240  {}
241 
242  private:
243  Date _date;
244  Time _time;
245 };
246 
247 
252 PT_API void operator >>=(const SerializationInfo& si, DateTime& dt);
253 
258 PT_API void operator <<=(SerializationInfo& si, const DateTime& dt);
259 
264 inline DateTime operator+(const DateTime& dt, const Timespan& ts)
265 {
266  DateTime tmp = dt;
267  tmp += ts;
268  return tmp;
269 }
270 
275 inline Timespan operator-(const DateTime& first, const DateTime& second)
276 {
277  Pt::int64_t dayDiff = Pt::int64_t( first.date().julian() ) -
278  Pt::int64_t( second.date().julian() );
279 
280  Pt::int64_t milliSecDiff = Pt::int64_t( first.time().toMSecs() ) -
281  Pt::int64_t( second.time().toMSecs() );
282 
283  Pt::int64_t result = (dayDiff * Time::MSecsPerDay + milliSecDiff) * 1000;
284 
285  return Timespan(result);
286 }
287 
292 inline DateTime operator-(const DateTime& dt, const Timespan& ts)
293 {
294  DateTime tmp = dt;
295  tmp -= ts;
296  return tmp;
297 }
298 
303 inline bool operator< (const DateTime& a, const DateTime& b)
304 {
305  return a.date() < b.date()
306  || (a.date() == b.date()
307  && a.time() < b.time());
308 }
309 
314 inline bool operator<= (const DateTime& a, const DateTime& b)
315 {
316  return a.date() < b.date()
317  || (a.date() == b.date()
318  && a.time() <= b.time());
319 }
320 
325 inline bool operator> (const DateTime& a, const DateTime& b)
326 {
327  return a.date() > b.date()
328  || (a.date() == b.date()
329  && a.time() > b.time());
330 }
331 
336 inline bool operator>= (const DateTime& a, const DateTime& b)
337 {
338  return a.date() > b.date()
339  || (a.date() == b.date()
340  && a.time() >= b.time());
341 }
342 
347 inline bool operator==(const DateTime& a, const DateTime& b)
348 {
349  return a.date() == b.date() && a.time() == b.time();
350 }
351 
356 inline bool operator!=(const DateTime& a, const DateTime& b)
357 {
358  return a.date() != b.date() || a.time() != b.time();
359 }
360 
361 
362 inline DateTime& DateTime::operator=(const DateTime& dateTime)
363 {
364  _date = dateTime.date();
365  _time = dateTime.time();
366  return *this;
367 }
368 
369 
370 inline void DateTime::set(int y, unsigned mon, unsigned d,
371  unsigned h, unsigned min, unsigned s, unsigned ms)
372 {
373  _date.set(y, mon, d);
374  _time.set(h, min, s, ms);
375 }
376 
377 
378 inline void DateTime::get(int& y, unsigned& mon, unsigned& d,
379  unsigned& h, unsigned& min, unsigned& s, unsigned& ms) const
380 {
381  _date.get(y, mon, d);
382  _time.get(h, min, s, ms);
383 }
384 
385 
386 inline bool DateTime::isValid(int year, unsigned month, unsigned day,
387  unsigned hour, unsigned minute, unsigned second, unsigned msec)
388 {
390 }
391 
392 } // namespace Pt
393 
394 #endif // PT_DATETIME_H
static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
Returns true if values are a valid time.
Definition: Time.h:298
Core module.
Definition: pt-gfx-images.dox:14
Date expressed in year, month, and day.
Definition: Date.h:104
unsigned minute() const
Returns the minute-part.
Definition: Time.h:152
unsigned day() const
Returns the day-part of the date.
Definition: Date.h:487
unsigned msec() const
Returns the millisecond-part.
Definition: Time.h:166
static DateTime fromIsoString(const std::string &s)
Interprets a string as a date and time in ISO-format.
Definition: DateTime.h:180
unsigned month() const
Returns the month-part of the date.
Definition: Date.h:495
void get(unsigned &h, unsigned &m, unsigned &s, unsigned &ms) const
Get the time values.
Definition: Time.h:206
Time & time()
Gets the time.
Definition: DateTime.h:125
void set(unsigned h, unsigned m, unsigned s, unsigned ms=0)
Sets the time.
Definition: Time.h:192
int year() const
Returns the year-part of the date.
Definition: DateTime.h:145
unsigned month() const
Returns the month-part of the date.
Definition: DateTime.h:140
int year() const
Returns the year-part of the date.
Definition: Date.h:503
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:75
unsigned second() const
Returns the second-part.
Definition: Time.h:159
unsigned minute() const
Returns the minute-part of the Time.
Definition: DateTime.h:155
std::string toIsoString(int *utcOffset) const
Returns the date and time in ISO-format.
Definition: DateTime.h:175
void get(int &year, unsigned &month, unsigned &day) const
Gets the year, month and day.
Definition: Date.h:472
const Time & time() const
Gets the time.
Definition: DateTime.h:120
unsigned msec() const
Returns the millisecond-part of the Time.
Definition: DateTime.h:165
unsigned hour() const
Returns the hour-part.
Definition: Time.h:145
bool operator==(const DateTime &a, const DateTime &b)
Returns true if equal.
Definition: DateTime.h:347
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
DateTime & setTime(const Time &t)
Sets the time.
Definition: DateTime.h:130
Time expressed in hours, minutes, seconds and milliseconds.
Definition: Time.h:98
DateTime(const DateTime &dateTime)
Copy Constructor.
Definition: DateTime.h:84
unsigned hour() const
Returns the hour-part of the Time.
Definition: DateTime.h:150
Timespan operator-(const DateTime &first, const DateTime &second)
Subtract two DateTimes.
Definition: DateTime.h:275
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition: Timespan.h:307
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:370
unsigned second() const
Returns the second-part of the Time.
Definition: DateTime.h:160
DateTime & setDate(const Date &dt)
Sets the date.
Definition: DateTime.h:115
uint32_t toMSecs() const
Converts to milliseconds.
Definition: Time.h:173
unsigned julian() const
Returns the Date as a julian day.
Definition: Date.h:236
unsigned day() const
Returns the day-part of the date.
Definition: DateTime.h:135
Represents time spans in microsecond resolution.
Definition: Timespan.h:63
bool operator!=(const DateTime &a, const DateTime &b)
Returns true if not equal.
Definition: DateTime.h:356
Date & date()
Gets the date.
Definition: DateTime.h:110
static bool isValid(int y, int m, int d)
Returns true if values describe a valid date.
Definition: Date.h:553
DateTime & operator-=(const Timespan &ts)
Assignment by difference operator.
Definition: DateTime.h:212
DateTime operator-(const DateTime &dt, const Timespan &ts)
Subtract a timespan.
Definition: DateTime.h:292
std::string toIsoString() const
Returns the date and time in ISO-format.
Definition: DateTime.h:170
void set(int y, unsigned m, unsigned d)
Sets the date to a year, month and day.
Definition: Date.h:244
DateTime operator+(const DateTime &dt, const Timespan &ts)
Add a timespan.
Definition: DateTime.h:264
DateTime & operator=(const DateTime &dateTime)
Assignment operator.
Definition: DateTime.h:362
Represents arbitrary types during serialization.
Definition: SerializationInfo.h:59
const Date & date() const
Gets the date.
Definition: DateTime.h:105
DateTime()
Default Constructor.
Definition: DateTime.h:70
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:386
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:378
DateTime & operator+=(const Timespan &ts)
Assignment by sum operator.
Definition: DateTime.h:190
static DateTime fromIsoString(const std::string &s, int *utcOffset)
Interprets a string as a date and time in ISO-format.
Definition: DateTime.h:185
Combined Date and Time value.
Definition: DateTime.h:66