OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DateTimeStringValidate< SQL_TYPE > Class Template Reference

#include <TimeGM.h>

+ Collaboration diagram for DateTimeStringValidate< SQL_TYPE >:

Public Member Functions

constexpr int64_t operator() (const std::string &datetime_str, const int32_t dimen)
 

Private Member Functions

constexpr int64_t parseDateTimeString ()
 
template<typename T >
constexpr void parseTimePart (T *&s, std::remove_const_t< T > *&p)
 
constexpr int64_t parseFractionalSeconds (char *&p)
 
const int64_t getEpochValue () const
 
template<typename EVAL_STRING , typename TARGET_CHAR , typename... EVAL_FORMATS>
auto detectFormatFromString (EVAL_STRING s, TARGET_CHAR &p, EVAL_FORMATS...eval_formats)
 

Private Attributes

std::string str_
 
int32_t dimen_
 
std::tm tm_ {0}
 

Detailed Description

template<SQLTypes SQL_TYPE>
class DateTimeStringValidate< SQL_TYPE >

Definition at line 63 of file TimeGM.h.

Member Function Documentation

template<SQLTypes SQL_TYPE>
template<typename EVAL_STRING , typename TARGET_CHAR , typename... EVAL_FORMATS>
auto DateTimeStringValidate< SQL_TYPE >::detectFormatFromString ( EVAL_STRING  s,
TARGET_CHAR &  p,
EVAL_FORMATS...  eval_formats 
)
inlineprivate

Definition at line 196 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::tm_.

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

198  {
199  ((p = strptime(s, eval_formats, &tm_)) || ...);
200  return;
201  }

+ Here is the caller graph for this function:

template<SQLTypes SQL_TYPE>
const int64_t DateTimeStringValidate< SQL_TYPE >::getEpochValue ( ) const
inlineprivate

Definition at line 186 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::str_.

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

186  {
187  // Check for unix time format
188  try {
189  return static_cast<int64_t>(std::stoll(str_));
190  } catch (const std::invalid_argument& ia) {
191  throw std::runtime_error("Invalid DATE/TIMESTAMP string " + str_);
192  }
193  }
std::string str_
Definition: TimeGM.h:203

+ Here is the caller graph for this function:

template<SQLTypes SQL_TYPE>
constexpr int64_t DateTimeStringValidate< SQL_TYPE >::operator() ( const std::string &  datetime_str,
const int32_t  dimen 
)
inline

Definition at line 65 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::dimen_, is_datetime(), DateTimeStringValidate< SQL_TYPE >::parseDateTimeString(), and DateTimeStringValidate< SQL_TYPE >::str_.

65  {
66  static_assert(is_datetime(SQL_TYPE));
67  str_ = datetime_str;
68  dimen_ = dimen;
69  return parseDateTimeString();
70  };
std::string str_
Definition: TimeGM.h:203
constexpr auto is_datetime(T sql_type)
Definition: sqltypes.h:194
constexpr int64_t parseDateTimeString()
Definition: TimeGM.h:73

+ Here is the call graph for this function:

template<SQLTypes SQL_TYPE>
constexpr int64_t DateTimeStringValidate< SQL_TYPE >::parseDateTimeString ( )
inlineprivate

Definition at line 73 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::detectFormatFromString(), DateTimeStringValidate< SQL_TYPE >::dimen_, DateTimeStringValidate< SQL_TYPE >::getEpochValue(), TimeGM::instance(), kDATE, kTIME, TimeGM::my_timegm(), TimeGM::parse_meridians(), DateTimeStringValidate< SQL_TYPE >::parseFractionalSeconds(), DateTimeStringValidate< SQL_TYPE >::parseTimePart(), DateTimeStringValidate< SQL_TYPE >::str_, and DateTimeStringValidate< SQL_TYPE >::tm_.

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

