EpcTools
An event based multi-threaded C++ development framework.
etime.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __etime_h_included
19 #define __etime_h_included
20 
23 
24 #include <sys/time.h>
25 #include <stdint.h>
26 
27 #include "ebase.h"
28 #include "estring.h"
29 
31 
32 #define TIME_SEP 1
33 #define DATE_SEP 2
34 #define MONTH_SEP 4
35 #define AM_SEP 8
36 #define PM_SEP 16
37 
38 #define MON_DAY_YEAR_ORDER 1
39 #define YEAR_MON_DAY_ORDER 2
40 #define YEAR_DAY_MON_ORDER 4
41 #define DAY_MON_YEAR_ORDER 8
42 #define MON_YEAR_DAY_ORDER 16
43 
44 #define DATE_FIELDS 7
45 #define SWAP_POSITION(a, b) \
46  { \
47  int temp_buf; \
48  temp_buf = a; \
49  a = b; \
50  b = temp_buf; \
51  }
52 #define GIVE_YEAR(year) \
53  do \
54  { \
55  year = year < 30 ? 2000 + year : year < 100 ? 1900 + year : year; \
56  } while (0)
57 
58 #define stringcmp_format(string1, string2, count) __builtin_strncasecmp(string1, string2, count)
59 #define format_localtime_s(a, b) localtime_r(b, a)
60 #define format_gmtime_s(a, b) gmtime_r(b, a)
61 #define format_sprintf_s __builtin_snprintf
62 
63 #define BASE_YEAR 1900
64 #define NUM_OF_DAYS_LEAPYEAR 366
65 #define NUM_OF_DAYS_YEAR 365
66 #define NUM_OF_DAYS_WEEK 7
67 
68 #define IF_LEAPYEAR(year) (!((year) % 4) && (((year) % 100) || !((year) % 400)))
69 
70 #define FORMAT_TIMEZONE timezone
71 
72 #define FORMAT_DAYLIGHT daylight
73 
74 size_t format_time_into_string(pStr st, size_t max_limit,
75  cpStr type_format, const struct tm *time_f, const struct timeval *tval);
76 
77 pStr format_time_into_specs(cpStr format, const struct tm *t, pStr pt, cpStr ptlim, const struct timeval *tv);
78 pStr convert_date_time_format(const Int n, cpStr format, pStr pt, cpStr ptlim);
79 pStr add_timeformat_to_string(cpStr str, pStr pt, cpStr ptlim);
80 
81 enum strftime_format
82 {
83 
84  NAME_DAY_WEEK_ABB = 'a',
85  FULL_DAY_NAME = 'A',
86  ABBRE_MON_NAME = 'b',
87  FULL_MON_NAME = 'B',
88  DATE_TIME = 'c',
89  CENTURY_NUMBER = 'C',
90  DEC_DATE_MON = 'd',
91  MON_DAY_YEAR = 'D',
92  DAY_AS_DECIMAL_0 = 'e',
93  ALTERNATE_ERA = 'E',
94  YEAR_MON_DAY = 'F',
95  TWO_DIG_YEAR = 'g',
96  FOUR_DIG_YEAR = 'G',
97  ABBRE_MON_NAME_2 = 'h',
98  HOUR_AS_24_CLK = 'H',
99  HOUR_AS_12_CLK = 'I',
100  DAY_AS_DECIMAL = 'j',
101  HOUR_AS_24_SINGLE = 'k',
102  HOUR_AS_12_SINGLE = 'l',
103  MON_AS_DECIMAL = 'm',
104  MIN_AS_DECIMAL = 'M',
105  NEW_LINE = 'n',
106  MODIFIER = 'O',
107  AM_PM = 'p',
108  am_pm = 'P',
109  TIME_AM_PM = 'r',
110  TIME_HOUR_MIN = 'R',
111  EPOCH_TIME = 's',
112  SECONDS_AS_DEC = 'S',
113  TAB_CHARACTER = 't',
114  TIME_IN_24_HOUR = 'T',
115  DAY_WEEK_AS_DEC = 'u',
116  WEEK_NUM_AS_DEC = 'U',
117  ISO_WEEK_NUM = 'V',
118  DAY_OF_WEEK = 'w',
119  WEEK_NUM_AS_DEC_MON = 'W',
120  DATE_WITHOUT_TIME = 'x',
121  TIME_WITHOUT_DATE = 'X',
122  YEAR_WITHOUT_CEN = 'y',
123  YEAR_WIT_CEN = 'Y',
124  HOUR_MIN_OFFSET = 'z',
125  TIMEZONE_NAME = 'Z',
126  DATE_TIME_TZ = '+',
127  LITERAL = '%',
128  RAND_VALUE_1 = 'i',
129  RAND_VALUE_2 = 'v',
130  MICRO_SEC = '1',
131  MILLI_SEC = '0',
132  NULL_CHAR = '\0'
133 };
134 
135 struct parse_date
136 {
137  UInt field_flags[DATE_FIELDS];
138  UInt field_parse_flags;
139  UInt field_values[DATE_FIELDS];
140  UInt field_count;
141 };
142 
143 struct ntp_time_t
144 {
145  UInt second;
146  UInt fraction;
147 };
148 
150 
199 class ETime
200 {
201 public:
204  {
205  *this = Now();
206  }
207 
209  ETime(const ETime &a)
210  {
211  *this = a;
212  }
213 
217  ETime(Int sec, Int usec)
218  {
219  m_time.tv_sec = sec;
220  m_time.tv_usec = usec;
221  }
222 
225  ETime(LongLong ms)
226  {
227  set(ms);
228  }
229 
238  ETime(Int year, Int mon, Int day, Int hour, Int min, Int sec, Bool isLocal);
239 
242  {
243  }
244 
248  ETime &operator=(const ETime &a)
249  {
250  m_time.tv_sec = a.m_time.tv_sec;
251  m_time.tv_usec = a.m_time.tv_usec;
252 
253  return *this;
254  }
258  ETime &operator=(const timeval &a)
259  {
260  set(a);
261  return *this;
262  }
266  ETime &operator=(LongLong ms)
267  {
268  set(ms);
269  return *this;
270  }
271 
275  Bool operator==(const ETime &a)
276  {
277  return ((m_time.tv_sec == a.m_time.tv_sec) && (m_time.tv_usec == a.m_time.tv_usec));
278  }
279 
283  Bool operator!=(const ETime &a)
284  {
285  return ((m_time.tv_sec != a.m_time.tv_sec) || (m_time.tv_usec != a.m_time.tv_usec));
286  }
287 
291  Bool operator<(const ETime &a)
292  {
293  if (m_time.tv_sec < a.m_time.tv_sec)
294  return true;
295  if (m_time.tv_sec == a.m_time.tv_sec)
296  {
297  if (m_time.tv_usec < a.m_time.tv_usec)
298  return true;
299  }
300 
301  return false;
302  }
303 
307  Bool operator>(const ETime &a)
308  {
309  if (m_time.tv_sec > a.m_time.tv_sec)
310  return true;
311  if (m_time.tv_sec == a.m_time.tv_sec)
312  {
313  if (m_time.tv_usec > a.m_time.tv_usec)
314  return true;
315  }
316 
317  return false;
318  }
319 
323  Bool operator>=(const ETime &a)
324  {
325  return !(*this < a);
326  }
327 
331  Bool operator<=(const ETime &a)
332  {
333  return !(*this > a);
334  }
335 
340  {
341  return add(a.m_time);
342  }
343 
347  ETime operator+(const timeval &t)
348  {
349  return add(t);
350  }
351 
356  {
357  return subtract(a.m_time);
358  }
359 
363  ETime operator-(const timeval &t)
364  {
365  return subtract(t);
366  }
367 
375  ETime add(Int days, Int hrs, Int mins, Int secs, Int usecs)
376  {
377  timeval t;
378  t.tv_sec =
379  (days * 86400) +
380  (hrs * 3600) +
381  (mins * 60) +
382  secs;
383  t.tv_usec = usecs;
384 
385  return add(t);
386  }
387 
391  ETime add(const timeval &t)
392  {
393  ETime nt(*this);
394 
395  nt.m_time.tv_sec += t.tv_sec;
396  nt.m_time.tv_usec += t.tv_usec;
397  if (nt.m_time.tv_usec >= 1000000)
398  {
399  nt.m_time.tv_usec -= 1000000;
400  nt.m_time.tv_sec++;
401  }
402 
403  return nt;
404  }
405 
413  ETime subtract(Int days, Int hrs, Int mins, Int secs, Int usecs)
414  {
415  timeval t;
416  t.tv_sec =
417  (days * 86400) +
418  (hrs * 3600) +
419  (mins * 60) +
420  secs;
421  t.tv_usec = usecs;
422 
423  return subtract(t);
424  }
425 
429  ETime subtract(const timeval &t)
430  {
431  ETime nt(*this);
432 
433  nt.m_time.tv_sec -= t.tv_sec;
434  nt.m_time.tv_usec -= t.tv_usec;
435  if (nt.m_time.tv_usec < 0)
436  {
437  nt.m_time.tv_usec += 1000000;
438  nt.m_time.tv_sec--;
439  }
440 
441  return nt;
442  }
443 
446  const timeval &getTimeVal()
447  {
448  return m_time;
449  }
450 
454  ETime &set(const timeval &a)
455  {
456  m_time.tv_sec = a.tv_sec;
457  m_time.tv_usec = a.tv_usec;
458  return *this;
459  }
460 
464  ETime & set(LongLong ms)
465  {
466  m_time.tv_sec = ms / 1000;
467  m_time.tv_usec = (ms % 1000) * 1000;
468  return *this;
469  }
470 
474  {
475  return m_time.tv_sec * 1000 + (m_time.tv_usec / 1000);
476  }
477 
480  Void getNTPTime(ntp_time_t &ntp) const;
483  Void setNTPTime(const ntp_time_t &ntp);
484 
487  Bool isValid() { return m_time.tv_sec != 0 || m_time.tv_usec != 0; }
488 
491  Int year();
494  Int month();
497  Int day();
500  Int hour();
503  Int minute();
506  Int second();
507 
510  static ETime Now();
515  Void Format(EString &dest, cpStr fmt, Bool local);
521  Void Format(pStr dest, Int maxsize, cpStr fmt, Bool local);
526  EString Format(cpStr fmt, Bool local) { EString s; Format(s,fmt,local); return s; }
531  Bool ParseDateTime(cpStr pszDate, Bool isLocal = True);
532 
533 private:
534  timeval m_time;
535 };
536 
537 #endif // #define __etime_h_included
ETime::setNTPTime
Void setNTPTime(const ntp_time_t &ntp)
Assigns the time represented by the NTP time to this ETime object.
Definition: etime.cpp:1188
ETime::operator=
ETime & operator=(const ETime &a)
Assignment operator.
Definition: etime.h:248
ETime::operator+
ETime operator+(const timeval &t)
Addition operator.
Definition: etime.h:347
format_time_into_specs
pStr format_time_into_specs(cpStr time_format, const struct tm *t, pStr dest, cpStr max_limit, const struct timeval *p_timeval)
Definition: etime.cpp:141
ETime::operator+
ETime operator+(const ETime &a)
Addition operator.
Definition: etime.h:339
ETime
Class for manipulating date and time of day values.
Definition: etime.h:199
ETime::Now
static ETime Now()
Retrieves the current time.
Definition: etime.cpp:1147
ETime::~ETime
~ETime()
Class destructor.
Definition: etime.h:241
ETime::second
Int second()
Retrieves the second.
Definition: etime.cpp:1139
True
#define True
True.
Definition: ebase.h:25
ETime::subtract
ETime subtract(const timeval &t)
Subtracts the timeval amount to the current value.
Definition: etime.h:429
ETime::operator=
ETime & operator=(LongLong ms)
Assignment operator.
Definition: etime.h:266
ebase.h
Macros for various standard C library functions and standard includes.
ETime::set
ETime & set(LongLong ms)
Assigns the specified millisecond value to this ETime object.
Definition: etime.h:464
ETime::operator-
ETime operator-(const timeval &t)
Subtraction operator.
Definition: etime.h:363
ETime::operator==
Bool operator==(const ETime &a)
Equality operator.
Definition: etime.h:275
ETime::operator>=
Bool operator>=(const ETime &a)
Greater than or equal to operator.
Definition: etime.h:323
format_time_into_string
size_t format_time_into_string(pStr st, size_t max_limit, cpStr type_format, struct tm *time_f, const struct timeval *tval)
Definition: etime.cpp:119
ETime::day
Int day()
Retrieves the day.
Definition: etime.cpp:1115
ETime::ETime
ETime(Int sec, Int usec)
Class constructor.
Definition: etime.h:217
ETime::operator<
Bool operator<(const ETime &a)
Less than operator.
Definition: etime.h:291
ETime::month
Int month()
Retrieves the month.
Definition: etime.cpp:1107
ETime::add
ETime add(const timeval &t)
Adds the timeval amount to the current value.
Definition: etime.h:391
ETime::getTimeVal
const timeval & getTimeVal()
Retrieves a reference to the timeval structure.
Definition: etime.h:446
ETime::ParseDateTime
Bool ParseDateTime(cpStr pszDate, Bool isLocal=True)
Parses the string containing the time and date.
Definition: etime.cpp:1177
convert_date_time_format
pStr convert_date_time_format(const Int n, cpStr date_time_format, pStr dest_buffer, cpStr max_dest_limit)
Definition: etime.cpp:461
ETime::Format
EString Format(cpStr fmt, Bool local)
Formats the date/time value as specified by the format string.
Definition: etime.h:526
ETime::ETime
ETime(LongLong ms)
Class constructor.
Definition: etime.h:225
ETime::Format
Void Format(EString &dest, cpStr fmt, Bool local)
Formats the date/time value as specified by the format string.
Definition: etime.cpp:1157
ETime::isValid
Bool isValid()
Indicates whether this ETime object is valid or not.
Definition: etime.h:487
ETime::subtract
ETime subtract(Int days, Int hrs, Int mins, Int secs, Int usecs)
Subtracts the specified days, hours minutes seconds and microseconds to the current value.
Definition: etime.h:413
ETime::minute
Int minute()
Retrieves the minute.
Definition: etime.cpp:1131
ETime::hour
Int hour()
Retrieves the hour.
Definition: etime.cpp:1123
ETime::getCassandraTimestmap
LongLong getCassandraTimestmap()
Converts this ETime value to one compatible with a Cassandra timestamp.
Definition: etime.h:473
ETime::operator-
ETime operator-(const ETime &a)
Subtraction operator.
Definition: etime.h:355
ETime::ETime
ETime()
Default constructor. Initializes to current time.
Definition: etime.h:203
ETime::operator!=
Bool operator!=(const ETime &a)
Inequality operator.
Definition: etime.h:283
EString
String class.
Definition: estring.h:30
ETime::ETime
ETime(const ETime &a)
Copy constructor.
Definition: etime.h:209
ETime::set
ETime & set(const timeval &a)
Assigns the specified timeval value to this ETime object.
Definition: etime.h:454
ETime::operator=
ETime & operator=(const timeval &a)
Assignment operator.
Definition: etime.h:258
ETime::operator>
Bool operator>(const ETime &a)
Greater than operator.
Definition: etime.h:307
ETime::year
Int year()
Retrieves the year.
Definition: etime.cpp:1099
ETime::operator<=
Bool operator<=(const ETime &a)
Less than or equal to operator.
Definition: etime.h:331
estring.h
Encapsulates and extends a std::string object.
ETime::getNTPTime
Void getNTPTime(ntp_time_t &ntp) const
Retrieves the NTP time representation of this ETime object.
Definition: etime.cpp:1182
ETime::add
ETime add(Int days, Int hrs, Int mins, Int secs, Int usecs)
Adds the specified days, hours minutes seconds and microseconds to the current value.
Definition: etime.h:375
add_timeformat_to_string
pStr add_timeformat_to_string(cpStr buffer, pStr dest_buffer, cpStr max_dest_limit)
Definition: etime.cpp:470