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