OmniSciDB  95562058bd
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DateTimeParser Class Reference

#include <DateTimeParser.h>

+ Collaboration diagram for DateTimeParser:

Classes

struct  DateTime
 

Public Types

enum  FormatType { FormatType::Date, FormatType::Time, FormatType::Timezone }
 

Public Member Functions

std::optional< int64_t > parse (std::string_view const &, unsigned dim)
 
void setFormatType (FormatType)
 
std::string_view unparsed () const
 

Private Member Functions

bool parseWithFormat (std::string_view format, std::string_view &str)
 
void resetDateTime ()
 
bool updateDateTimeAndStr (char const field, std::string_view &)
 

Private Attributes

DateTime dt_
 
FormatType format_type_
 
std::string_view unparsed_
 

Detailed Description

Set format_type_ and parse date/time/timestamp strings into (s,ms,us,ns) since the epoch based on given dim in (0,3,6,9) respectively. Basic idea is to parse given string by matching to formats ("%Y-%m-%d", "%m/%d/%Y", ...) until a valid parse is found. Save parsed values into a DateTime dt_ struct from which the final epoch-based int64_t value is calculated.

Definition at line 43 of file DateTimeParser.h.

Member Enumeration Documentation

Enumerator
Date 
Time 
Timezone 

Definition at line 45 of file DateTimeParser.h.

45 { Date, Time, Timezone };

Member Function Documentation

std::optional< int64_t > DateTimeParser::parse ( std::string_view const &  str,
unsigned  dim 
)

Definition at line 238 of file DateTimeParser.cpp.

References dt_, format_type_, anonymous_namespace{DateTimeParser.cpp}::formatViews(), DateTimeParser::DateTime::getTime(), parseWithFormat(), and unparsed_.

Referenced by dateTimeParse< kDATE >(), dateTimeParse< kTIME >(), and dateTimeParse< kTIMESTAMP >().

238  {
239  static std::vector<std::vector<std::string_view>> const& format_views = formatViews();
240  auto const& formats = format_views.at(static_cast<int>(format_type_));
241  for (std::string_view const& format : formats) {
242  std::string_view str_unparsed = str;
243  if (parseWithFormat(format, str_unparsed)) {
244  unparsed_ = str_unparsed;
245  return dt_.getTime(dim);
246  }
247  }
248  unparsed_ = str;
249  return std::nullopt;
250 }
std::string_view unparsed_
std::vector< std::vector< std::string_view > > formatViews()
bool parseWithFormat(std::string_view format, std::string_view &str)
int64_t getTime(unsigned const dim) const
FormatType format_type_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool DateTimeParser::parseWithFormat ( std::string_view  format,
std::string_view &  str 
)
private

Definition at line 213 of file DateTimeParser.cpp.

References anonymous_namespace{DateTimeParser.cpp}::eatSpace(), and updateDateTimeAndStr().

Referenced by parse().

213  {
214  while (!format.empty()) {
215  if (format.front() == '%') {
216  eatSpace(str);
217  if (!updateDateTimeAndStr(format[1], str)) {
218  return false;
219  }
220  format.remove_prefix(2);
221  } else if (isspace(format.front())) {
222  eatSpace(format);
223  eatSpace(str);
224  } else if (!str.empty() && format.front() == str.front()) {
225  format.remove_prefix(1);
226  str.remove_prefix(1);
227  } else {
228  return false;
229  }
230  }
231  return true;
232 }
bool updateDateTimeAndStr(char const field, std::string_view &)
void eatSpace(std::string_view &str)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void DateTimeParser::resetDateTime ( )
private

Definition at line 252 of file DateTimeParser.cpp.

References dt_.

Referenced by setFormatType().

252  {
253  dt_ = DateTime();
254 }

+ Here is the caller graph for this function:

void DateTimeParser::setFormatType ( FormatType  format_type)

Definition at line 256 of file DateTimeParser.cpp.

References format_type_, and resetDateTime().

Referenced by dateTimeParse< kDATE >(), dateTimeParse< kTIME >(), and dateTimeParse< kTIMESTAMP >().

256  {
257  resetDateTime();
258  format_type_ = format_type;
259 }
FormatType format_type_

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::string_view DateTimeParser::unparsed ( ) const

Definition at line 261 of file DateTimeParser.cpp.

References unparsed_.

Referenced by dateTimeParse< kDATE >(), dateTimeParse< kTIME >(), and dateTimeParse< kTIMESTAMP >().

261  {
262  return unparsed_;
263 }
std::string_view unparsed_

+ Here is the caller graph for this function:

bool DateTimeParser::updateDateTimeAndStr ( char const  field,
std::string_view &  str 
)
private

Definition at line 267 of file DateTimeParser.cpp.

References cat(), DateTimeParser::DateTime::d, dt_, anonymous_namespace{DateTimeParser.cpp}::eatMonth(), DateTimeParser::DateTime::H, DateTimeParser::DateTime::m, DateTimeParser::DateTime::M, anonymous_namespace{DateTimeParser.cpp}::month_prefixes, DateTimeParser::DateTime::n, DateTimeParser::DateTime::p, anonymous_namespace{DateTimeParser.cpp}::pow_10, DateTimeParser::DateTime::S, DateTimeParser::DateTime::Y, and DateTimeParser::DateTime::z.

