OmniSciDB  085a039ca4
 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 (c) 2020 OmniSci, 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 #pragma once
17 
18 #include "Shared/StringTransform.h"
19 #include "sqltypes.h"
20 
21 #include <optional>
22 #include <ostream>
23 #include <string_view>
24 
25 template <SQLTypes SQL_TYPE>
26 std::optional<int64_t> dateTimeParseOptional(std::string_view, unsigned const dim);
27 
28 template <>
29 std::optional<int64_t> dateTimeParseOptional<kDATE>(std::string_view, unsigned const dim);
30 
31 template <>
32 std::optional<int64_t> dateTimeParseOptional<kTIME>(std::string_view, unsigned const dim);
33 
34 template <>
35 std::optional<int64_t> dateTimeParseOptional<kTIMESTAMP>(std::string_view,
36  unsigned const dim);
37 
38 template <SQLTypes SQL_TYPE>
39 int64_t dateTimeParse(std::string_view const s, unsigned const dim) {
40  if (auto const time = dateTimeParseOptional<SQL_TYPE>(s, dim)) {
41  return *time;
42  } else {
43  throw std::runtime_error(cat("Invalid ", toString(SQL_TYPE), " string (", s, ')'));
44  }
45 }
46 
55  public:
56  enum class FormatType { Date, Time, Timezone };
57  std::optional<int64_t> parse(std::string_view const, unsigned dim);
59  std::string_view unparsed() const;
60 
61  struct DateTime {
62  int64_t Y{1970}; // Full year
63  unsigned m{1}; // month (1-12)
64  unsigned d{1}; // day of month (1-31)
65  unsigned H{0}; // hour (0-23)
66  unsigned M{0}; // minute (0-59)
67  unsigned S{0}; // second (0-61)
68  unsigned n{0}; // fraction of a second in nanoseconds (0-999999999)
69  int z{0}; // timezone offset in seconds
70  std::optional<bool> p; // true if pm, false if am, nullopt if unspecified
71 
72  int64_t getTime(unsigned const dim) const;
73  friend std::ostream& operator<<(std::ostream&, DateTime const&);
74  };
75 
76  private:
79  std::string_view unparsed_;
80 
81  bool parseWithFormat(std::string_view format, std::string_view& str);
82  void resetDateTime();
83  bool updateDateTimeAndStr(char const field, std::string_view&);
84 };
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 OmniSci.
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:31
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
std::string toString(const Executor::ExtModuleKinds &kind)
Definition: Execute.h:1453
int64_t dateTimeParse(std::string_view const s, unsigned const dim)
FormatType format_type_
void setFormatType(FormatType)
std::string_view unparsed() const