| 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #ifndef TIMEZONE_HH |
| 20 | #define TIMEZONE_HH |
| 21 | |
| 22 | // This file is for timezone routines. |
| 23 | |
| 24 | #include "Adaptor.hh" |
| 25 | |
| 26 | #include <memory> |
| 27 | #include <stdexcept> |
| 28 | #include <stdint.h> |
| 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | namespace orc { |
| 33 | |
| 34 | static const int64_t SECONDS_PER_HOUR = 60 * 60; |
| 35 | static const int64_t SECONDS_PER_DAY = SECONDS_PER_HOUR * 24; |
| 36 | |
| 37 | /** |
| 38 | * A variant (eg. PST or PDT) of a timezone (eg. America/Los_Angeles). |
| 39 | */ |
| 40 | struct TimezoneVariant { |
| 41 | int64_t gmtOffset; |
| 42 | bool isDst; |
| 43 | std::string name; |
| 44 | |
| 45 | std::string toString() const; |
| 46 | }; |
| 47 | |
| 48 | /** |
| 49 | * A region that shares the same legal rules for wall clock time and |
| 50 | * day light savings transitions. They are typically named for the largest |
| 51 | * city in the region (eg. America/Los_Angeles or America/Mexico_City). |
| 52 | */ |
| 53 | class Timezone { |
| 54 | public: |
| 55 | virtual ~Timezone(); |
| 56 | |
| 57 | /** |
| 58 | * Get the variant for the given time (time_t). |
| 59 | */ |
| 60 | virtual const TimezoneVariant& getVariant(int64_t clk) const = 0; |
| 61 | |
| 62 | /** |
| 63 | * Get the number of seconds between the ORC epoch in this timezone |
| 64 | * and Unix epoch. |
| 65 | * ORC epoch is 1 Jan 2015 00:00:00 local. |
| 66 | * Unix epoch is 1 Jan 1970 00:00:00 UTC. |
| 67 | */ |
| 68 | virtual int64_t getEpoch() const = 0; |
| 69 | |
| 70 | /** |
| 71 | * Print the timezone to the stream. |
| 72 | */ |
| 73 | virtual void print(std::ostream&) const = 0; |
| 74 | |
| 75 | /** |
| 76 | * Get the version of the zone file. |
| 77 | */ |
| 78 | virtual uint64_t getVersion() const =0; |
| 79 | |
| 80 | /** |
| 81 | * Convert wall clock time of current timezone to UTC timezone |
| 82 | */ |
| 83 | virtual int64_t convertToUTC(int64_t clk) const = 0; |
| 84 | }; |
| 85 | |
| 86 | /** |
| 87 | * Get the local timezone. |
| 88 | * Results are cached. |
| 89 | */ |
| 90 | const Timezone& getLocalTimezone(); |
| 91 | |
| 92 | /** |
| 93 | * Get a timezone by name (eg. America/Los_Angeles). |
| 94 | * Results are cached. |
| 95 | */ |
| 96 | const Timezone& getTimezoneByName(const std::string& zone); |
| 97 | |
| 98 | /** |
| 99 | * Parse a set of bytes as a timezone file as if they came from filename. |
| 100 | */ |
| 101 | std::unique_ptr<Timezone> getTimezone(const std::string& filename, |
| 102 | const std::vector<unsigned char>& b); |
| 103 | |
| 104 | class TimezoneError: public std::runtime_error { |
| 105 | public: |
| 106 | TimezoneError(const std::string& what); |
| 107 | TimezoneError(const TimezoneError&); |
| 108 | virtual ~TimezoneError() ORC_NOEXCEPT; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * Represents the parsed POSIX timezone rule strings that are used to |
| 113 | * describe the future transitions, because they can go arbitrarily far into |
| 114 | * the future. |
| 115 | */ |
| 116 | class FutureRule { |
| 117 | public: |
| 118 | virtual ~FutureRule(); |
| 119 | virtual bool isDefined() const = 0; |
| 120 | virtual const TimezoneVariant& getVariant(int64_t clk) const = 0; |
| 121 | virtual void print(std::ostream& out) const = 0; |
| 122 | }; |
| 123 | |
| 124 | /** |
| 125 | * Parse the POSIX TZ string. |
| 126 | */ |
| 127 | std::shared_ptr<FutureRule> parseFutureRule(const std::string& ruleString); |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |