OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DateTimeParser.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "Shared/StringTransform.h"
20 #include "sqltypes.h"
21 
22 #include <optional>
23 #include <ostream>
24 #include <string_view>
25 
26 template <SQLTypes SQL_TYPE>
27 std::optional<int64_t> dateTimeParseOptional(std::string_view, unsigned const dim);
28 
29 template <>
30 std::optional<int64_t> dateTimeParseOptional<kDATE>(std::string_view, unsigned const dim);
31 
32 template <>
33 std::optional<int64_t> dateTimeParseOptional<kTIME>(std::string_view, unsigned const dim);
34 
35 template <>
36 std::optional<int64_t> dateTimeParseOptional<kTIMESTAMP>(std::string_view,
37  unsigned const dim);
38 
39 template <SQLTypes SQL_TYPE>
40 int64_t dateTimeParse(std::string_view const s, unsigned const dim) {
41  if (auto const time = dateTimeParseOptional<SQL_TYPE>(s, dim)) {
42  return *time;
43  } else {
44  throw std::runtime_error(cat("Invalid ", toString(SQL_TYPE), " string (", s, ')'));
45  }
46 }
47 
56  public:
57  enum class FormatType { Date, Time, Timezone };
58  std::optional<int64_t> parse(std::string_view const, unsigned dim);
60  std::string_view unparsed() const;
61 
62  struct DateTime {
63  int64_t Y{1970}; // Full year
64  unsigned m{1}; // month (1-12)
65  unsigned d{1}; // day of month (1-31)
66  unsigned H{0}; // hour (0-23)
67  unsigned M{0}; // minute (0-59)
68  unsigned S{0}; // second (0-61)
69  unsigned n{0}; // fraction of a second in nanoseconds (0-999999999)
70  int z{0}; // timezone offset in seconds
71  std::optional<bool> p; // true if pm, false if am, nullopt if unspecified
72 
73  int64_t getTime(unsigned const dim) const;
74  friend std::ostream& operator<<(std::ostream&, DateTime const&);
75  };
76 
77  private:
80  std::string_view unparsed_;
81 
82  bool parseWithFormat(std::string_view format, std::string_view& str);
83  void resetDateTime();
84  bool updateDateTimeAndStr(char const field, std::string_view&);
85 };
std::optional< bool > p
std::optional< int64_t > parse(std::string_view const, unsigned dim)
std::string cat(Ts &&...args)
std::optional< int64_t > dateTimeParseOptional(std::string_view, unsigned const dim)
bool updateDateTimeAndStr(char const field, std::string_view &)
std::string_view unparsed_
friend std::ostream & operator<<(std::ostream &, DateTime const &)
Constants for Builtin SQL Types supported by HEAVY.AI.
std::string toString(const QueryDescriptionType &type)
Definition: Types.h:64
bool parseWithFormat(std::string_view format, std::string_view &str)
std::optional< int64_t > dateTimeParseOptional< kTIME >(std::string_view str, unsigned const dim)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:33
std::optional< int64_t > dateTimeParseOptional< kDATE >(std::string_view str, unsigned const dim)
std::optional< int64_t > dateTimeParseOptional< kTIMESTAMP >(std::string_view str, unsigned const dim)
int64_t getTime(unsigned const dim) const
int64_t dateTimeParse(std::string_view const s, unsigned const dim)
FormatType format_type_
void setFormatType(FormatType)
std::string_view unparsed() const