OmniSciDB  8a228a1076
TimeGM Class Reference

#include <TimeGM.h>

Public Member Functions

time_t my_timegm (tm const *tm)
 
time_t my_timegm (tm const *tm, const time_t fsc, const int32_t dimen)
 
time_t my_timegm_days (tm const *tm)
 
time_t parse_fractional_seconds (uint64_t sfrac, const int32_t ntotal, const int32_t dimen)
 
time_t parse_meridians (const time_t &timeval, const char *p, const uint32_t hour, const int32_t dimen)
 

Static Public Member Functions

static TimeGMinstance ()
 

Private Member Functions

time_t get_overflow_underflow_safe_epoch (tm const *tm, const time_t fsc, const int32_t dimen)
 
int is_leap_year (int year)
 
int leap_days (int y1, int y2)
 
 TimeGM ()
 
virtual ~TimeGM ()
 

Private Attributes

const int monoff [12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
 

Detailed Description

Definition at line 32 of file TimeGM.h.

Constructor & Destructor Documentation

◆ TimeGM()

TimeGM::TimeGM ( )
inlineprivate

Definition at line 58 of file TimeGM.h.

58 {};

◆ ~TimeGM()

virtual TimeGM::~TimeGM ( )
inlineprivatevirtual

Definition at line 59 of file TimeGM.h.

59 {};

Member Function Documentation

◆ get_overflow_underflow_safe_epoch()

time_t TimeGM::get_overflow_underflow_safe_epoch ( tm const *  tm,
const time_t  fsc,
const int32_t  dimen 
)
private

Definition at line 126 of file timegm.cpp.

References my_timegm(), and to_string().

Referenced by instance(), and my_timegm().

128  {
129  const time_t epoch_seconds = my_timegm(tm);
130  const time_t epoch_scaled = epoch_seconds * static_cast<int64_t>(pow(10, dimen));
131  if (epoch_seconds &&
132  epoch_seconds != epoch_scaled / static_cast<int64_t>(pow(10, dimen))) {
133  throw std::runtime_error("Value out of range for TIMESTAMP(" + std::to_string(dimen) +
134  ").");
135  }
136  const time_t epoch_total = epoch_scaled + fsc;
137  if ((epoch_seconds > 0 && epoch_total < 0) || (epoch_seconds < 0 && epoch_total > 0)) {
138  throw std::runtime_error("Value out of range for TIMESTAMP(" + std::to_string(dimen) +
139  ").");
140  }
141 
142  return epoch_total;
143 }
time_t my_timegm(tm const *tm)
Definition: timegm.cpp:110
std::string to_string(char const *&&v)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ instance()

static TimeGM& TimeGM::instance ( )
inlinestatic

Definition at line 44 of file TimeGM.h.

References get_overflow_underflow_safe_epoch().

Referenced by DateTimeStringValidate< SQL_TYPE >::parseDateTimeString(), and DateTimeStringValidate< SQL_TYPE >::parseFractionalSeconds().

44  {
45  static TimeGM timegm{};
46  return timegm;
47  }
Definition: TimeGM.h:32
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ is_leap_year()

int TimeGM::is_leap_year ( int  year)
private

Definition at line 45 of file timegm.cpp.

Referenced by my_timegm_days().

45  {
46  return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
47 }
+ Here is the caller graph for this function:

◆ leap_days()

int TimeGM::leap_days ( int  y1,
int  y2 
)
private

Definition at line 49 of file timegm.cpp.

Referenced by my_timegm_days().

49  {
50  --y1;
51  --y2;
52  return (y2 / 4 - y1 / 4) - (y2 / 100 - y1 / 100) + (y2 / 400 - y1 / 400);
53 }
+ Here is the caller graph for this function:

◆ my_timegm() [1/2]

time_t TimeGM::my_timegm ( tm const *  tm)

Definition at line 110 of file timegm.cpp.

References my_timegm_days().

Referenced by get_overflow_underflow_safe_epoch(), and DateTimeStringValidate< SQL_TYPE >::parseDateTimeString().

110  {
111  time_t days;
112  time_t hours;
113  time_t minutes;
114  time_t seconds;
115 
116  days = my_timegm_days(tm);
117  hours = days * 24 + tm->tm_hour;
118  minutes = hours * 60 + tm->tm_min;
119  seconds = minutes * 60 + tm->tm_sec;
120 
121  seconds -= tm->tm_gmtoff;
122 
123  return seconds;
124 }
time_t my_timegm_days(tm const *tm)
Definition: timegm.cpp:97
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ my_timegm() [2/2]

time_t TimeGM::my_timegm ( tm const *  tm,
const time_t  fsc,
const int32_t  dimen 
)

Definition at line 145 of file timegm.cpp.

References get_overflow_underflow_safe_epoch().

145  {
146  return get_overflow_underflow_safe_epoch(tm, fsc, dimen);
147 }
time_t get_overflow_underflow_safe_epoch(tm const *tm, const time_t fsc, const int32_t dimen)
Definition: timegm.cpp:126
+ Here is the call graph for this function:

◆ my_timegm_days()

time_t TimeGM::my_timegm_days ( tm const *  tm)

Definition at line 97 of file timegm.cpp.

References is_leap_year(), leap_days(), and monoff.

Referenced by my_timegm().

97  {
98  int32_t year;
99  time_t days;
100  year = 1900 + tm->tm_year;
101  days = 365 * (year - 1970) + leap_days(1970, year);
102  days += monoff[tm->tm_mon];
103  if (tm->tm_mon > 1 && is_leap_year(year)) {
104  ++days;
105  }
106  days += tm->tm_mday - 1;
107  return days;
108 }
int leap_days(int y1, int y2)
Definition: timegm.cpp:49
int is_leap_year(int year)
Definition: timegm.cpp:45
const int monoff[12]
Definition: TimeGM.h:55
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ parse_fractional_seconds()

time_t TimeGM::parse_fractional_seconds ( uint64_t  sfrac,
const int32_t  ntotal,
const int32_t  dimen 
)

Definition at line 55 of file timegm.cpp.

Referenced by DateTimeStringValidate< SQL_TYPE >::parseFractionalSeconds().

57  {
58  int nfrac = log10(sfrac) + 1;
59  if (ntotal - nfrac > dimen) {
60  return 0;
61  }
62  if (ntotal >= 0 && ntotal < dimen) {
63  sfrac *= pow(10, dimen - ntotal);
64  } else if (ntotal > dimen) {
65  sfrac /= pow(10, ntotal - dimen);
66  }
67  return sfrac;
68 }
+ Here is the caller graph for this function:

◆ parse_meridians()

time_t TimeGM::parse_meridians ( const time_t &  timeval,
const char *  p,
const uint32_t  hour,
const int32_t  dimen 
)

Definition at line 70 of file timegm.cpp.

References kSecsPerHalfDay.

Referenced by DateTimeStringValidate< SQL_TYPE >::parseDateTimeString().

73  {
74  std::string time_str(p);
75  std::transform(time_str.begin(), time_str.end(), time_str.begin(), ::tolower);
76 
77  static std::regex pm_regex("(p[.]?m[.]?)");
78  std::smatch pm_match;
79  if (std::regex_search(time_str, pm_match, pm_regex)) {
80  return hour == 12 ? timeval
81  : timeval + kSecsPerHalfDay * static_cast<int64_t>(pow(10, dimen));
82  }
83 
84  static std::regex am_regex("(a[.]?m[.]?)");
85  std::smatch am_match;
86  if (std::regex_search(time_str, am_match, am_regex)) {
87  return hour == 12 ? timeval - kSecsPerHalfDay * static_cast<int64_t>(pow(10, dimen))
88  : timeval;
89  }
90 
91  return timeval;
92 }
static constexpr int64_t kSecsPerHalfDay
+ Here is the caller graph for this function:

Member Data Documentation

◆ monoff

const int TimeGM::monoff[12] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}
private

Definition at line 55 of file TimeGM.h.

Referenced by my_timegm_days().


The documentation for this class was generated from the following files: