1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 16:57:16 +01:00
SqMod/module/Library/Chrono/Date.hpp

365 lines
12 KiB
C++
Raw Normal View History

#pragma once
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class used to represent a certain date.
*/
class Date
{
public:
// ------------------------------------------------------------------------------------------------
static SQChar Delimiter;
private:
// ------------------------------------------------------------------------------------------------
uint16_t m_Year{}; // Year
uint8_t m_Month{}; // Month
uint8_t m_Day{}; // Day
// ------------------------------------------------------------------------------------------------
SQChar m_Delimiter; // Component delimiter when generating strings.
protected:
/* ------------------------------------------------------------------------------------------------
* Compare the values of two instances.
*/
SQMOD_NODISCARD int32_t Compare(const Date & o) const;
public:
/* ------------------------------------------------------------------------------------------------
* Default constructor.
*/
Date()
: m_Year(1970)
, m_Month(1)
, m_Day(1)
, m_Delimiter(Delimiter)
{
/* ... */
}
/* ------------------------------------------------------------------------------------------------
* Specific year constructor.
*/
explicit Date(uint16_t year)
: m_Delimiter(Delimiter)
{
Set(year, 1, 1);
}
/* ------------------------------------------------------------------------------------------------
* Specific year and month constructor.
*/
Date(uint16_t year, uint8_t month)
: m_Delimiter(Delimiter)
{
Set(year, month, 1);
}
/* ------------------------------------------------------------------------------------------------
* Specific date constructor.
*/
Date(uint16_t year, uint8_t month, uint8_t day)
: m_Delimiter(Delimiter)
{
Set(year, month, day);
}
/* ------------------------------------------------------------------------------------------------
* String constructor.
*/
explicit Date(const SQChar * str)
: m_Delimiter(Delimiter)
{
SetStr(str);
}
2021-08-18 20:37:33 +02:00
/* ------------------------------------------------------------------------------------------------
* Time-stamp constructor.
*/
explicit Date(int64_t ts);
/* ------------------------------------------------------------------------------------------------
* Copy constructor.
*/
Date(const Date & o) = default;
/* ------------------------------------------------------------------------------------------------
* Move constructor.
*/
Date(Date && o) = default;
/* ------------------------------------------------------------------------------------------------
* Destructor.
*/
~Date() = default;
/* ------------------------------------------------------------------------------------------------
* Copy assignment operator.
*/
Date & operator = (const Date & o) = default;
/* ------------------------------------------------------------------------------------------------
* Move assignment operator.
*/
Date & operator = (Date && o) = default;
/* --------------------------------------------------------------------------------------------
* Equality comparison operator.
*/
bool operator == (const Date & o) const
{
return Compare(o) == 0;
}
/* --------------------------------------------------------------------------------------------
* Inequality comparison operator.
*/
bool operator != (const Date & o) const
{
return Compare(o) != 0;
}
/* --------------------------------------------------------------------------------------------
* Less than comparison operator.
*/
bool operator < (const Date & o) const
{
return Compare(o) < 0;
}
/* --------------------------------------------------------------------------------------------
* Greater than comparison operator.
*/
bool operator > (const Date & o) const
{
return Compare(o) > 0;
}
/* --------------------------------------------------------------------------------------------
* Less than or equal comparison operator.
*/
bool operator <= (const Date & o) const
{
return Compare(o) <= 0;
}
/* --------------------------------------------------------------------------------------------
* Greater than or equal comparison operator.
*/
bool operator >= (const Date & o) const
{
return Compare(o) >= 0;
}
/* --------------------------------------------------------------------------------------------
* Addition operator.
*/
Date operator + (const Date & o) const;
/* --------------------------------------------------------------------------------------------
* Subtraction operator.
*/
Date operator - (const Date & o) const;
/* --------------------------------------------------------------------------------------------
* Multiplication operator.
*/
Date operator * (const Date & o) const;
/* --------------------------------------------------------------------------------------------
* Division operator.
*/
Date operator / (const Date & o) const;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to compare two instances of this type.
*/
SQMOD_NODISCARD int32_t Cmp(const Date & o) const
{
return Compare(o);
}
/* --------------------------------------------------------------------------------------------
* Used by the script engine to convert an instance of this type to a string.
*/
SQMOD_NODISCARD String ToString() const;
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Assign the specified values.
*/
void Set(uint16_t year)
{
2016-06-04 10:55:06 +02:00
Set(year, m_Month, m_Day);
}
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Assign the specified values.
*/
void Set(uint16_t year, uint8_t month)
{
2016-06-04 10:55:06 +02:00
Set(year, month, m_Day);
}
/* ------------------------------------------------------------------------------------------------
* Assign the specified values.
*/
void Set(uint16_t year, uint8_t month, uint8_t day);
2016-06-04 10:55:06 +02:00
/* ------------------------------------------------------------------------------------------------
* Retrieve the local delimiter character.
*/
SQMOD_NODISCARD SQChar GetDelimiter() const
{
2016-06-04 10:55:06 +02:00
return m_Delimiter;
}
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Modify the local delimiter character.
*/
2016-06-04 10:55:06 +02:00
void SetDelimiter(SQChar c)
{
2016-06-04 10:55:06 +02:00
m_Delimiter = c;
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the values as a string.
*/
SQMOD_NODISCARD String GetStr() const
2016-06-04 10:55:06 +02:00
{
return ToString();
2016-06-04 10:55:06 +02:00
}
/* ------------------------------------------------------------------------------------------------
* Extract the values from a string.
2016-06-04 10:55:06 +02:00
*/
void SetStr(const SQChar * str);
/* ------------------------------------------------------------------------------------------------
* Retrieve the day component.
*/
SQMOD_NODISCARD uint16_t GetDayOfYear() const
{
return Chrono::DayOfYear(m_Year, m_Month, m_Day);
}
/* ------------------------------------------------------------------------------------------------
* Modify the day component.
*/
void SetDayOfYear(uint16_t doy);
/* ------------------------------------------------------------------------------------------------
* Retrieve the year component.
*/
SQMOD_NODISCARD uint16_t GetYear() const
{
return m_Year;
}
/* ------------------------------------------------------------------------------------------------
* Modify the year component.
*/
void SetYear(uint16_t year);
/* ------------------------------------------------------------------------------------------------
* Retrieve the month component.
*/
SQMOD_NODISCARD uint8_t GetMonth() const
{
return m_Month;
}
/* ------------------------------------------------------------------------------------------------
* Modify the month component.
*/
void SetMonth(uint8_t month);
/* ------------------------------------------------------------------------------------------------
* Retrieve the day component.
*/
SQMOD_NODISCARD uint8_t GetDay() const
{
return m_Day;
}
/* ------------------------------------------------------------------------------------------------
* Modify the day component.
*/
void SetDay(uint8_t day);
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Add the specified amount of years to the current date.
*/
Date & AddYears(int32_t years);
2016-06-04 10:55:06 +02:00
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of months to the current date.
*/
Date & AddMonths(int32_t months);
2016-06-04 10:55:06 +02:00
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of days to the current date.
*/
Date & AddDays(int32_t days);
2016-06-04 10:55:06 +02:00
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of years to obtain a new date.
*/
SQMOD_NODISCARD Date AndYears(int32_t years);
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Add the specified amount of months to obtain a new date.
*/
SQMOD_NODISCARD Date AndMonths(int32_t months);
/* ------------------------------------------------------------------------------------------------
2016-06-04 10:55:06 +02:00
* Add the specified amount of days to obtain a new date.
*/
SQMOD_NODISCARD Date AndDays(int32_t days);
/* ------------------------------------------------------------------------------------------------
* See whether the associated year is a leap year.
*/
SQMOD_NODISCARD bool IsThisLeapYear() const
{
return Chrono::IsLeapYear(m_Year);
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the number of days in the associated year.
*/
SQMOD_NODISCARD uint16_t GetYearDays() const
{
return Chrono::DaysInYear(m_Year);
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the number of days in the associated month.
*/
SQMOD_NODISCARD uint8_t GetMonthDays() const
{
return Chrono::DaysInMonth(m_Year, m_Month);
}
/* ------------------------------------------------------------------------------------------------
* Convert this date instance to a time-stamp.
*/
SQMOD_NODISCARD Timestamp GetTimestamp() const;
2021-08-18 20:37:33 +02:00
/* --------------------------------------------------------------------------------------------
*
*/
std::time_t ToTimeT() const;
};
} // Namespace:: SqMod