hueplusplus  1.0.0
TimePattern.h
Go to the documentation of this file.
1 
22 #ifndef INCLUDE_HUEPLUSPLUS_TIME_PATTERN
23 #define INCLUDE_HUEPLUSPLUS_TIME_PATTERN
24 
25 #include <chrono>
26 #include <string>
27 #include <cstddef>
28 
29 namespace hueplusplus
30 {
32 namespace time
33 {
41 std::string timepointToTimestamp(std::chrono::system_clock::time_point time);
42 
49 std::chrono::system_clock::time_point parseTimestamp(const std::string& timestamp);
50 
57 std::chrono::system_clock::time_point parseUTCTimestamp(const std::string& timestamp);
58 
63 std::string durationTo_hh_mm_ss(std::chrono::system_clock::duration duration);
64 
69 std::chrono::system_clock::duration parseDuration(const std::string& hourMinSec);
70 
73 {
74  using clock = std::chrono::system_clock;
75 
76 public:
79  explicit AbsoluteTime(clock::time_point baseTime);
80 
84  clock::time_point getBaseTime() const;
85 
89  std::string toString() const;
90 
94  static AbsoluteTime parse(const std::string& s);
95 
99  static AbsoluteTime parseUTC(const std::string& s);
100 
101 private:
102  clock::time_point base;
103 };
109 {
110  using clock = std::chrono::system_clock;
111 
112 public:
117  explicit AbsoluteVariedTime(clock::time_point baseTime, clock::duration variation = std::chrono::seconds(0));
118 
122  clock::duration getRandomVariation() const;
123 
130  std::string toString() const;
131 
135  static AbsoluteVariedTime parse(const std::string& s);
136 
137 private:
138  clock::duration variation;
139 };
140 
144 class Weekdays
145 {
146 public:
148  Weekdays() : bitmask(0) { }
151  explicit Weekdays(int num) : bitmask(1 << num) { }
152 
154  bool isNone() const;
156  bool isAll() const;
158  bool isMonday() const;
160  bool isTuesday() const;
162  bool isWednesday() const;
164  bool isThursday() const;
166  bool isFriday() const;
168  bool isSaturday() const;
170  bool isSunday() const;
171 
175  Weekdays unionWith(Weekdays other) const;
178  Weekdays operator|(Weekdays other) const { return unionWith(other); }
179 
182  std::string toString() const;
183 
185  static Weekdays none();
187  static Weekdays all();
189  static Weekdays monday();
191  static Weekdays tuesday();
193  static Weekdays wednesday();
195  static Weekdays thursday();
197  static Weekdays friday();
199  static Weekdays saturday();
201  static Weekdays sunday();
202 
206  static Weekdays parse(const std::string& s);
207 
209  bool operator==(const Weekdays& other) const { return bitmask == other.bitmask; }
211  bool operator!=(const Weekdays& other) const { return bitmask != other.bitmask; }
212 
213 private:
214  int bitmask;
215 };
216 
222 {
223  using clock = std::chrono::system_clock;
224 
225 public:
231  explicit RecurringTime(clock::duration daytime, Weekdays days, clock::duration variation = std::chrono::seconds(0));
232 
234  clock::duration getDaytime() const;
238  clock::duration getRandomVariation() const;
240  Weekdays getWeekdays() const;
241 
248  std::string toString() const;
249 
250 private:
251  clock::duration time;
252  clock::duration variation;
253  Weekdays days;
254 };
255 
261 {
262  using clock = std::chrono::system_clock;
263 
264 public:
269  TimeInterval(clock::duration start, clock::duration end, Weekdays days = Weekdays::all());
270 
272  clock::duration getStartTime() const;
274  clock::duration getEndTime() const;
276  Weekdays getWeekdays() const;
277 
284  std::string toString() const;
285 
286 private:
287  clock::duration start;
288  clock::duration end;
289  Weekdays days;
290 };
291 
296 class Timer
297 {
298  using clock = std::chrono::system_clock;
299 
300 public:
301  // \brief Used to represent infinite repetitions
302  static constexpr int infiniteExecutions = 0;
303 
307  Timer(clock::duration duration, clock::duration variation = std::chrono::seconds(0));
312  Timer(clock::duration duration, int numExecutions, clock::duration variation = std::chrono::seconds(0));
313 
315  bool isRecurring() const;
316 
319  int getNumberOfExecutions() const;
321  clock::duration getExpiryTime() const;
325  clock::duration getRandomVariation() const;
326 
339  std::string toString() const;
340 
341 private:
342  clock::duration expires;
343  clock::duration variation;
344  int numExecutions;
345 };
346 
352 {
353 public:
355  enum class Type
356  {
357  undefined,
358  absolute,
359  recurring,
360  interval,
361  timer
362  };
363 
365  TimePattern();
367  ~TimePattern();
369  explicit TimePattern(const AbsoluteVariedTime& absolute);
371  explicit TimePattern(const RecurringTime& recurring);
373  explicit TimePattern(const TimeInterval& interval);
375  explicit TimePattern(const Timer& timer);
376 
378  TimePattern(const TimePattern& other);
379 
381  TimePattern& operator=(const TimePattern& other);
382 
386  Type getType() const;
387 
390  AbsoluteVariedTime asAbsolute() const;
391 
394  RecurringTime asRecurring() const;
395 
398  TimeInterval asInterval() const;
399 
402  Timer asTimer() const;
403 
407  std::string toString() const;
408 
416  static TimePattern parse(const std::string& s);
417 
418 private:
420  void destroy();
421 
422 private:
423  Type type;
424  union
425  {
426  std::nullptr_t undefined;
431  };
432 };
433 } // namespace time
434 } // namespace hueplusplus
435 
436 #endif
clock::time_point getBaseTime() const
Get base time point.
Definition: TimePattern.cpp:155
Timer timer
Definition: TimePattern.h:430
std::string toString() const
Get formatted string as expected by Hue API.
Definition: TimePattern.cpp:159
std::nullptr_t undefined
Definition: TimePattern.h:426
Time interval repeated daily to weekly.
Definition: TimePattern.h:260
AbsoluteTime(clock::time_point baseTime)
Create absolute time point.
Definition: TimePattern.cpp:153
Namespace for the hueplusplus library.
Definition: Action.h:27
Weekdays(int num)
Create with the day num.
Definition: TimePattern.h:151
Any number of days of the week.
Definition: TimePattern.h:144
Holds different time representations.
Definition: TimePattern.h:351
static Weekdays all()
Creates set of all days.
Definition: TimePattern.cpp:278
std::chrono::system_clock::time_point parseUTCTimestamp(const std::string &timestamp)
Converts an UTC timestamp to a time_point.
Definition: TimePattern.cpp:90
static AbsoluteTime parseUTC(const std::string &s)
Parse AbsoluteTime from formatted UTC string.
Definition: TimePattern.cpp:171
std::string timepointToTimestamp(std::chrono::system_clock::time_point time)
Converts a time_point to a timestamp string.
Definition: TimePattern.cpp:58
Type
Currently active type.
Definition: TimePattern.h:355
std::string durationTo_hh_mm_ss(std::chrono::system_clock::duration duration)
Converts duration to a time string.
Definition: TimePattern.cpp:126
std::chrono::system_clock::time_point parseTimestamp(const std::string &timestamp)
Converts a timestamp to a time_point.
Definition: TimePattern.cpp:79
Time repeated weekly to daily, with possible random variation.
Definition: TimePattern.h:221
static AbsoluteTime parse(const std::string &s)
Parse AbsoluteTime from formatted string in local timezone.
Definition: TimePattern.cpp:164
One-time, absolute time point.
Definition: TimePattern.h:72
AbsoluteVariedTime absolute
Definition: TimePattern.h:427
Definition: TimePattern.h:108
TimeInterval interval
Definition: TimePattern.h:429
Weekdays operator|(Weekdays other) const
Create set union with other Weekdays.
Definition: TimePattern.h:178
std::chrono::system_clock::duration parseDuration(const std::string &hourMinSec)
Converts time string to a duration.
Definition: TimePattern.cpp:144
Timer that is started and triggers after specified delay.
Definition: TimePattern.h:296
RecurringTime recurring
Definition: TimePattern.h:428
bool operator==(const Weekdays &other) const
Check whether all days are equal.
Definition: TimePattern.h:209
Weekdays()
Create with no days.
Definition: TimePattern.h:148
bool operator!=(const Weekdays &other) const
Check whether not all days are equal.
Definition: TimePattern.h:211