AceTime  0.1
Date and time classes for Arduino that supports the TZ DAtabase, and a system clock synchronized from an NTP server or an RTC chip.
ManualZoneSpecifier.h
1 #ifndef ACE_TIME_MANUAL_ZONE_SPECIFIER_H
2 #define ACE_TIME_MANUAL_ZONE_SPECIFIER_H
3 
4 #include <string.h> // strcmp()
5 #include "TimeOffset.h"
6 #include "ZoneSpecifier.h"
7 
8 namespace ace_time {
9 
17  public:
43  bool isDst = false,
44  const char* stdAbbrev = "",
45  const char* dstAbbrev = "",
48  mStdOffset(stdOffset),
49  mIsDst(isDst),
50  mStdAbbrev(stdAbbrev),
51  mDstAbbrev(dstAbbrev),
52  mDeltaOffset(deltaOffset) {}
53 
55  ManualZoneSpecifier(const ManualZoneSpecifier&) = default;
56 
59 
61  TimeOffset stdOffset() const { return mStdOffset; }
62 
64  bool isDst() const { return mIsDst; }
65 
67  const char* stdAbbrev() const { return mStdAbbrev; }
68 
70  const char* dstAbbrev() const { return mDstAbbrev; }
71 
73  TimeOffset deltaOffset() const { return mDeltaOffset; }
74 
79  void stdOffset(TimeOffset offset) { mStdOffset = offset; }
80 
85  void isDst(bool isDst) { mIsDst = isDst; }
86 
87  TimeOffset getUtcOffset(acetime_t /*epochSeconds*/) const override {
88  return mIsDst
90  // Note: Use toOffsetCode() because TimeOffset is currently
91  // implemented using OffsetCodes. If that changes to minutes,
92  // then this should use toMinutes().
93  mStdOffset.toOffsetCode() + mDeltaOffset.toOffsetCode())
94  : mStdOffset;
95  }
96 
97  TimeOffset getDeltaOffset(acetime_t /*epochSeconds*/) const override {
98  return mIsDst ? mDeltaOffset : TimeOffset();
99  }
100 
101  const char* getAbbrev(acetime_t /*epochSeconds*/) const override {
102  return mIsDst ? mDstAbbrev : mStdAbbrev;
103  }
104 
106  return getUtcOffset(0);
107  }
108 
109  void printTo(Print& printer) const override;
110 
111  private:
112  bool equals(const ZoneSpecifier& other) const override {
113  const auto& that = (const ManualZoneSpecifier&) other;
114  // These parameters are ordered in decreasing expected probability of
115  // being different from the other.
116  return isDst() == that.isDst()
117  && stdOffset() == that.stdOffset()
118  && deltaOffset() == that.deltaOffset()
119  && strcmp(stdAbbrev(), that.stdAbbrev()) == 0
120  && strcmp(dstAbbrev(), that.dstAbbrev()) == 0;
121  }
122 
124  TimeOffset mStdOffset;
125 
127  bool mIsDst;
128 
130  const char* mStdAbbrev;
131 
133  const char* mDstAbbrev;
134 
136  TimeOffset mDeltaOffset;
137 };
138 
139 }
140 
141 #endif
TimeOffset getDeltaOffset(acetime_t) const override
Return the DST delta offset at epochSeconds.
ManualZoneSpecifier & operator=(const ManualZoneSpecifier &)=default
Default assignment operator.
int8_t toOffsetCode() const
Return the time offset as the number of 15 minute increments.
Definition: TimeOffset.h:112
void isDst(bool isDst)
Set the current isDst flag.
static TimeOffset forHour(int8_t hour)
Create TimeOffset with the corresponding hour offset.
Definition: TimeOffset.h:59
void printTo(Print &printer) const override
Print a human-readable identifier.
An implementation of ZoneSpecifier which allows the user to manually adjust the UTC offset and the DS...
Base interface for ZoneSpecifier classes.
Definition: ZoneSpecifier.h:39
const char * stdAbbrev() const
Get the standard abbreviation.
static TimeOffset forOffsetCode(int8_t offsetCode)
Create TimeOffset from the offset code.
Definition: TimeOffset.h:97
static const uint8_t kTypeManual
Indicate ManualZoneSpecifier.
Definition: ZoneSpecifier.h:42
void stdOffset(TimeOffset offset)
Set the standard UTC offset.
const char * dstAbbrev() const
Get the DST abbreviation.
A thin wrapper that represents a time offset from a reference point, usually 00:00 at UTC...
Definition: TimeOffset.h:53
ManualZoneSpecifier(TimeOffset stdOffset=TimeOffset(), bool isDst=false, const char *stdAbbrev="", const char *dstAbbrev="", TimeOffset deltaOffset=TimeOffset::forHour(1))
Constructor for a time zone with an offset from UTC that does not change with epochSeconds.
TimeOffset deltaOffset() const
Get the DST delta offset.
TimeOffset getUtcOffset(acetime_t) const override
Return the total UTC offset at epochSeconds, including DST offset.
TimeOffset stdOffset() const
Get the standard UTC offset.
const char * getAbbrev(acetime_t) const override
Return the time zone abbreviation at epochSeconds.
bool isDst() const
Get the current isDst flag.
TimeOffset getUtcOffsetForDateTime(const LocalDateTime &) const override
Return the UTC offset matching the given the date/time components.