AceTime  1.1.2
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
ZoneManager.h
1 /*
2  * MIT License
3  * Copyright (c) 2019 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ZONE_MANAGER_H
7 #define ACE_TIME_ZONE_MANAGER_H
8 
9 #include "ZoneProcessorCache.h"
10 #include "ZoneRegistrar.h"
11 #include "TimeZoneData.h"
12 #include "TimeZone.h"
13 
14 namespace ace_time {
15 
29 template<typename ZI, typename ZR, typename ZSC>
30 class ZoneManager {
31  public:
32  const ZR& getRegistrar() const { return mZoneRegistrar; }
33 
34  TimeZone createForZoneInfo(const ZI* zoneInfo) {
35  if (! zoneInfo) return TimeZone::forError();
36  return TimeZone(zoneInfo, &mZoneProcessorCache);
37  }
38 
39  TimeZone createForZoneName(const char* name) {
40  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForName(name);
41  return createForZoneInfo(zoneInfo);
42  }
43 
44  TimeZone createForZoneId(uint32_t id) {
45  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForId(id);
46  return createForZoneInfo(zoneInfo);
47  }
48 
49  TimeZone createForZoneIndex(uint16_t index) {
50  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForIndex(index);
51  return createForZoneInfo(zoneInfo);
52  }
53 
60  switch (d.type) {
61  case TimeZone::kTypeError:
62  return TimeZone::forError();
63  case TimeZone::kTypeManual:
65  TimeOffset::forMinutes(d.stdOffsetMinutes),
66  TimeOffset::forMinutes(d.dstOffsetMinutes));
67  case TimeZone::kTypeBasic:
68  case TimeZone::kTypeExtended:
69  return createForZoneId(d.zoneId);
70  default:
71  return TimeZone();
72  }
73  }
74 
75  uint16_t indexForZoneName(const char* name) const {
76  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForName(name);
77  if (! zoneInfo) return 0;
78  return (zoneInfo - mZoneRegistrar.getZoneInfoForIndex(0));
79  }
80 
81  uint16_t indexForZoneId(uint32_t id) const {
82  const ZI* zoneInfo = mZoneRegistrar.getZoneInfoForId(id);
83  if (! zoneInfo) return 0;
84  return (zoneInfo - mZoneRegistrar.getZoneInfoForIndex(0));
85  }
86 
87  protected:
88  ZoneManager(uint16_t registrySize, const ZI* const* zoneRegistry):
89  mZoneRegistrar(registrySize, zoneRegistry),
90  mZoneProcessorCache() {}
91 
92  private:
93  // disable copy constructor and assignment operator
94  ZoneManager(const ZoneManager&) = delete;
95  ZoneManager& operator=(const ZoneManager&) = delete;
96 
97  const ZR mZoneRegistrar;
98  ZSC mZoneProcessorCache;
99 };
100 
101 #if 1
102 
105 template<uint16_t SIZE>
106 class BasicZoneManager: public ZoneManager<basic::ZoneInfo,
107  BasicZoneRegistrar, BasicZoneProcessorCache<SIZE>> {
108  public:
109  BasicZoneManager(uint16_t registrySize,
110  const basic::ZoneInfo* const* zoneRegistry):
111  ZoneManager<basic::ZoneInfo, BasicZoneRegistrar,
112  BasicZoneProcessorCache<SIZE>>(registrySize, zoneRegistry) {}
113 };
114 
118 template<uint16_t SIZE>
119 class ExtendedZoneManager: public ZoneManager<extended::ZoneInfo,
120  ExtendedZoneRegistrar, ExtendedZoneProcessorCache<SIZE>> {
121  public:
122  ExtendedZoneManager(uint16_t registrySize,
123  const extended::ZoneInfo* const* zoneRegistry):
124  ZoneManager<extended::ZoneInfo, ExtendedZoneRegistrar,
125  ExtendedZoneProcessorCache<SIZE>>(registrySize, zoneRegistry) {}
126 };
127 
128 #else
129 
130 // NOTE: The following typedef seems shorter and easier to maintain. The
131 // problem is that it makes error messages basically impossible to decipher
132 // because the immensely long full template class name is printed out. There
133 // seems to be no difference in code size between the two. The compiler seems
134 // to optimize away the vtables of the parent and child classes.
135 
136 template<uint8_t SIZE>
137 using BasicZoneManager = ZoneManager<basic::ZoneInfo,
139 
140 template<uint8_t SIZE>
141 using ExtendedZoneManager = ZoneManager<extended::ZoneInfo,
143 
144 #endif
145 
146 }
147 
148 #endif
ace_time::BasicZoneProcessorCache
Definition: ZoneProcessorCache.h:102
ace_time::ExtendedZoneProcessorCache
Definition: ZoneProcessorCache.h:108
ace_time::TimeZone::forError
static TimeZone forError()
Return a TimeZone representing an error condition.
Definition: TimeZone.h:139
ace_time::ZoneRegistrar
Class that allows looking up the ZoneInfo (ZI) from its TZDB identifier (e.g.
Definition: ZoneRegistrar.h:42
ace_time::BasicZoneManager
Definition: ZoneManager.h:106
ace_time::TimeOffset::forMinutes
static TimeOffset forMinutes(int16_t minutes)
Create TimeOffset from minutes from 00:00.
Definition: TimeOffset.h:83
ace_time::ZoneManager::createForTimeZoneData
TimeZone createForTimeZoneData(const TimeZoneData &d)
Create from the TimeZoneData created by TimeZone::toTimeZoneData().
Definition: ZoneManager.h:59
ace_time::ZoneManager
Returns the TimeZone given the zoneInfo, zoneName, or zoneId.
Definition: TimeZone.h:21
ace_time::TimeZoneData
Data structure that captures the internal state of a TimeZone object with enough information so that ...
Definition: TimeZoneData.h:27
ace_time::ExtendedZoneManager
Definition: ZoneManager.h:119
ace_time::TimeZone
Class that describes a time zone.
Definition: TimeZone.h:82
ace_time::TimeZone::forTimeOffset
static TimeZone forTimeOffset(TimeOffset stdOffset, TimeOffset dstOffset=TimeOffset())
Factory method to create from a UTC offset and an optional DST offset.
Definition: TimeZone.h:104
ace_time::TimeZoneData::zoneId
uint32_t zoneId
All of kTypeBasic, kTypeExtended, kTypeBasicManaged, kTypeExtendedManaged collapse down to a kTypeZon...
Definition: TimeZoneData.h:45