Loading...
Searching...
No Matches
TimePattern.h
Go to the documentation of this file.
#ifndef INCLUDE_HUEPLUSPLUS_TIME_PATTERN
#define INCLUDE_HUEPLUSPLUS_TIME_PATTERN
#include <chrono>
#include <string>
#include <cstddef>
namespace hueplusplus
{
namespace time
{
std::string timepointToTimestamp(std::chrono::system_clock::time_point time);
std::chrono::system_clock::time_point parseTimestamp(const std::string& timestamp);
std::chrono::system_clock::time_point parseUTCTimestamp(const std::string& timestamp);
std::string durationTo_hh_mm_ss(std::chrono::system_clock::duration duration);
std::chrono::system_clock::duration parseDuration(const std::string& hourMinSec);
class AbsoluteTime
{
using clock = std::chrono::system_clock;
public:
explicit AbsoluteTime(clock::time_point baseTime);
clock::time_point getBaseTime() const;
std::string toString() const;
static AbsoluteTime parse(const std::string& s);
static AbsoluteTime parseUTC(const std::string& s);
private:
clock::time_point base;
};
class AbsoluteVariedTime : public AbsoluteTime
{
using clock = std::chrono::system_clock;
public:
explicit AbsoluteVariedTime(clock::time_point baseTime, clock::duration variation = std::chrono::seconds(0));
clock::duration getRandomVariation() const;
std::string toString() const;
static AbsoluteVariedTime parse(const std::string& s);
private:
clock::duration variation;
};
class Weekdays
{
public:
Weekdays() : bitmask(0) { }
explicit Weekdays(int num) : bitmask(1 << num) { }
bool isNone() const;
bool isAll() const;
bool isMonday() const;
bool isTuesday() const;
bool isWednesday() const;
bool isThursday() const;
bool isFriday() const;
bool isSaturday() const;
bool isSunday() const;
Weekdays unionWith(Weekdays other) const;
Weekdays operator|(Weekdays other) const { return unionWith(other); }
std::string toString() const;
static Weekdays none();
static Weekdays all();
static Weekdays monday();
static Weekdays tuesday();
static Weekdays wednesday();
static Weekdays thursday();
static Weekdays friday();
static Weekdays saturday();
static Weekdays sunday();
static Weekdays parse(const std::string& s);
bool operator==(const Weekdays& other) const { return bitmask == other.bitmask; }
bool operator!=(const Weekdays& other) const { return bitmask != other.bitmask; }
private:
int bitmask;
};
class RecurringTime
{
using clock = std::chrono::system_clock;
public:
explicit RecurringTime(clock::duration daytime, Weekdays days, clock::duration variation = std::chrono::seconds(0));
clock::duration getDaytime() const;
clock::duration getRandomVariation() const;
Weekdays getWeekdays() const;
std::string toString() const;
private:
clock::duration time;
clock::duration variation;
Weekdays days;
};
class TimeInterval
{
using clock = std::chrono::system_clock;
public:
TimeInterval(clock::duration start, clock::duration end, Weekdays days = Weekdays::all());
clock::duration getStartTime() const;
clock::duration getEndTime() const;
Weekdays getWeekdays() const;
std::string toString() const;
private:
clock::duration start;
clock::duration end;
Weekdays days;
};
class Timer
{
using clock = std::chrono::system_clock;
public:
// \brief Used to represent infinite repetitions
static constexpr int infiniteExecutions = 0;
Timer(clock::duration duration, clock::duration variation = std::chrono::seconds(0));
Timer(clock::duration duration, int numExecutions, clock::duration variation = std::chrono::seconds(0));
bool isRecurring() const;
int getNumberOfExecutions() const;
clock::duration getExpiryTime() const;
clock::duration getRandomVariation() const;
std::string toString() const;
private:
clock::duration expires;
clock::duration variation;
int numExecutions;
};
class TimePattern
{
public:
enum class Type
{
undefined,
absolute,
recurring,
interval,
timer
};
TimePattern();
~TimePattern();
explicit TimePattern(const AbsoluteVariedTime& absolute);
explicit TimePattern(const RecurringTime& recurring);
explicit TimePattern(const TimeInterval& interval);
explicit TimePattern(const Timer& timer);
TimePattern(const TimePattern& other);
TimePattern& operator=(const TimePattern& other);
Type getType() const;
AbsoluteVariedTime asAbsolute() const;
RecurringTime asRecurring() const;
TimeInterval asInterval() const;
Timer asTimer() const;
std::string toString() const;
static TimePattern parse(const std::string& s);
private:
void destroy();
private:
Type type;
union
{
std::nullptr_t undefined;
AbsoluteVariedTime absolute;
RecurringTime recurring;
TimeInterval interval;
Timer timer;
};
};
} // namespace time
} // namespace hueplusplus
#endif
hueplusplus::time::AbsoluteTime::AbsoluteTime
AbsoluteTime(clock::time_point baseTime)
Create absolute time point.
Definition TimePattern.cpp:153
hueplusplus::time::AbsoluteTime::getBaseTime
clock::time_point getBaseTime() const
Get base time point.
Definition TimePattern.cpp:155
hueplusplus::time::AbsoluteTime::parseUTC
static AbsoluteTime parseUTC(const std::string &s)
Parse AbsoluteTime from formatted UTC string.
Definition TimePattern.cpp:171
hueplusplus::time::AbsoluteTime::toString
std::string toString() const
Get formatted string as expected by Hue API.
Definition TimePattern.cpp:159
hueplusplus::time::AbsoluteTime::parse
static AbsoluteTime parse(const std::string &s)
Parse AbsoluteTime from formatted string in local timezone.
Definition TimePattern.cpp:164
hueplusplus::time::AbsoluteVariedTime
Definition TimePattern.h:109
hueplusplus::time::AbsoluteVariedTime::getRandomVariation
clock::duration getRandomVariation() const
Get random variation or zero.
Definition TimePattern.cpp:182
hueplusplus::time::AbsoluteVariedTime::toString
std::string toString() const
Get formatted string as expected by Hue API.
Definition TimePattern.cpp:200
hueplusplus::time::AbsoluteVariedTime::parse
static AbsoluteVariedTime parse(const std::string &s)
Parse AbsoluteTime from formatted string in local timezone.
Definition TimePattern.cpp:187
hueplusplus::time::AbsoluteVariedTime::AbsoluteVariedTime
AbsoluteVariedTime(clock::time_point baseTime, clock::duration variation=std::chrono::seconds(0))
Create absolute time point.
Definition TimePattern.cpp:178
hueplusplus::time::RecurringTime
Time repeated weekly to daily, with possible random variation.
Definition TimePattern.h:222
hueplusplus::time::RecurringTime::RecurringTime
RecurringTime(clock::duration daytime, Weekdays days, clock::duration variation=std::chrono::seconds(0))
Create recurring time.
Definition TimePattern.cpp:327
hueplusplus::time::RecurringTime::getRandomVariation
clock::duration getRandomVariation() const
Get random variation.
Definition TimePattern.cpp:336
hueplusplus::time::RecurringTime::toString
std::string toString() const
Get formatted string as expected by Hue API.
Definition TimePattern.cpp:346
hueplusplus::time::RecurringTime::getDaytime
clock::duration getDaytime() const
Get time of day.
Definition TimePattern.cpp:331
hueplusplus::time::RecurringTime::getWeekdays
Weekdays getWeekdays() const
Get days on which the repetition will happen.
Definition TimePattern.cpp:341
hueplusplus::time::TimeInterval
Time interval repeated daily to weekly.
Definition TimePattern.h:261
hueplusplus::time::TimeInterval::getEndTime
clock::duration getEndTime() const
Get end time of the interval.
Definition TimePattern.cpp:369
hueplusplus::time::TimeInterval::TimeInterval
TimeInterval(clock::duration start, clock::duration end, Weekdays days=Weekdays::all())
Create time interval.
Definition TimePattern.cpp:360
hueplusplus::time::TimeInterval::toString
std::string toString() const
Get formatted string as expected by Hue API.
Definition TimePattern.cpp:379
hueplusplus::time::TimeInterval::getWeekdays
Weekdays getWeekdays() const
Get active days.
Definition TimePattern.cpp:374
hueplusplus::time::TimeInterval::getStartTime
clock::duration getStartTime() const
Get start time of the interval.
Definition TimePattern.cpp:364
hueplusplus::time::TimePattern
Holds different time representations.
Definition TimePattern.h:352
hueplusplus::time::TimePattern::parse
static TimePattern parse(const std::string &s)
Parses TimePattern from formatted string as returned by Hue API.
Definition TimePattern.cpp:555
hueplusplus::time::TimePattern::absolute
AbsoluteVariedTime absolute
Definition TimePattern.h:427
hueplusplus::time::TimePattern::interval
TimeInterval interval
Definition TimePattern.h:429
hueplusplus::time::TimePattern::recurring
RecurringTime recurring
Definition TimePattern.h:428
hueplusplus::time::TimePattern::asRecurring
RecurringTime asRecurring() const
Get contained recurring time.
Definition TimePattern.cpp:521
hueplusplus::time::TimePattern::timer
Timer timer
Definition TimePattern.h:430
hueplusplus::time::TimePattern::undefined
std::nullptr_t undefined
Definition TimePattern.h:426
hueplusplus::time::TimePattern::operator=
TimePattern & operator=(const TimePattern &other)
Copy assignment for union.
Definition TimePattern.cpp:472
hueplusplus::time::TimePattern::asInterval
TimeInterval asInterval() const
Get contained time interval.
Definition TimePattern.cpp:526
hueplusplus::time::TimePattern::asAbsolute
AbsoluteVariedTime asAbsolute() const
Get contained absolute time.
Definition TimePattern.cpp:516
hueplusplus::time::TimePattern::Type
Type
Currently active type.
Definition TimePattern.h:356
hueplusplus::time::TimePattern::TimePattern
TimePattern()
Create empty TimePattern.
Definition TimePattern.cpp:452
hueplusplus::time::TimePattern::~TimePattern
~TimePattern()
Destructor for union.
Definition TimePattern.cpp:454
hueplusplus::time::TimePattern::getType
Type getType() const
Get currently active type.
Definition TimePattern.cpp:511
hueplusplus::time::TimePattern::toString
std::string toString() const
Get formatted string of the contained value as expected by Hue API.
Definition TimePattern.cpp:536
hueplusplus::time::TimePattern::asTimer
Timer asTimer() const
Get contained timer.
Definition TimePattern.cpp:531
hueplusplus::time::Timer
Timer that is started and triggers after specified delay.
Definition TimePattern.h:297
hueplusplus::time::Timer::getExpiryTime
clock::duration getExpiryTime() const
Get expiry time.
Definition TimePattern.cpp:414
hueplusplus::time::Timer::getNumberOfExecutions
int getNumberOfExecutions() const
Get number of executions.
Definition TimePattern.cpp:409
hueplusplus::time::Timer::Timer
Timer(clock::duration duration, clock::duration variation=std::chrono::seconds(0))
Create one-off timer.
Definition TimePattern.cpp:396
hueplusplus::time::Timer::infiniteExecutions
static constexpr int infiniteExecutions
Definition TimePattern.h:302
hueplusplus::time::Timer::isRecurring
bool isRecurring() const
Returns true when the timer is executed more than once.
Definition TimePattern.cpp:404
hueplusplus::time::Timer::getRandomVariation
clock::duration getRandomVariation() const
Get random variation of expiry time.
Definition TimePattern.cpp:419
hueplusplus::time::Timer::toString
std::string toString() const
Get formatted string as expected by Hue API.
Definition TimePattern.cpp:424
hueplusplus::time::Weekdays
Any number of days of the week.
Definition TimePattern.h:145
hueplusplus::time::Weekdays::operator|
Weekdays operator|(Weekdays other) const
Create set union with other Weekdays.
Definition TimePattern.h:178
hueplusplus::time::Weekdays::sunday
static Weekdays sunday()
Creates Sunday.
Definition TimePattern.cpp:315
hueplusplus::time::Weekdays::Weekdays
Weekdays()
Create with no days.
Definition TimePattern.h:148
hueplusplus::time::Weekdays::saturday
static Weekdays saturday()
Creates Saturday.
Definition TimePattern.cpp:310
hueplusplus::time::Weekdays::isAll
bool isAll() const
Check if all days are set.
Definition TimePattern.cpp:216
hueplusplus::time::Weekdays::operator==
bool operator==(const Weekdays &other) const
Check whether all days are equal.
Definition TimePattern.h:209
hueplusplus::time::Weekdays::isTuesday
bool isTuesday() const
Check if Tuesday is contained.
Definition TimePattern.cpp:227
hueplusplus::time::Weekdays::all
static Weekdays all()
Creates set of all days.
Definition TimePattern.cpp:278
hueplusplus::time::Weekdays::isNone
bool isNone() const
Check if no days are set.
Definition TimePattern.cpp:211
hueplusplus::time::Weekdays::isWednesday
bool isWednesday() const
Check if Wednesday is contained.
Definition TimePattern.cpp:232
hueplusplus::time::Weekdays::isSaturday
bool isSaturday() const
Check if Saturday is contained.
Definition TimePattern.cpp:247
hueplusplus::time::Weekdays::operator!=
bool operator!=(const Weekdays &other) const
Check whether not all days are equal.
Definition TimePattern.h:211
hueplusplus::time::Weekdays::isThursday
bool isThursday() const
Check if Thursday is contained.
Definition TimePattern.cpp:237
hueplusplus::time::Weekdays::toString
std::string toString() const
Create a formatted, numeric string.
Definition TimePattern.cpp:257
hueplusplus::time::Weekdays::monday
static Weekdays monday()
Creates Monday.
Definition TimePattern.cpp:285
hueplusplus::time::Weekdays::friday
static Weekdays friday()
Creates Friday.
Definition TimePattern.cpp:305
hueplusplus::time::Weekdays::isFriday
bool isFriday() const
Check if Friday is contained.
Definition TimePattern.cpp:242
hueplusplus::time::Weekdays::Weekdays
Weekdays(int num)
Create with the day num.
Definition TimePattern.h:151
hueplusplus::time::Weekdays::isSunday
bool isSunday() const
Check if Sunday is contained.
Definition TimePattern.cpp:252
hueplusplus::time::Weekdays::isMonday
bool isMonday() const
Check if Monday is contained.
Definition TimePattern.cpp:222
hueplusplus::time::Weekdays::thursday
static Weekdays thursday()
Creates Thursday.
Definition TimePattern.cpp:300
hueplusplus::time::Weekdays::unionWith
Weekdays unionWith(Weekdays other) const
Create set union with other Weekdays.
Definition TimePattern.cpp:267
hueplusplus::time::Weekdays::tuesday
static Weekdays tuesday()
Creates Tuesday.
Definition TimePattern.cpp:290
hueplusplus::time::Weekdays::none
static Weekdays none()
Creates an empty Weekdays.
Definition TimePattern.cpp:273
hueplusplus::time::Weekdays::wednesday
static Weekdays wednesday()
Creates Wednesday.
Definition TimePattern.cpp:295
hueplusplus::time::Weekdays::parse
static Weekdays parse(const std::string &s)
Parse from three digit code.
Definition TimePattern.cpp:320
hueplusplus::time
Namespace for time/date related classes and functions.
Definition TimePattern.h:33
hueplusplus::time::parseUTCTimestamp
std::chrono::system_clock::time_point parseUTCTimestamp(const std::string &timestamp)
Converts an UTC timestamp to a time_point.
Definition TimePattern.cpp:90
hueplusplus::time::parseTimestamp
std::chrono::system_clock::time_point parseTimestamp(const std::string &timestamp)
Converts a timestamp to a time_point.
Definition TimePattern.cpp:79
hueplusplus::time::parseDuration
std::chrono::system_clock::duration parseDuration(const std::string &hourMinSec)
Converts time string to a duration.
Definition TimePattern.cpp:144
hueplusplus::time::durationTo_hh_mm_ss
std::string durationTo_hh_mm_ss(std::chrono::system_clock::duration duration)
Converts duration to a time string.
Definition TimePattern.cpp:126
hueplusplus::time::timepointToTimestamp
std::string timepointToTimestamp(std::chrono::system_clock::time_point time)
Converts a time_point to a timestamp string.
Definition TimePattern.cpp:58
hueplusplus
Namespace for the hueplusplus library.
Definition Action.h:28