AceTime  3.0.0
Date and time classes for Arduino that support timezones from the TZ Database.
Zone.h
1 /*
2  * MIT License
3  * Copyright (c) 2019 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_ZONE_DATA_H
7 #define ACE_TIME_ZONE_DATA_H
8 
9 #include <AceCommon.h> // KString
10 #include "../zoneinfo/infos.h"
11 #include "TimeOffset.h"
12 
13 class Print;
14 
15 namespace ace_time {
16 
24 template <typename D>
25 class ZoneTemplate {
26  public:
31  ZoneTemplate(const typename D::ZoneInfo* zoneInfo):
32  mZoneInfoBroker(zoneInfo) {}
33 
39  ZoneTemplate(const typename D::ZoneInfoBroker& zoneInfo):
40  mZoneInfoBroker(zoneInfo) {}
41 
42  // Use default copy constructor and assignment operator
43  ZoneTemplate(const ZoneTemplate&) = default;
44  ZoneTemplate& operator=(const ZoneTemplate&) = default;
45 
47  bool isNull() const { return mZoneInfoBroker.isNull(); }
48 
50  void printNameTo(Print& printer) const {
51  const __FlashStringHelper* name = mZoneInfoBroker.name();
52  typename D::ZoneContextBroker zoneContext =
53  mZoneInfoBroker.zoneContext();
54  ace_common::KString kname(
55  name, zoneContext.fragments(), zoneContext.numFragments());
56  kname.printTo(printer);
57  }
58 
64  void printShortNameTo(Print& printer) const {
65  const __FlashStringHelper* name = mZoneInfoBroker.name();
66  const __FlashStringHelper* shortName = zoneinfo::findShortName(name);
67  ace_common::printReplaceCharTo(printer, shortName, '_', ' ');
68  }
69 
71  uint32_t zoneId() const {
72  return mZoneInfoBroker.zoneId();
73  }
74 
77  uint8_t numEras = mZoneInfoBroker.numEras();
78  typename D::ZoneEraBroker zeb = mZoneInfoBroker.era(numEras - 1);
79  return TimeOffset::forSeconds(zeb.offsetSeconds());
80  }
81 
83  ace_common::KString kname() const {
84  const auto* name = isNull() ? nullptr : mZoneInfoBroker.name();
85  typename D::ZoneContextBroker zoneContext =
86  mZoneInfoBroker.zoneContext();
87  return ace_common::KString(
88  name, zoneContext.fragments(), zoneContext.numFragments());
89  }
90 
91  private:
92  typename D::ZoneInfoBroker mZoneInfoBroker;
93 };
94 
95 using BasicZone = ZoneTemplate<basic::Info>;
96 using ExtendedZone = ZoneTemplate<extended::Info>;
97 using CompleteZone = ZoneTemplate<complete::Info>;
98 
99 }
100 
101 #endif
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC,...
Definition: TimeOffset.h:56
static TimeOffset forSeconds(int32_t seconds)
Create TimeOffset from seconds from 00:00.
Definition: TimeOffset.h:96
A thin wrapper around a ZoneInfo data structure to provide a stable API access to some useful ZoneInf...
Definition: Zone.h:25
ZoneTemplate(const typename D::ZoneInfoBroker &zoneInfo)
Constructor from a Info:ZoneInfoBroker, used by various ZoneProcessor.
Definition: Zone.h:39
ace_common::KString kname() const
Return the name as a KString.
Definition: Zone.h:83
ZoneTemplate(const typename D::ZoneInfo *zoneInfo)
Constructor from a raw Info::ZoneInfo* pointer, intended for manual inspection of a ZoneInfo record.
Definition: Zone.h:31
bool isNull() const
Return true if zoneInfo is null.
Definition: Zone.h:47
uint32_t zoneId() const
Return the zoneId of the current zoneInfo.
Definition: Zone.h:71
void printShortNameTo(Print &printer) const
Print the short pretty zone name to the printer.
Definition: Zone.h:64
TimeOffset stdOffset() const
Return the STDOFF of the last ZoneEra.
Definition: Zone.h:76
void printNameTo(Print &printer) const
Print the full zone name to printer.
Definition: Zone.h:50