73  {
74  char* tp = nullptr;
75  if constexpr (SQL_TYPE == kTIME) {
76  char const* x = str_.c_str();
77  parseTimePart(x, tp);
78  tm_.tm_mday = 1;
79  tm_.tm_mon = 0;
80  tm_.tm_year = 70;
81  return static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
82  }
84  str_.c_str(), tp, "%Y-%m-%d", "%m/%d/%Y", "%d-%b-%y", "%d/%b/%Y");
85  if (!tp) {
86  return getEpochValue();
87  }
88  char* p = nullptr;
89  parseTimePart(tp, p);
90  if constexpr (SQL_TYPE == kDATE) {
91  return static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
92  }
93  // handle fractional seconds
94  int64_t timeval = 0;
95  if (dimen_ > 0) {
96  const int64_t frac = parseFractionalSeconds(p);
97  timeval = static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_, frac, dimen_));
98  } else {
99  timeval = static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
100  if (*p == '.') {
101  p++;
102  }
103  }
104  if (*p != '\0') {
105  uint32_t hour = 0;
106  sscanf(tp, "%u", &hour);
107  timeval = static_cast<int64_t>(TimeGM::instance().parse_meridians(
108  static_cast<time_t>(timeval), p, hour, dimen_));
109  }
110  return timeval;
111  }
constexpr void parseTimePart(T *&s, std::remove_const_t< T > *&p)
Definition: TimeGM.h:114
Definition: sqltypes.h:52
time_t parse_meridians(const time_t &timeval, const char *p, const uint32_t hour, const int32_t dimen)
Definition: timegm.cpp:67
std::string str_
Definition: TimeGM.h:203
time_t my_timegm(tm const *tm)
Definition: timegm.cpp:108
const int64_t getEpochValue() const
Definition: TimeGM.h:186
Definition: sqltypes.h:56
constexpr int64_t parseFractionalSeconds(char *&p)
Definition: TimeGM.h:170
auto detectFormatFromString(EVAL_STRING s, TARGET_CHAR &p, EVAL_FORMATS...eval_formats)
Definition: TimeGM.h:196
static TimeGM & instance()
Definition: TimeGM.h:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<SQLTypes SQL_TYPE>
constexpr int64_t DateTimeStringValidate< SQL_TYPE >::parseFractionalSeconds ( char *&  p)
inlineprivate

Definition at line 170 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::dimen_, TimeGM::instance(), and TimeGM::parse_fractional_seconds().

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

170  {
171  if (*p == '.') {
172  p++;
173  uint64_t frac_num = 0;
174  int ntotal = 0;
175  sscanf(p, "%" SCNu64 "%n", &frac_num, &ntotal);
176  return static_cast<int64_t>(
177  TimeGM::instance().parse_fractional_seconds(frac_num, ntotal, dimen_));
178  } else if (*p == '\0') {
179  return 0;
180  } else { // check for misleading/unclear syntax
181  throw std::runtime_error("Unclear syntax for leading fractional seconds: " +
182  std::string(p));
183  }
184  }
time_t parse_fractional_seconds(uint64_t sfrac, const int32_t ntotal, const int32_t dimen)
Definition: timegm.cpp:52
static TimeGM & instance()
Definition: TimeGM.h:44

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

template<SQLTypes SQL_TYPE>
template<typename T >
constexpr void DateTimeStringValidate< SQL_TYPE >::parseTimePart ( T *&  s,
std::remove_const_t< T > *&  p 
)
inlineprivate

Definition at line 114 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::detectFormatFromString(), kDATE, kTIME, and DateTimeStringValidate< SQL_TYPE >::str_.

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

114  {
115  if constexpr (SQL_TYPE == kDATE) {
116  if (*s == 'T' || *s == ' ') {
117  ++s;
118  }
119  detectFormatFromString(s, p, "%z");
120  return;
121  } else if constexpr (SQL_TYPE == kTIME) {
122  if (*s == 'T') {
123  ++s;
124  }
125  detectFormatFromString(s, p, "%T %z", "%T", "%H%M%S", "%R");
126  if (!p) {
127  throw std::runtime_error("Invalid TIME string " + str_);
128  }
129  return;
130  } else {
131  if (*s == 'T' || *s == ' ' || *s == ':') {
132  ++s;
133  } else {
134  throw std::runtime_error("Invalid TIMESTAMP break string " + std::string(s));
135  }
136  detectFormatFromString(s, p, "%T %z", "%T", "%H%M%S", "%R");
137  if (!p) {
138  // check for weird customer format and remove decimal seconds from string if there
139  // is a period followed by a number
140  char* startptr = nullptr;
141  char* endptr = nullptr;
142  // find last decimal in string
143  int loop = strlen(s);
144  while (loop > 0) {
145  if (s[loop] == '.') {
146  // found last period
147  startptr = &s[loop];
148  break;
149  }
150  loop--;
151  }
152  if (startptr) {
153  // look for space
154  endptr = strchr(startptr, ' ');
155  if (endptr) {
156  // ok we found a start and and end remove the decimal portion will need to
157  // capture this for later
158  memmove(startptr, endptr, strlen(endptr) + 1);
159  }
160  }
162  s, p, "%T %z", "%I . %M . %S %p"); // customers weird '.' separated date
163  }
164  if (!p) {
165  throw std::runtime_error("Invalid TIMESTAMP time string " + std::string(s));
166  }
167  }
168  }
Definition: sqltypes.h:52
std::string str_
Definition: TimeGM.h:203
Definition: sqltypes.h:56
auto detectFormatFromString(EVAL_STRING s, TARGET_CHAR &p, EVAL_FORMATS...eval_formats)
Definition: TimeGM.h:196

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

template<SQLTypes SQL_TYPE>
std::tm DateTimeStringValidate< SQL_TYPE >::tm_ {0}
private

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