AceTime  0.3
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.
TimeZone.h
1 #ifndef ACE_TIME_TIME_ZONE_H
2 #define ACE_TIME_TIME_ZONE_H
3 
4 #include <stdint.h>
5 #include "TimeOffset.h"
6 #include "ZoneSpecifier.h"
7 #include "ManualZoneSpecifier.h"
8 
9 class Print;
10 
11 namespace ace_time {
12 
96 class TimeZone {
97  public:
98  static const uint8_t kTypeFixed = 0;
99  static const uint8_t kTypeManual = ZoneSpecifier::kTypeManual;
100  static const uint8_t kTypeBasic = ZoneSpecifier::kTypeBasic;
101  static const uint8_t kTypeExtended = ZoneSpecifier::kTypeExtended;
102 
109  return TimeZone(offset);
110  }
111 
118  static TimeZone forZoneSpecifier(const ZoneSpecifier* zoneSpecifier) {
119  return TimeZone(zoneSpecifier);
120  }
121 
124  mType(kTypeFixed),
125  mOffset() {}
126 
131  uint8_t getType() const { return mType; }
132 
134  TimeOffset getUtcOffset(acetime_t epochSeconds) const {
135  return (mType == kTypeFixed)
136  ? mOffset
137  : mZoneSpecifier->getUtcOffset(epochSeconds);
138  }
139 
145  TimeOffset getDeltaOffset(acetime_t epochSeconds) const {
146  return (mType == kTypeFixed)
147  ? TimeOffset()
148  : mZoneSpecifier->getDeltaOffset(epochSeconds);
149  }
150 
158  OffsetDateTime odt = (mType == kTypeFixed)
161  return odt;
162  }
163 
172  void printTo(Print& printer) const;
173 
181  void printAbbrevTo(Print& printer, acetime_t epochSeconds) const;
182 
191  bool isDst() const {
192  if (mType != kTypeManual) return false;
193  return ((ManualZoneSpecifier*) mZoneSpecifier)->isDst();
194  }
195 
202  void isDst(bool dst) {
203  if (mType != kTypeManual) return;
204  ((ManualZoneSpecifier*) mZoneSpecifier)->isDst(dst);
205  }
206 
207  // Use default copy constructor and assignment operator.
208  TimeZone(const TimeZone&) = default;
209  TimeZone& operator=(const TimeZone&) = default;
210 
211  private:
212  friend bool operator==(const TimeZone& a, const TimeZone& b);
213 
218  explicit TimeZone(TimeOffset offset):
219  mType(kTypeFixed),
220  mOffset(offset) {}
221 
234  explicit TimeZone(const ZoneSpecifier* zoneSpecifier):
235  mType(zoneSpecifier->getType()),
236  mZoneSpecifier(zoneSpecifier) {}
237 
238  uint8_t mType;
239 
240  union {
243 
246  };
247 };
248 
249 inline bool operator==(const TimeZone& a, const TimeZone& b) {
250  if (a.getType() != b.getType()) return false;
251  if (a.getType() == TimeZone::kTypeFixed) {
252  return a.mOffset == b.mOffset;
253  } else {
254  if (a.mZoneSpecifier == b.mZoneSpecifier) return true;
255  return *a.mZoneSpecifier == *b.mZoneSpecifier;
256  }
257 }
258 
259 inline bool operator!=(const TimeZone& a, const TimeZone& b) {
260  return ! (a == b);
261 }
262 
263 }
264 
265 #endif
virtual OffsetDateTime getOffsetDateTime(const LocalDateTime &ldt) const =0
Return the best estimate of the OffsetDateTime at the given LocalDateTime for the timezone of the cur...
uint8_t getType() const
Return the kTypeXxx of the current instance.
Definition: ZoneSpecifier.h:52
static OffsetDateTime forLocalDateTimeAndOffset(const LocalDateTime &localDateTime, TimeOffset timeOffset)
Factory method from LocalDateTime and TimeOffset.
const ZoneSpecifier * mZoneSpecifier
Used if mType == mTypeZoneSpecifier.
Definition: TimeZone.h:245
static const uint8_t kTypeBasic
Indicate BasicZoneSpecifier.
Definition: ZoneSpecifier.h:46
bool isDst() const
Return the isDst() value of the underlying ManualZoneSpecifier.
Definition: TimeZone.h:191
virtual TimeOffset getDeltaOffset(acetime_t epochSeconds) const =0
Return the DST delta offset at epochSeconds.
An implementation of ZoneSpecifier which allows the user to manually adjust the UTC offset and the DS...
OffsetDateTime getOffsetDateTime(const LocalDateTime &ldt) const
Return the best estimate of the OffsetDateTime at the given LocalDateTime for the current TimeZone...
Definition: TimeZone.h:157
Base interface for ZoneSpecifier classes.
Definition: ZoneSpecifier.h:40
void printTo(Print &printer) const
Print the human readable representation of the time zone.
Definition: TimeZone.cpp:7
TimeZone()
Default constructor.
Definition: TimeZone.h:123
static TimeZone forZoneSpecifier(const ZoneSpecifier *zoneSpecifier)
Factory method to create from a ZoneSpecifier.
Definition: TimeZone.h:118
static const uint8_t kTypeExtended
Indicate ExtendedZoneSpecifier.
Definition: ZoneSpecifier.h:49
static TimeZone forTimeOffset(TimeOffset offset)
Factory method to create from a fixed UTC offset.
Definition: TimeZone.h:108
The date (year, month, day) and time (hour, minute, second) fields representing the time with an offs...
static const uint8_t kTypeManual
Indicate ManualZoneSpecifier.
Definition: ZoneSpecifier.h:43
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
TimeOffset mOffset
Used if mType == mTypeFixed.
Definition: TimeZone.h:242
Class that describes a time zone.
Definition: TimeZone.h:96
uint8_t getType() const
Return the type of TimeZone.
Definition: TimeZone.h:131
void printAbbrevTo(Print &printer, acetime_t epochSeconds) const
Print the time zone abbreviation for the given epochSeconds.
Definition: TimeZone.cpp:19
virtual TimeOffset getUtcOffset(acetime_t epochSeconds) const =0
Return the total UTC offset at epochSeconds, including DST offset.
void isDst(bool dst)
Sets the isDst() flag of the underlying ManualZoneSpecifier.
Definition: TimeZone.h:202
TimeOffset getUtcOffset(acetime_t epochSeconds) const
Return the total UTC offset at epochSeconds, including DST offset.
Definition: TimeZone.h:134
TimeOffset getDeltaOffset(acetime_t epochSeconds) const
Return the DST offset from standard UTC offset at epochSeconds.
Definition: TimeZone.h:145