ztsdb
zone.hpp
1 // (C) 2016 Leonardo Silvestri
2 //
3 // This file is part of ztsdb.
4 //
5 // ztsdb is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // ztsdb is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with ztsdb. If not, see <http://www.gnu.org/licenses/>.
17 
18 
19 #ifndef ZONE_HPP
20 #define ZONE_HPP
21 
22 
23 #include <memory>
24 #include <map>
25 #include <unordered_map>
26 #include <set>
27 #include <string>
28 #include <boost/filesystem.hpp>
29 #include "tz.hpp"
30 #include "../globals.hpp"
31 
32 
34 namespace tz {
35 
36  namespace fsys = boost::filesystem;
37 
38  struct Zone {
39  friend std::string to_string(const Zone& z);
40  friend std::string summary(const Zone& z);
41  friend struct Zones;
42 
43  Zone(fsys::path p); // will throw
44 
45  std::pair<Global::dtime::duration, const char*> getoffset(Global::dtime dt) const;
46 
47  Global::dtime::duration getoffset(Global::dtime dt, int& pos) const;
48  Global::dtime::duration getoffset(Global::dtime dt, int& pos, int dir) const;
49 
50  std::set<Global::dtime::duration> getReverseOffset(Global::dtime dt) const;
51 
52  private:
53  // Reverse lookup of offsets of times that already have a time
54  // zone offset applied. The set can contain 0 elements in the case
55  // of an impossible date representation, 1 element if there is no
56  // ambiguity, and 2 elements when there is two potential offsets
57  // that can be applied:
58  std::map<time_t, std::set<int>> rl;
59  // the state as read by the iana-provided 'tzload' function:
60  std::unique_ptr<state> s;
61 
62  void buildRl(); // build the reverse lookup map
63  };
64 
65 
66  struct Zones {
67  friend std::string to_string(const Zones& zs);
68 
69  Zones();
70  Zones(fsys::path p); // will throw
71 
72  void init(fsys::path p); // will throw
73 
74  const Zone& find(const std::string& s) const;
75  // Global::dtime::duration getoffset(const std::string& abbrev) const;
76 
77  private:
78  std::unordered_map<std::string, Zone> m;
79  };
80 
81 
82  std::string to_string(const Zone& z);
83  std::string summary(const Zone& z);
84  std::string to_string(const Zones& zs);
85 
86 }
87 
88 
89 
90 #endif
tz::Zone
Definition: zone.hpp:38
tz
Timezone handling and temporal types and functions depending on timezones.
Definition: period.hpp:28
tz::Zones
Definition: zone.hpp:66