libUTL++
Time.h
1 #pragma once
2 
4 
5 #include <libutl/Duration.h>
6 
8 
9 UTL_NS_BEGIN;
10 
12 
18 {
26 };
27 
29 
35 {
48 };
49 
51 
57 {
65 };
66 
68 
74 {
79 };
80 
82 
146 
148 class Time : public Object
149 {
151 
152 public:
158  Time(const String& fmt, const String& str)
159  {
160  set(fmt, str);
161  }
162 
174  Time(uint_t year,
175  uint_t month = 1,
176  uint_t day = 1,
177  uint_t hour = 0,
178  uint_t min = 0,
179  double sec = 0,
180  bool isGMT = false)
181  {
182  set(year, month, day, hour, min, sec, isGMT);
183  }
184 
189  Time(time_t t)
190  {
191  set(t);
192  }
193 
194  virtual int compare(const Object& rhs) const;
195 
196  virtual void copy(const Object& rhs);
197 
198  virtual void serialize(Stream& stream, uint_t io, uint_t mode = ser_default);
199 
201 
202 
203  operator time_t() const
204  {
205  return _dateTime;
206  }
207 
209  time_t
210  get() const
211  {
212  return _dateTime;
213  }
214 
216  void getComponents(uint_t* year,
217  uint_t* month,
218  uint_t* day,
219  uint_t* wday = nullptr,
220  uint_t* hour = nullptr,
221  uint_t* min = nullptr,
222  double* sec = nullptr,
223  bool* isDST = nullptr,
224  bool isGMT = false) const;
225 
227  bool
228  isNull() const
229  {
230  return (getYear() == 1970);
231  }
232 
234  void
236  {
237  set((time_t)0);
238  }
239 
241  uint_t
242  getYear() const
243  {
244  struct tm* tmP = localtime(&_dateTime);
245  return tmP->tm_year;
246  }
247 
249  uint_t
250  getMonth() const
251  {
252  struct tm* tmP = localtime(&_dateTime);
253  return tmP->tm_mon;
254  }
255 
257  uint_t
258  getDay() const
259  {
260  struct tm* tmP = localtime(&_dateTime);
261  return tmP->tm_mday;
262  }
263 
265  uint_t
266  getDayOfWeek() const
267  {
268  struct tm* tmP = localtime(&_dateTime);
269  return tmP->tm_wday;
270  }
271 
273  uint_t
274  getHour() const
275  {
276  struct tm* tmP = localtime(&_dateTime);
277  return tmP->tm_hour;
278  }
279 
281  uint_t
282  getMin() const
283  {
284  struct tm* tmP = localtime(&_dateTime);
285  return tmP->tm_min;
286  }
287 
289  uint_t
290  getSec() const
291  {
292  struct tm* tmP = localtime(&_dateTime);
293  return tmP->tm_sec;
294  }
295 
297  uint_t
298  getTimeOfDay() const
299  {
300  uint_t h, m;
301  double s;
302  getComponents(nullptr, nullptr, nullptr, nullptr, &h, &m, &s);
303  return (h * 3600) + (m * 60) + s;
304  }
305 
307  uint_t maxDay() const;
308 
310  void
311  set(time_t dateTime)
312  {
313  _dateTime = dateTime;
314  }
315 
327  void set(uint_t year,
328  uint_t month,
329  uint_t day,
330  uint_t hour = 0,
331  uint_t min = 0,
332  double sec = 0,
333  bool isGMT = false);
334 
336  Time&
338  {
339  set(time(nullptr));
340  return *this;
341  }
343 
345 
346 
347  virtual String toString() const;
348 
354  String toString(const String& fmt) const;
355 
357  String toGMTstring() const;
358 
364  String toGMTstring(const String& fmt) const;
365 
371  void set(const String& fmt, const String& str);
373 
375 
376 
380  Time& roundDown(uint_t unit);
381 
386  Time& roundDown(const Duration& dur);
387 
392  Time& roundUp(uint_t unit);
393 
398  Time& roundUp(const Duration& dur);
400 
402 
403 
404  Time& operator=(time_t t)
405  {
406  set(t);
407  return self;
408  }
409 
411  Time operator+(int i) const
412  {
413  return operator+(Duration(i));
414  }
415 
417  Time operator+(const Duration& rhs) const
418  {
419  Time res = *this;
420  res._dateTime += (time_t)rhs.get();
421  return res;
422  }
423 
425  Time operator-(const Duration& rhs) const
426  {
427  Time res = *this;
428  res._dateTime -= (time_t)rhs.get();
429  return res;
430  }
431 
433  Time operator-(int i) const
434  {
435  return *this - Duration(i);
436  }
437 
439  Duration operator-(const Time& rhs) const;
440 
442  const Time& operator+=(const Duration& rhs)
443  {
444  _dateTime += (time_t)rhs.get();
445  return *this;
446  }
447 
449  const Time& operator-=(const Duration& rhs)
450  {
451  _dateTime -= (time_t)rhs.get();
452  return *this;
453  }
455 
457 
458 
459  static Time
461  {
462  Time res;
463  res.setCurrent();
464  return res;
465  }
466 
473  static Time today(uint_t hour = 0, uint_t min = 0, double sec = 0);
475 
477  static const char* abbrevWeekDayName[7];
479  static const char* weekDayName[7];
481  static const char* abbrevMonthName[12];
483  static const char* monthName[12];
485  static const uint_t monthDays[12];
486 
487 private:
488  void
489  init()
490  {
491  setNull();
492  }
493  void
494  deInit()
495  {
496  }
497 
498 private:
499  static int secondsFromGMT;
500  time_t _dateTime;
501 };
502 
504 
506 
508 
509 UTL_NS_END;
Tuesday.
Definition: Time.h:21
September.
Definition: Time.h:44
Monday.
Definition: Time.h:20
String toString(const FwdIt &begin, const FwdIt &end, const String &sep, bool key=false)
Obtain a string representation of a sequence (via Object::toString()).
void serialize(bool &b, Stream &stream, uint_t io, uint_t mode=ser_default)
Serialize a boolean.
T roundDown(const T &v, const T &m)
Round the given value v down to the nearest multiple of m.
Definition: util_inl.h:747
week
Definition: Time.h:63
Time(time_t t)
Constructor.
Definition: Time.h:189
second
Definition: Time.h:58
uint_t getMin() const
Get the minute [0,59].
Definition: Time.h:282
void deInit()
De-initialize UTL++.
March.
Definition: Time.h:38
May.
Definition: Time.h:40
bool isNull() const
Get the Null flag.
Definition: Time.h:228
DST auto select.
Definition: Time.h:77
default representation (via getSerializeMode())
Definition: util.h:75
Time & operator=(time_t t)
Assignment from time_t.
Definition: Time.h:404
#define UTL_CLASS_DECL(DC, BC)
Declaration of standard UTL++ functionality for a non-template class.
Definition: macros.h:688
April.
Definition: Time.h:39
uint_t getDayOfWeek() const
Get the day of week [0 = Sunday, .
Definition: Time.h:266
uint_t getTimeOfDay() const
Get the number of seconds since midnight.
Definition: Time.h:298
DST on.
Definition: Time.h:76
Time(const String &fmt, const String &str)
Constructor.
Definition: Time.h:158
Character string.
Definition: String.h:31
uint_t getDay() const
Get the day.
Definition: Time.h:258
Sunday.
Definition: Time.h:19
const Time & operator+=(const Duration &rhs)
Add the given duration to self.
Definition: Time.h:442
uint_t getMonth() const
Get the month.
Definition: Time.h:250
Saturday.
Definition: Time.h:25
tm_dst_t
DST flag.
Definition: Time.h:73
#define UTL_CLASS_SERIALIZE(className)
Implement serialize() for a class.
Definition: macros.h:625
static Time current()
Return the current time.
Definition: Time.h:460
void copy(T *dest, const T *src, size_t len)
Copy one array of objects to another.
Definition: util_inl.h:690
minute
Definition: Time.h:59
July.
Definition: Time.h:42
tm_unit_t
Unit of time.
Definition: Time.h:56
November.
Definition: Time.h:46
Time operator+(const Duration &rhs) const
Return self plus the given duration.
Definition: Time.h:417
tm_month_t
Month of year.
Definition: Time.h:34
leave DST alone
Definition: Time.h:78
Time operator+(int i) const
Return self plus the given number of seconds.
Definition: Time.h:411
half-hour
Definition: Time.h:60
Duration of time.
Definition: Duration.h:40
day
Definition: Time.h:62
December.
Definition: Time.h:47
uint_t getYear() const
Get the year.
Definition: Time.h:242
June.
Definition: Time.h:41
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
month
Definition: Time.h:64
Date/time.
Definition: Time.h:148
hour
Definition: Time.h:61
Stream I/O abstraction.
Definition: Stream.h:68
February.
Definition: Time.h:37
Time operator-(const Duration &rhs) const
Return self minus the given duration.
Definition: Time.h:425
Time & setCurrent()
Set to the current time.
Definition: Time.h:337
August.
Definition: Time.h:43
T get() const
Get the value.
Definition: Number.h:53
const T & min(const T &a, const T &b, const R &... args)
Return the smallest value among two or more provided values of the same type.
Definition: util_inl.h:61
October.
Definition: Time.h:45
Wednesday.
Definition: Time.h:22
Friday.
Definition: Time.h:24
January.
Definition: Time.h:36
tm_day_t
Day of week.
Definition: Time.h:17
DST off.
Definition: Time.h:75
Time(uint_t year, uint_t month=1, uint_t day=1, uint_t hour=0, uint_t min=0, double sec=0, bool isGMT=false)
Constructor.
Definition: Time.h:174
uint_t getHour() const
Get the hour [0,23].
Definition: Time.h:274
Root of UTL++ class hierarchy.
Definition: Object.h:52
int compare(bool lhs, bool rhs)
Compare two boolean values.
const Time & operator-=(const Duration &rhs)
Subtract the given duration from self.
Definition: Time.h:449
T roundUp(const T &v, const T &m)
Round the given value v up to the nearest multiple of m.
Definition: util_inl.h:728
Time operator-(int i) const
Return self minus the given number of seconds.
Definition: Time.h:433
Thursday.
Definition: Time.h:23
void init()
Initialize UTL++.
void setNull()
Set to null.
Definition: Time.h:235
uint_t getSec() const
Get the second [0,59].
Definition: Time.h:290