OmniSciDB  2e3a973ef4
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 int64_t dateTimeParse(std::string_view, unsigned const dim);
26 
27 template <>
28 int64_t dateTimeParse<kDATE>(std::string_view, unsigned const dim);
29 
30 template <>
31 int64_t dateTimeParse<kTIME>(std::string_view, unsigned const dim);
32 
33 template <>
34 int64_t dateTimeParse<kTIMESTAMP>(std::string_view, unsigned const dim);
35 
44  public:
45  enum class FormatType { Date, Time, Timezone };
46  std::optional<int64_t> parse(std::string_view const&, unsigned dim);
48  std::string_view unparsed() const;
49 
50  struct DateTime {
51  int64_t Y{1970}; // Full year
52  unsigned m{1}; // month (1-12)
53  unsigned d{1}; // day of month (1-31)
54  unsigned H{0}; // hour (0-23)
55  unsigned M{0}; // minute (0-59)
56  unsigned S{0}; // second (0-61)
57  unsigned n{0}; // fraction of a second in nanoseconds (0-999999999)
58  int z{0}; // timezone offset in seconds
59  std::optional<bool> p; // true if pm, false if am, nullopt if unspecified
60 
61  int64_t getTime(unsigned const dim) const;
62  friend std::ostream& operator<<(std::ostream&, DateTime const&);
63  };
64 
65  private:
68  std::string_view unparsed_;
69 
70  bool parseWithFormat(std::string_view format, std::string_view& str);
71  void resetDateTime();
72  bool updateDateTimeAndStr(char const field, std::string_view&);
73 };
std::optional< bool > p
int64_t dateTimeParse< kDATE >(std::string_view, unsigned const dim)
bool updateDateTimeAndStr(char const field, std::string_view &)
std::ostream & operator<<(std::ostream &os, const SessionInfo &session_info)
Definition: SessionInfo.cpp:53
std::string_view unparsed_
Constants for Builtin SQL Types supported by OmniSci.
bool parseWithFormat(std::string_view format, std::string_view &str)
int64_t dateTimeParse(std::string_view, unsigned const dim)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
std::optional< int64_t > parse(std::string_view const &, unsigned dim)
std::string_view unparsed() const
FormatType format_type_
int64_t dateTimeParse< kTIME >(std::string_view, unsigned const dim)
void setFormatType(FormatType)
int64_t dateTimeParse< kTIMESTAMP >(std::string_view, unsigned const dim)