time.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 by Tommi Maekitalo
3  * Copyright (C) 2006-2008 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_TIME_H
31 #define CXXTOOLS_TIME_H
32 
33 #include <cxxtools/timespan.h>
34 #include <string>
35 #include <stdexcept>
36 #include <stdint.h>
37 
38 namespace cxxtools
39 {
40 
41 class SerializationInfo;
42 
43 class InvalidTime : public std::invalid_argument
44 {
45  public:
46  InvalidTime();
47 
48  explicit InvalidTime(const std::string& what)
49  : std::invalid_argument(what)
50  { }
51 
52  ~InvalidTime() throw()
53  {}
54 };
55 
59 class Time
60 {
61  public:
62  static const unsigned MaxHours = 23;
63  static const unsigned HoursPerDay = 24;
64  static const unsigned MaxMinutes = 59;
65  static const unsigned MinutesPerHour = 60;
66  static const unsigned MinutesPerDay = 1440;
67  static const unsigned MaxSeconds = 59;
68  static const unsigned SecondsPerDay = 86400;
69  static const unsigned SecondsPerHour = 3600;
70  static const unsigned SecondsPerMinute = 60;
71  static const unsigned MSecsPerDay = 86400000;
72  static const unsigned MSecsPerHour = 3600000;
73  static const unsigned MSecsPerMinute = 60000;
74  static const unsigned MSecsPerSecond = 1000;
75  static const uint64_t USecsPerDay = static_cast<uint64_t>(MSecsPerDay) * 1000;
76 
79  Time()
80  : _usecs(0)
81  {}
82 
104  explicit Time(const std::string& d, const std::string& fmt = "%H:%M:%S%j");
105 
110  Time(unsigned h, unsigned m, unsigned s = 0, unsigned ms = 0, unsigned usec = 0)
111  {
112  set(h, m, s, ms, usec);
113  }
114 
117  unsigned hour() const
118  {
119  return unsigned(_usecs / 1000 / 1000 / 60 / 60);
120  }
121 
124  unsigned minute() const
125  {
126  return unsigned(_usecs / 1000 / 1000 / 60) % 60;
127  }
128 
131  unsigned second() const
132  {
133  return unsigned(_usecs / 1000 / 1000) % 60;
134  }
135 
138  unsigned msec() const
139  {
140  return unsigned(_usecs / 1000) % 1000;
141  }
142 
147  unsigned usec() const
148  {
149  return unsigned(_usecs % 1000000);
150  }
151 
152  unsigned totalMSecs() const
153  { return _usecs / 1000; }
154 
155  uint64_t totalUSecs() const
156  { return _usecs; }
157 
158  void setTotalMSecs(unsigned msecs)
159  { _usecs = msecs * 1000; }
160 
161  void setTotalUSecs(uint64_t m)
162  { _usecs = m; }
163 
169  void set(unsigned hour, unsigned min, unsigned sec, unsigned msec = 0, unsigned usec = 0)
170  {
171  if ( ! isValid(hour, min, sec, msec, usec) )
172  {
173  throw InvalidTime();
174  }
175 
176  _usecs = (((((static_cast<uint64_t>(hour) * 60 + min) * 60) + sec) * 1000) + msec) * 1000 + usec;
177  }
178 
183  void get(unsigned& h, unsigned& m, unsigned& s, unsigned& ms) const
184  {
185  h = hour();
186  m = minute();
187  s = second();
188  ms = msec();
189  }
190 
196  void get(unsigned& h, unsigned& m, unsigned& s, unsigned& ms, unsigned& us) const
197  {
198  h = hour();
199  m = minute();
200  s = second();
201  ms = msec();
202  us = usec();
203  }
204 
225  std::string toString(const std::string& fmt = "%H:%M:%S%j") const;
226 
229  Time& operator=(const Time& other)
230  { _usecs=other._usecs; return *this; }
231 
234  bool operator==(const Time& other) const
235  { return _usecs == other._usecs; }
236 
239  bool operator!=(const Time& other) const
240  { return _usecs != other._usecs; }
241 
244  bool operator<(const Time& other) const
245  { return _usecs < other._usecs; }
246 
249  bool operator<=(const Time& other) const
250  { return _usecs <= other._usecs; }
251 
254  bool operator>(const Time& other) const
255  { return _usecs > other._usecs; }
256 
259  bool operator>=(const Time& other) const
260  { return _usecs >= other._usecs; }
261 
264  Time& operator+=(const Timespan& ts);
265 
268  Time& operator-=(const Timespan& ts);
269 
272  friend Time operator+(const Time& time, const Timespan& ts);
273 
276  friend Time operator-(const Time& time, const Timespan& ts);
277 
280  friend Timespan operator-(const Time& a, const Time& b)
281  {
282  return Microseconds(
283  a._usecs >= b._usecs
284  ? double(a._usecs - b._usecs)
285  : -double(b._usecs - a._usecs));
286  }
287 
290  std::string toIsoString() const
291  { return toString(); }
292 
295  static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
296  {
297  return h < 24 && m < 60 && s < 60 && ms < 1000;
298  }
299 
302  static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms, unsigned usec)
303  {
304  return h < 24 && m < 60 && s < 60 && ms < 1000 && usec < 1000000;
305  }
306 
313  static Time fromIsoString(const std::string& s)
314  { return Time(s); }
315 
316  private:
318  uint64_t _usecs;
319  };
320 
321  void operator >>=(const SerializationInfo& si, Time& time);
322 
323  void operator <<=(SerializationInfo& si, const Time& time);
324 
327  inline Time operator+(const Time& time, const Timespan& ts)
328  {
329  Time t(time);
330  t += ts;
331  return t;
332  }
333 
336  inline Time operator-(const Time& time, const Timespan& ts)
337  {
338  Time t(time);
339  t -= ts;
340  return t;
341  }
342 
343 }
344 
345 #endif // CXXTOOLS_TIME_H