Referenced by parseWithFormat().

267  {
268  switch (field) {
269  case 'Y':
270  if (auto const year = fromChars<int64_t>(str)) {
271  dt_.Y = *year;
272  return true;
273  }
274  return false;
275  case 'y':
276  if (2 <= str.size() && isdigit(str[0]) && isdigit(str[1])) {
277  if (auto const year = fromChars<unsigned>(str, 2)) {
278  dt_.Y = (*year < 69 ? 2000 : 1900) + *year;
279  return true;
280  }
281  }
282  return false;
283  case 'm':
284  if (auto const month = fromChars<unsigned>(str, 2)) {
285  if (1 <= *month && *month <= 12) {
286  dt_.m = *month;
287  return true;
288  }
289  }
290  return false;
291  case 'b':
292  if (3 <= str.size()) {
293  int const key =
294  std::tolower(str[0]) << 16 | std::tolower(str[1]) << 8 | std::tolower(str[2]);
295  constexpr auto end = month_prefixes.data() + month_prefixes.size();
296  // This is faster than a lookup into a std::unordered_map.
297  auto const ptr = std::find(month_prefixes.data(), end, key);
298  if (ptr != end) {
299  dt_.m = ptr - month_prefixes.data() + 1;
300  eatMonth(dt_.m, str);
301  return true;
302  }
303  }
304  return false;
305  case 'd':
306  if (auto const day = fromChars<unsigned>(str, 2)) {
307  if (1 <= *day && *day <= 31) {
308  dt_.d = *day;
309  return true;
310  }
311  }
312  return false;
313  case 'H':
314  if (auto const hour = fromChars<unsigned>(str, 2)) {
315  if (*hour <= 23) {
316  dt_.H = *hour;
317  return true;
318  }
319  }
320  return false;
321  case 'I':
322  if (auto const hour = fromChars<unsigned>(str, 2)) {
323  if (1 <= *hour && *hour <= 12) {
324  dt_.H = *hour;
325  return true;
326  }
327  }
328  return false;
329  case 'M':
330  if (auto const minute = fromChars<unsigned>(str, 2)) {
331  if (*minute <= 59) {
332  dt_.M = *minute;
333  return true;
334  }
335  }
336  return false;
337  case 'S':
338  if (auto const second = fromChars<unsigned>(str, 2)) {
339  if (*second <= 61) {
340  dt_.S = *second;
341  if (!str.empty() && str.front() == '.') {
342  str.remove_prefix(1);
343  size_t len = str.size();
344  if (auto const ns = fromChars<unsigned>(str, 9)) {
345  len -= str.size();
346  dt_.n = *ns * pow_10[9 - len];
347  } else {
348  return false; // Reject period not followed by a digit
349  }
350  }
351  return true;
352  }
353  }
354  return false;
355  case 'z':
356  // [-+]\d\d:?\d\d
357  if (5 <= str.size() && (str.front() == '-' || str.front() == '+') &&
358  isdigit(str[1]) && isdigit(str[2]) && isdigit(str[4]) &&
359  (str[3] == ':' ? 6 <= str.size() && isdigit(str[5]) : isdigit(str[3]))) {
360  char const* sep = &str[3];
361  int hours{0}, minutes{0};
362  std::from_chars(str.data() + 1, sep, hours);
363  sep += *sep == ':';
364  std::from_chars(sep, sep + 2, minutes);
365  dt_.z = (str.front() == '-' ? -60 : 60) * (60 * hours + minutes);
366  str.remove_prefix(sep - str.data() + 2);
367  return true;
368  }
369  return false;
370  case 'p':
371  // %p implies optional, so never return false
372  if (boost::algorithm::istarts_with(str, "am") ||
373  boost::algorithm::istarts_with(str, "pm") ||
374  boost::algorithm::istarts_with(str, "a.m.") ||
375  boost::algorithm::istarts_with(str, "p.m.")) {
376  dt_.p = std::tolower(str.front()) == 'p';
377  str.remove_prefix(std::tolower(str[1]) == 'm' ? 2 : 4);
378  } else {
379  dt_.p.reset();
380  }
381  return true;
382  default:
383  throw std::runtime_error(cat("Unrecognized format: %", field));
384  }
385 }
std::optional< bool > p
std::string cat(Ts &&...args)
void eatMonth(unsigned const month, std::string_view &str)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
constexpr std::array< int, 12 > month_prefixes

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

DateTime DateTimeParser::dt_
private

Definition at line 66 of file DateTimeParser.h.

Referenced by parse(), resetDateTime(), and updateDateTimeAndStr().

FormatType DateTimeParser::format_type_
private

Definition at line 67 of file DateTimeParser.h.

Referenced by parse(), and setFormatType().

std::string_view DateTimeParser::unparsed_
private

Definition at line 68 of file DateTimeParser.h.

Referenced by parse(), and unparsed().


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