OmniSciDB  49c0524acb
 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, char *&p)
 
constexpr int64_t parseFractionalSeconds (char *&p)
 
const int64_t getEpochValue () const
 
template<typename EVAL_STRING , typename TARGET_CHAR , typename... EVAL_FORMATS>
void 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 59 of file TimeGM.h.

Member Function Documentation

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

Definition at line 189 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::tm_.

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

191  {
192  ((p = strptime(s, eval_formats, &tm_)) || ...);
193  }

+ Here is the caller graph for this function:

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

Definition at line 179 of file TimeGM.h.

References DateTimeStringValidate< SQL_TYPE >::str_.

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

179  {
180  // Check for unix time format
181  try {
182  return static_cast<int64_t>(std::stoll(str_));
183  } catch (const std::invalid_argument& ia) {
184  throw std::runtime_error("Invalid DATE/TIMESTAMP string " + str_);
185  }
186  }
std::string str_
Definition: TimeGM.h:195

+ 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 61 of file TimeGM.h.

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

61  {
62  static_assert(is_datetime(SQL_TYPE));
63  str_ = datetime_str;
64  dimen_ = dimen;
65  return parseDateTimeString();
66  };
std::string str_
Definition: TimeGM.h:195
constexpr auto is_datetime(T sql_type)
Definition: sqltypes.h:191
constexpr int64_t parseDateTimeString()
Definition: TimeGM.h:69

+ Here is the call graph for this function:

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

Definition at line 69 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 >::str_, and DateTimeStringValidate< SQL_TYPE >::tm_.

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

69  {
70  char* tp = nullptr;
71  if constexpr (SQL_TYPE == kTIME) {
72  tm_.tm_hour = tm_.tm_min = tm_.tm_sec = -1;
73  parseTimePart<const char*>(str_.c_str(), tp);
74  tm_.tm_mday = 1;
75  tm_.tm_mon = 0;
76  tm_.tm_year = 70;
77  return static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
78  }
80  str_.c_str(), tp, "%Y-%m-%d", "%m/%d/%Y", "%d-%b-%y", "%d/%b/%Y");
81  if (!tp) {
82  return getEpochValue();
83  }
84  char* p = nullptr;
85  parseTimePart<char*&>(tp, p);
86  if constexpr (SQL_TYPE == kDATE) {
87  return static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
88  }
89  // handle fractional seconds
90  int64_t timeval = 0;
91  if (dimen_ > 0) {
92  const int64_t frac = parseFractionalSeconds(p);
93  timeval = static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_, frac, dimen_));
94  } else {
95  timeval = static_cast<int64_t>(TimeGM::instance().my_timegm(&tm_));
96  if (*p == '.') {
97  p++;
98  }
99  }
100  if (*p != '\0') {
101  uint32_t hour = 0;
102  sscanf(tp, "%u", &hour);
103  timeval = static_cast<int64_t>(TimeGM::instance().parse_meridians(
104  static_cast<time_t>(timeval), p, hour, dimen_));
105  }
106  return timeval;
107  }
Definition: sqltypes.h:51
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:195
time_t my_timegm(const struct tm *tm)
Definition: timegm.cpp:108
const int64_t getEpochValue() const
Definition: TimeGM.h:179
Definition: sqltypes.h:55
constexpr int64_t parseFractionalSeconds(char *&p)
Definition: TimeGM.h:163
void detectFormatFromString(EVAL_STRING s, TARGET_CHAR &p, EVAL_FORMATS...eval_formats)
Definition: TimeGM.h:189
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 163 of file TimeGM.h.

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

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

163  {
164  if (*p == '.') {
165  p++;
166  uint64_t frac_num = 0;
167  int ntotal = 0;
168  sscanf(p, "%" SCNu64 "%n", &frac_num, &ntotal);
169  return static_cast<int64_t>(
170  TimeGM::instance().parse_fractional_seconds(frac_num, ntotal, dimen_));
171  } else if (*p == '\0') {
172  return 0;
173  } else { // check for misleading/unclear syntax
174  throw std::runtime_error("Unclear syntax for leading fractional seconds: " +
175  std::string(p));
176  }
177  }
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 ( s,
char *&  p 
)
inlineprivate

Definition at line 110 of file TimeGM.h.

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

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

+ Here is the call graph for this function:

Member Data Documentation


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