OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 55 of file DateTimeParser.h.

Member Enumeration Documentation

Enumerator
Date 
Time 
Timezone 

Definition at line 57 of file DateTimeParser.h.

57 { Date, Time, Timezone };

Member Function Documentation

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

Definition at line 244 of file DateTimeParser.cpp.

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

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

244  {
245  static std::vector<std::vector<std::string_view>> const& format_views = formatViews();
246  auto const& formats = format_views.at(static_cast<int>(format_type_));
247  for (std::string_view const format : formats) {
248  std::string_view str_unparsed = str;
249  if (parseWithFormat(format, str_unparsed)) {
250  unparsed_ = str_unparsed;
251  return dt_.getTime(dim);
252  }
253  }
254  unparsed_ = str;
255  return std::nullopt;
256 }
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 219 of file DateTimeParser.cpp.

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

Referenced by parse().

219  {
220  while (!format.empty()) {
221  if (format.front() == '%') {
222  eatSpace(str);
223  if (!updateDateTimeAndStr(format[1], str)) {
224  return false;
225  }
226  format.remove_prefix(2);
227  } else if (isspace(format.front())) {
228  eatSpace(format);
229  eatSpace(str);
230  } else if (!str.empty() && format.front() == str.front()) {
231  format.remove_prefix(1);
232  str.remove_prefix(1);
233  } else {
234  return false;
235  }
236  }
237  return true;
238 }
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 258 of file DateTimeParser.cpp.

References dt_.

Referenced by setFormatType().

258  {
259  dt_ = DateTime();
260 }

+ Here is the caller graph for this function:

void DateTimeParser::setFormatType ( FormatType  format_type)

Definition at line 262 of file DateTimeParser.cpp.

References format_type_, and resetDateTime().

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

262  {
263  resetDateTime();
264  format_type_ = format_type;
265 }
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 267 of file DateTimeParser.cpp.

References unparsed_.

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

267  {
268  return unparsed_;
269 }
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 273 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().

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

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

FormatType DateTimeParser::format_type_
private

Definition at line 79 of file DateTimeParser.h.

Referenced by parse(), and setFormatType().

std::string_view DateTimeParser::unparsed_
private

Definition at line 80 of file DateTimeParser.h.

Referenced by parse(), and unparsed().


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