2020-03-22 00:45:04 +01:00
|
|
|
#pragma once
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
#include "Library/Chrono.hpp"
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
namespace SqMod {
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Helper class used to represent a certain time.
|
|
|
|
*/
|
|
|
|
class Time
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2016-03-26 17:18:41 +01:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
static SQChar Delimiter;
|
|
|
|
|
2016-06-04 18:17:42 +02:00
|
|
|
protected:
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Compare the values of two instances.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD int32_t Compare(const Time & o) const;
|
2016-06-04 18:17:42 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2021-08-18 20:37:33 +02:00
|
|
|
uint8_t m_Hour{0}; // Hour
|
|
|
|
uint8_t m_Minute{0}; // Minute
|
|
|
|
uint8_t m_Second{0}; // Second
|
|
|
|
uint16_t m_Millisecond{0}; // Millisecond
|
2016-06-04 18:17:42 +02:00
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
SQChar m_Delimiter; // Component delimiter when generating strings.
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
2016-03-25 13:28:07 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Default constructor.
|
|
|
|
*/
|
|
|
|
Time()
|
|
|
|
: m_Hour(0)
|
|
|
|
, m_Minute(0)
|
|
|
|
, m_Second(0)
|
|
|
|
, m_Millisecond(0)
|
2016-03-26 17:18:41 +01:00
|
|
|
, m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
/* ... */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
explicit Time(uint8_t hour)
|
2016-03-26 17:18:41 +01:00
|
|
|
: m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time(uint8_t hour, uint8_t minute)
|
2016-03-26 17:18:41 +01:00
|
|
|
: m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, minute, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time(uint8_t hour, uint8_t minute, uint8_t second)
|
2016-03-26 17:18:41 +01:00
|
|
|
: m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, minute, second, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Base constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time(uint8_t hour, uint8_t minute, uint8_t second, uint16_t millisecond)
|
2016-03-26 17:18:41 +01:00
|
|
|
: m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, minute, second, millisecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* String constructor.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
explicit Time(const SQChar * str)
|
2016-03-26 17:18:41 +01:00
|
|
|
: m_Delimiter(Delimiter)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
SetStr(str);
|
|
|
|
}
|
|
|
|
|
2021-08-18 20:37:33 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Time-stamp constructor.
|
|
|
|
*/
|
|
|
|
explicit Time(int64_t ts);
|
|
|
|
|
2016-03-25 13:28:07 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Copy constructor.
|
|
|
|
*/
|
|
|
|
Time(const Time & o) = default;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Move constructor.
|
|
|
|
*/
|
|
|
|
Time(Time && o) = default;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Destructor.
|
|
|
|
*/
|
|
|
|
~Time() = default;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Copy assignment operator.
|
|
|
|
*/
|
|
|
|
Time & operator = (const Time & o) = default;
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Move assignment operator.
|
|
|
|
*/
|
|
|
|
Time & operator = (Time && o) = default;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Equality comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator == (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Inequality comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator != (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Less than comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator < (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) < 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Greater than comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator > (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Less than or equal comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator <= (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Greater than or equal comparison operator.
|
|
|
|
*/
|
|
|
|
bool operator >= (const Time & o) const
|
|
|
|
{
|
|
|
|
return Compare(o) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Addition operator.
|
|
|
|
*/
|
|
|
|
Time operator + (const Time & o) const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Subtraction operator.
|
|
|
|
*/
|
|
|
|
Time operator - (const Time & o) const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Multiplication operator.
|
|
|
|
*/
|
|
|
|
Time operator * (const Time & o) const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Division operator.
|
|
|
|
*/
|
|
|
|
Time operator / (const Time & o) const;
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to compare two instances of this type.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD int32_t Cmp(const Time & o) const
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
return Compare(o);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
* Used by the script engine to convert an instance of this type to a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String ToString() const;
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Assign the specified values.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Set(uint8_t hour)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, m_Minute, m_Second, m_Millisecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Assign the specified values.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Set(uint8_t hour, uint8_t minute)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, minute, m_Second, m_Millisecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Assign the specified values.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Set(uint8_t hour, uint8_t minute, uint8_t second)
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
Set(hour, minute, second, m_Millisecond);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Assign the specified values.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void Set(uint8_t hour, uint8_t minute, uint8_t second, uint16_t millisecond);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
2016-06-04 18:17:42 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the local delimiter character.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD SQChar GetDelimiter() const
|
2016-06-04 18:17:42 +02:00
|
|
|
{
|
|
|
|
return m_Delimiter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Modify the local delimiter character.
|
|
|
|
*/
|
|
|
|
void SetDelimiter(SQChar c)
|
|
|
|
{
|
|
|
|
m_Delimiter = c;
|
|
|
|
}
|
|
|
|
|
2016-03-25 13:28:07 +01:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the values as a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD String GetStr() const
|
2016-06-04 18:17:42 +02:00
|
|
|
{
|
|
|
|
return ToString();
|
|
|
|
}
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Extract the values from a string.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void SetStr(const SQChar * str);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the hour component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD uint8_t GetHour() const
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
return m_Hour;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Modify the hour component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void SetHour(uint8_t hour);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the minute component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD uint8_t GetMinute() const
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
return m_Minute;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Modify the minute component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void SetMinute(uint8_t minute);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the second component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD uint8_t GetSecond() const
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
return m_Second;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Modify the second component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void SetSecond(uint8_t second);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Retrieve the millisecond component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD uint16_t GetMillisecond() const
|
2016-03-25 13:28:07 +01:00
|
|
|
{
|
|
|
|
return m_Millisecond;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Modify the millisecond component.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
void SetMillisecond(uint16_t millisecond);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
* Add the specified amount of hours to the current time.
|
2016-03-25 13:28:07 +01:00
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time & AddHours(int32_t hours);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
* Add the specified amount of minutes to the current time.
|
2016-03-25 13:28:07 +01:00
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time & AddMinutes(int32_t minutes);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
* Add the specified amount of seconds to the current time.
|
2016-03-25 13:28:07 +01:00
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time & AddSeconds(int32_t seconds);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
* Add the specified amount of milliseconds to the current time.
|
2016-03-25 13:28:07 +01:00
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
Time & AddMilliseconds(int32_t milliseconds);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
2016-06-04 18:17:42 +02:00
|
|
|
* Add the specified amount of hours to obtain a new time.
|
2016-03-25 13:28:07 +01:00
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD Time AndHours(int32_t hours);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
2016-06-04 18:17:42 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Add the specified amount of minutes to obtain a new time.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD Time AndMinutes(int32_t minutes);
|
2016-03-25 13:28:07 +01:00
|
|
|
|
2016-06-04 18:17:42 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Add the specified amount of seconds to obtain a new time.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD Time AndSeconds(int32_t seconds);
|
2016-03-26 17:18:41 +01:00
|
|
|
|
2016-06-04 18:17:42 +02:00
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Add the specified amount of milliseconds to obtain a new time.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD Time AndMilliseconds(int32_t milliseconds);
|
2016-06-04 21:33:34 +02:00
|
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------
|
|
|
|
* Convert this time instance to a time-stamp.
|
|
|
|
*/
|
2021-01-30 07:51:39 +01:00
|
|
|
SQMOD_NODISCARD Timestamp GetTimestamp() const;
|
2021-08-18 20:37:33 +02:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
std::time_t ToTimeT() const;
|
2016-03-25 13:28:07 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
} // Namespace:: SqMod
|