mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-12-20 23:17:18 +01:00
Rename source to module.
This commit is contained in:
414
module/Library/Chrono/Date.cpp
Normal file
414
module/Library/Chrono/Date.cpp
Normal file
@@ -0,0 +1,414 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(Typename, _SC("SqDate"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar Date::Delimiter = '-';
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Date::Compare(const Date & o) const
|
||||
{
|
||||
if (m_Year < o.m_Year)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Year > o.m_Year)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Month < o.m_Month)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Month > o.m_Month)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Day < o.m_Day)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Day > o.m_Day)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
// They're equal
|
||||
return 0;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::operator + (const Date & o) const
|
||||
{
|
||||
// Add the components individually
|
||||
return Date(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::operator - (const Date & o) const
|
||||
{
|
||||
return Date(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::operator * (const Date & o) const
|
||||
{
|
||||
return Date(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::operator / (const Date & o) const
|
||||
{
|
||||
return Date(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Date::ToString() const
|
||||
{
|
||||
return ToStrF("%04u%c%02u%c%02u", m_Year, m_Delimiter, m_Month, m_Delimiter, m_Day);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::Set(Uint16 year, Uint8 month, Uint8 day)
|
||||
{
|
||||
if (!Chrono::ValidDate(year, month, day))
|
||||
{
|
||||
STHROWF("Invalid date: %04u%c%02u%c%02u" , m_Year, m_Delimiter, m_Month, m_Delimiter, m_Day);
|
||||
}
|
||||
// Assign the specified values
|
||||
m_Year = year;
|
||||
m_Month = month;
|
||||
m_Day = day;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::SetStr(CSStr str)
|
||||
{
|
||||
// The format specifications that will be used to scan the string
|
||||
static SQChar fs[] = _SC(" %u - %u - %u ");
|
||||
// Is the specified string empty?
|
||||
if (!str || *str == '\0')
|
||||
{
|
||||
// Clear the values
|
||||
m_Year = 0;
|
||||
m_Month = 0;
|
||||
m_Day = 0;
|
||||
// We're done here
|
||||
return;
|
||||
}
|
||||
// Assign the specified delimiter
|
||||
fs[4] = m_Delimiter;
|
||||
fs[9] = m_Delimiter;
|
||||
// The sscanf function requires at least 32 bit integers
|
||||
Uint32 year = 0, month = 0, day = 0;
|
||||
// Attempt to extract the component values from the specified string
|
||||
sscanf(str, fs, &year, &month, &day);
|
||||
// Clamp the extracted values to the boundaries of associated type and assign them
|
||||
Set(ClampL< Uint32, Uint8 >(year),
|
||||
ClampL< Uint32, Uint8 >(month),
|
||||
ClampL< Uint32, Uint8 >(day)
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::SetDayOfYear(Uint16 doy)
|
||||
{
|
||||
// Reverse the given day of year to a full date
|
||||
Date d = Chrono::ReverseDayOfyear(m_Year, doy);
|
||||
// Set the obtained month
|
||||
SetMonth(d.m_Month);
|
||||
// Set the obtained day
|
||||
SetDay(d.m_Day);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::SetYear(Uint16 year)
|
||||
{
|
||||
// Make sure the year is valid
|
||||
if (!year)
|
||||
{
|
||||
STHROWF("Invalid year: %u", year);
|
||||
}
|
||||
// Assign the value
|
||||
m_Year = year;
|
||||
// Make sure the new date is valid
|
||||
if (!Chrono::ValidDate(m_Year, m_Month, m_Day))
|
||||
{
|
||||
m_Month = 1;
|
||||
m_Day = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::SetMonth(Uint8 month)
|
||||
{
|
||||
// Make sure the month is valid
|
||||
if (month == 0 || month > 12)
|
||||
{
|
||||
STHROWF("Invalid month: %u", month);
|
||||
}
|
||||
// Assign the value
|
||||
m_Month = month;
|
||||
// Make sure the month days are in range
|
||||
if (m_Day > Chrono::DaysInMonth(m_Year, m_Month))
|
||||
{
|
||||
m_Month = 1; // Fall back to the beginning of the month
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Date::SetDay(Uint8 day)
|
||||
{
|
||||
// Grab the amount of days in the current month
|
||||
const Uint8 dim = Chrono::DaysInMonth(m_Year, m_Month);
|
||||
// Make sure the day is valid
|
||||
if (day == 0)
|
||||
{
|
||||
STHROWF("Invalid day: %u", day);
|
||||
}
|
||||
else if (day > dim)
|
||||
{
|
||||
STHROWF("Day is out of range: %u > %u", day, dim);
|
||||
}
|
||||
// Assign the value
|
||||
m_Day = day;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date & Date::AddYears(Int32 years)
|
||||
{
|
||||
// Do we have a valid amount of years?
|
||||
if (years)
|
||||
{
|
||||
// Add the specified amount of years
|
||||
SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date & Date::AddMonths(Int32 months)
|
||||
{
|
||||
// Do we have a valid amount of months?
|
||||
if (months)
|
||||
{
|
||||
// Extract the number of years
|
||||
Int32 years = static_cast< Int32 >(months / 12);
|
||||
// Extract the number of months
|
||||
months = (months % 12) + m_Month;
|
||||
// Do we have extra months?
|
||||
if (months >= 12)
|
||||
{
|
||||
// Increase the years
|
||||
++years;
|
||||
// Subtract one year from months
|
||||
months %= 12;
|
||||
}
|
||||
else if (months < 0)
|
||||
{
|
||||
// Decrease the years
|
||||
--years;
|
||||
// Add one year to months
|
||||
months = 12 - months;
|
||||
}
|
||||
// Are there any years to add?
|
||||
if (years)
|
||||
{
|
||||
SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Add the months
|
||||
SetMonth(months);
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date & Date::AddDays(Int32 days)
|
||||
{
|
||||
// Do we have a valid amount of days?
|
||||
if (days)
|
||||
{
|
||||
// Whether the number of days is positive or negative
|
||||
const Int32 dir = days > 0 ? 1 : -1;
|
||||
// Grab current year
|
||||
Int32 year = m_Year;
|
||||
// Calculate the days in the current year
|
||||
Int32 diy = Chrono::DaysInYear(year);
|
||||
// Calculate the day of year
|
||||
Int32 doy = GetDayOfYear() + days;
|
||||
// Calculate the resulting years
|
||||
while (doy > diy || doy < 0)
|
||||
{
|
||||
doy -= diy * dir;
|
||||
year += dir;
|
||||
diy = Chrono::DaysInYear(year);
|
||||
}
|
||||
// Set the obtained year
|
||||
SetYear(year);
|
||||
// Set the obtained day of year
|
||||
SetDayOfYear(doy);
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::AndYears(Int32 years)
|
||||
{
|
||||
// Do we have a valid amount of years?
|
||||
if (!years)
|
||||
{
|
||||
return Date(*this); // Return the date as is
|
||||
}
|
||||
// Replicate the current date
|
||||
Date d(*this);
|
||||
// Add the specified amount of years
|
||||
d.SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
// Return the resulted date
|
||||
return d;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::AndMonths(Int32 months)
|
||||
{
|
||||
// Do we have a valid amount of months?
|
||||
if (!months)
|
||||
{
|
||||
return Date(*this); // Return the date as is
|
||||
}
|
||||
// Extract the number of years
|
||||
Int32 years = static_cast< Int32 >(months / 12);
|
||||
// Extract the number of months
|
||||
months = (months % 12) + m_Month;
|
||||
// Do we have extra months?
|
||||
if (months >= 12)
|
||||
{
|
||||
// Increase the years
|
||||
++years;
|
||||
// Subtract one year from months
|
||||
months %= 12;
|
||||
}
|
||||
else if (months < 0)
|
||||
{
|
||||
// Decrease the years
|
||||
--years;
|
||||
// Add one year to months
|
||||
months = 12 - months;
|
||||
}
|
||||
// Replicate the current date
|
||||
Date d(*this);
|
||||
// Are there any years to add?
|
||||
if (years)
|
||||
{
|
||||
d.SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Add the months
|
||||
d.SetMonth(months);
|
||||
// Return the resulted date
|
||||
return d;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Date::AndDays(Int32 days)
|
||||
{
|
||||
// Do we have a valid amount of days?
|
||||
if (!days)
|
||||
{
|
||||
return Date(*this); // Return the date as is
|
||||
}
|
||||
// Whether the number of days is positive or negative
|
||||
const Int32 dir = days > 0 ? 1 : -1;
|
||||
// Grab current year
|
||||
Int32 year = m_Year;
|
||||
// Calculate the days in the current year
|
||||
Int32 diy = Chrono::DaysInYear(year);
|
||||
// Calculate the day of year
|
||||
Int32 doy = GetDayOfYear() + days;
|
||||
// Calculate the resulting years
|
||||
while (doy > diy || doy < 0)
|
||||
{
|
||||
doy -= diy * dir;
|
||||
year += dir;
|
||||
diy = Chrono::DaysInYear(year);
|
||||
}
|
||||
// Replicate the current date
|
||||
Date d(*this);
|
||||
// Set the obtained year
|
||||
d.SetYear(year);
|
||||
// Set the obtained day of year
|
||||
d.SetDayOfYear(doy);
|
||||
// Return the resulted date
|
||||
return d;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp Date::GetTimestamp() const
|
||||
{
|
||||
// Calculate the current day of the year
|
||||
Int32 days = Chrono::DayOfYear(m_Year, m_Month, m_Day);
|
||||
// Calculate all days till the current year
|
||||
for (Int32 year = 0; year < m_Year; --year)
|
||||
{
|
||||
days += Chrono::DaysInYear(year);
|
||||
}
|
||||
// Return the resulted timestamp
|
||||
return Timestamp(static_cast< Int64 >(days * 86400000000LL));
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(Typename::Str,
|
||||
Class< Date >(vm, Typename::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< Uint16 >()
|
||||
.Ctor< Uint16, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8 >()
|
||||
// Static Properties
|
||||
.SetStaticValue(_SC("GlobalDelimiter"), &Date::Delimiter)
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
||||
.Func(_SC("_tostring"), &Date::ToString)
|
||||
.Func(_SC("cmp"), &Date::Cmp)
|
||||
// Meta-methods
|
||||
.Func< Date (Date::*)(const Date &) const >(_SC("_add"), &Date::operator +)
|
||||
.Func< Date (Date::*)(const Date &) const >(_SC("_sub"), &Date::operator -)
|
||||
.Func< Date (Date::*)(const Date &) const >(_SC("_mul"), &Date::operator *)
|
||||
.Func< Date (Date::*)(const Date &) const >(_SC("_div"), &Date::operator /)
|
||||
// Properties
|
||||
.Prop(_SC("Delimiter"), &Date::GetDelimiter, &Date::SetDelimiter)
|
||||
.Prop(_SC("Str"), &Date::GetStr, &Date::SetStr)
|
||||
.Prop(_SC("DayOfYear"), &Date::GetDayOfYear, &Date::SetDayOfYear)
|
||||
.Prop(_SC("Year"), &Date::GetYear, &Date::SetYear)
|
||||
.Prop(_SC("Month"), &Date::GetMonth, &Date::SetMonth)
|
||||
.Prop(_SC("Day"), &Date::GetDay, &Date::SetDay)
|
||||
.Prop(_SC("LeapYear"), &Date::IsThisLeapYear)
|
||||
.Prop(_SC("YearDays"), &Date::GetYearDays)
|
||||
.Prop(_SC("MonthDays"), &Date::GetMonthDays)
|
||||
.Prop(_SC("Timestamp"), &Date::GetTimestamp)
|
||||
// Member Methods
|
||||
.Func(_SC("AddYears"), &Date::AddYears)
|
||||
.Func(_SC("AddMonths"), &Date::AddMonths)
|
||||
.Func(_SC("AddDays"), &Date::AddDays)
|
||||
.Func(_SC("AndYears"), &Date::AndYears)
|
||||
.Func(_SC("AndMonths"), &Date::AndMonths)
|
||||
.Func(_SC("AndDays"), &Date::AndDays)
|
||||
// Overloaded Methods
|
||||
.Overload< void (Date::*)(Uint16) >(_SC("Set"), &Date::Set)
|
||||
.Overload< void (Date::*)(Uint16, Uint8) >(_SC("Set"), &Date::Set)
|
||||
.Overload< void (Date::*)(Uint16, Uint8, Uint8) >(_SC("Set"), &Date::Set)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
357
module/Library/Chrono/Date.hpp
Normal file
357
module/Library/Chrono/Date.hpp
Normal file
@@ -0,0 +1,357 @@
|
||||
#ifndef _LIBRARY_CHRONO_DATE_HPP_
|
||||
#define _LIBRARY_CHRONO_DATE_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Helper class used to represent a certain date.
|
||||
*/
|
||||
class Date
|
||||
{
|
||||
public:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static SQChar Delimiter;
|
||||
|
||||
private:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint16 m_Year; // Year
|
||||
Uint8 m_Month; // Month
|
||||
Uint8 m_Day; // Day
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar m_Delimiter; // Component delimiter when generating strings.
|
||||
|
||||
protected:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Compare the values of two instances.
|
||||
*/
|
||||
Int32 Compare(const Date & o) const;
|
||||
|
||||
public:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
Date()
|
||||
: m_Year(1970)
|
||||
, m_Month(1)
|
||||
, m_Day(1)
|
||||
, m_Delimiter(Delimiter)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic year constructor.
|
||||
*/
|
||||
Date(Uint16 year)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(year, 1, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic year and month constructor.
|
||||
*/
|
||||
Date(Uint16 year, Uint8 month)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(year, month, 1);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date constructor.
|
||||
*/
|
||||
Date(Uint16 year, Uint8 month, Uint8 day)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(year, month, day);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* String constructor.
|
||||
*/
|
||||
Date(CSStr str)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
SetStr(str);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
Int32 Cmp(const Date & o) const
|
||||
{
|
||||
return Compare(o);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year)
|
||||
{
|
||||
Set(year, m_Month, m_Day);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month)
|
||||
{
|
||||
Set(year, month, m_Day);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the local delimiter character.
|
||||
*/
|
||||
SQChar GetDelimiter() const
|
||||
{
|
||||
return m_Delimiter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the local delimiter character.
|
||||
*/
|
||||
void SetDelimiter(SQChar c)
|
||||
{
|
||||
m_Delimiter = c;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the values as a string.
|
||||
*/
|
||||
CSStr GetStr() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Extract the values from a string.
|
||||
*/
|
||||
void SetStr(CSStr str);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the day component.
|
||||
*/
|
||||
Uint16 GetDayOfYear() const
|
||||
{
|
||||
return Chrono::DayOfYear(m_Year, m_Month, m_Day);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the day component.
|
||||
*/
|
||||
void SetDayOfYear(Uint16 doy);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the year component.
|
||||
*/
|
||||
Uint16 GetYear() const
|
||||
{
|
||||
return m_Year;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the year component.
|
||||
*/
|
||||
void SetYear(Uint16 year);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the month component.
|
||||
*/
|
||||
Uint8 GetMonth() const
|
||||
{
|
||||
return m_Month;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the month component.
|
||||
*/
|
||||
void SetMonth(Uint8 month);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the day component.
|
||||
*/
|
||||
Uint8 GetDay() const
|
||||
{
|
||||
return m_Day;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the day component.
|
||||
*/
|
||||
void SetDay(Uint8 day);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of years to the current date.
|
||||
*/
|
||||
Date & AddYears(Int32 years);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of months to the current date.
|
||||
*/
|
||||
Date & AddMonths(Int32 months);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of days to the current date.
|
||||
*/
|
||||
Date & AddDays(Int32 days);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of years to obtain a new date.
|
||||
*/
|
||||
Date AndYears(Int32 years);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of months to obtain a new date.
|
||||
*/
|
||||
Date AndMonths(Int32 months);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of days to obtain a new date.
|
||||
*/
|
||||
Date AndDays(Int32 days);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* See whether the associated year is a leap year.
|
||||
*/
|
||||
bool IsThisLeapYear() const
|
||||
{
|
||||
return Chrono::IsLeapYear(m_Year);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of days in the associated year.
|
||||
*/
|
||||
Uint16 GetYearDays() const
|
||||
{
|
||||
return Chrono::DaysInYear(m_Year);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of days in the associated month.
|
||||
*/
|
||||
Uint8 GetMonthDays() const
|
||||
{
|
||||
return Chrono::DaysInMonth(m_Year, m_Month);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Convert this date instance to a time-stamp.
|
||||
*/
|
||||
Timestamp GetTimestamp() const;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CHRONO_DATE_HPP_
|
||||
822
module/Library/Chrono/Datetime.cpp
Normal file
822
module/Library/Chrono/Datetime.cpp
Normal file
@@ -0,0 +1,822 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(Typename, _SC("SqDatetime"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar Datetime::Delimiter = ' ';
|
||||
SQChar Datetime::DateDelim = '-';
|
||||
SQChar Datetime::TimeDelim = ':';
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Datetime::Compare(const Datetime & o) const
|
||||
{
|
||||
if (m_Year < o.m_Year)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Year > o.m_Year)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Month < o.m_Month)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Month > o.m_Month)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Day < o.m_Day)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Day > o.m_Day)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Hour < o.m_Hour)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Hour > o.m_Hour)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Minute < o.m_Minute)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Minute > o.m_Minute)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Second < o.m_Second)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Second > o.m_Second)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Millisecond < o.m_Millisecond)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Millisecond == o.m_Millisecond)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::operator + (const Datetime & o) const
|
||||
{
|
||||
// Add the components individually
|
||||
return Datetime(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::operator - (const Datetime & o) const
|
||||
{
|
||||
return Datetime(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::operator * (const Datetime & o) const
|
||||
{
|
||||
return Datetime(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::operator / (const Datetime & o) const
|
||||
{
|
||||
return Datetime(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Datetime::ToString() const
|
||||
{
|
||||
return ToStrF("%04u%c%02u%c%02u%c%02u%c%02u%c%02u%c%u"
|
||||
, m_Year, m_DateDelim, m_Month, m_DateDelim, m_Day
|
||||
, m_Delimiter
|
||||
, m_Hour, m_TimeDelim, m_Minute, m_TimeDelim, m_Second , m_TimeDelim, m_Millisecond
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::Set(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
|
||||
{
|
||||
// Validate the specified date
|
||||
if (!Chrono::ValidDate(year, month, day))
|
||||
{
|
||||
STHROWF("Invalid date: %04u%c%02u%c%02u%c%u"
|
||||
, m_DateDelim, m_Year
|
||||
, m_DateDelim, m_Month
|
||||
, m_DateDelim, m_Day
|
||||
);
|
||||
}
|
||||
// Is the specified hour within range?
|
||||
else if (hour >= 24)
|
||||
{
|
||||
STHROWF("Hour value is out of range: %u >= 24", hour);
|
||||
}
|
||||
// Is the specified minute within range?
|
||||
else if (minute >= 60)
|
||||
{
|
||||
STHROWF("Minute value is out of range: %u >= 60", minute);
|
||||
}
|
||||
// Is the specified second within range?
|
||||
else if (second >= 60)
|
||||
{
|
||||
STHROWF("Second value is out of range: %u >= 60", second);
|
||||
}
|
||||
// Is the specified millisecond within range?
|
||||
else if (millisecond >= 1000)
|
||||
{
|
||||
STHROWF("Millisecond value is out of range: %u >= 1000", millisecond);
|
||||
}
|
||||
// Assign the specified values
|
||||
m_Year = year;
|
||||
m_Month = month;
|
||||
m_Day = day;
|
||||
m_Hour = hour;
|
||||
m_Minute = minute;
|
||||
m_Second = second;
|
||||
m_Millisecond = millisecond;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetStr(CSStr str)
|
||||
{
|
||||
// The format specifications that will be used to scan the string
|
||||
static SQChar fs[] = _SC(" %u - %u - %u %u : %u : %u : %u ");
|
||||
// Is the specified string empty?
|
||||
if (!str || *str == '\0')
|
||||
{
|
||||
// Clear the values
|
||||
m_Year = 0;
|
||||
m_Month = 0;
|
||||
m_Day = 0;
|
||||
m_Hour = 0;
|
||||
m_Minute = 0;
|
||||
m_Second = 0;
|
||||
m_Millisecond = 0;
|
||||
// We're done here
|
||||
return;
|
||||
}
|
||||
// Assign the specified delimiter
|
||||
fs[4] = m_DateDelim;
|
||||
fs[9] = m_DateDelim;
|
||||
fs[14] = m_Delimiter;
|
||||
fs[19] = m_TimeDelim;
|
||||
fs[24] = m_TimeDelim;
|
||||
fs[29] = m_TimeDelim;
|
||||
// The sscanf function requires at least 32 bit integers
|
||||
Uint32 year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0, milli = 0;
|
||||
// Attempt to extract the component values from the specified string
|
||||
sscanf(str, fs, &year, &month, &day, &hour, &minute, &second, &milli);
|
||||
// Clamp the extracted values to the boundaries of associated type and assign them
|
||||
Set(ClampL< Uint32, Uint8 >(year),
|
||||
ClampL< Uint32, Uint8 >(month),
|
||||
ClampL< Uint32, Uint8 >(day),
|
||||
ClampL< Uint32, Uint8 >(hour),
|
||||
ClampL< Uint32, Uint8 >(minute),
|
||||
ClampL< Uint32, Uint8 >(second),
|
||||
ClampL< Uint32, Uint16 >(milli)
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetDayOfYear(Uint16 doy)
|
||||
{
|
||||
// Reverse the given day of year to a full date
|
||||
Date d = Chrono::ReverseDayOfyear(m_Year, doy);
|
||||
// Set the obtained month
|
||||
SetMonth(d.GetMonth());
|
||||
// Set the obtained day
|
||||
SetDay(d.GetDay());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetYear(Uint16 year)
|
||||
{
|
||||
// Make sure the year is valid
|
||||
if (!year)
|
||||
{
|
||||
STHROWF("Invalid year: %u", year);
|
||||
}
|
||||
// Assign the value
|
||||
m_Year = year;
|
||||
// Make sure the new date is valid
|
||||
if (!Chrono::ValidDate(m_Year, m_Month, m_Day))
|
||||
{
|
||||
m_Month = 1;
|
||||
m_Day = 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetMonth(Uint8 month)
|
||||
{
|
||||
// Make sure the month is valid
|
||||
if (month == 0 || month > 12)
|
||||
{
|
||||
STHROWF("Invalid month: %u", month);
|
||||
}
|
||||
// Assign the value
|
||||
m_Month = month;
|
||||
// Make sure the month days are in range
|
||||
if (m_Day > Chrono::DaysInMonth(m_Year, m_Month))
|
||||
{
|
||||
m_Month = 1; // Fall back to the beginning of the month
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetDay(Uint8 day)
|
||||
{
|
||||
// Grab the amount of days in the current month
|
||||
const Uint8 dim = Chrono::DaysInMonth(m_Year, m_Month);
|
||||
// Make sure the day is valid
|
||||
if (day == 0)
|
||||
{
|
||||
STHROWF("Invalid day: %u", day);
|
||||
}
|
||||
else if (day > dim)
|
||||
{
|
||||
STHROWF("Day is out of range: %u > %u", day, dim);
|
||||
}
|
||||
// Assign the value
|
||||
m_Day = day;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetHour(Uint8 hour)
|
||||
{
|
||||
// Is the specified hour within range?
|
||||
if (hour >= 24)
|
||||
{
|
||||
STHROWF("Hour value is out of range: %u >= 24", hour);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Hour = hour;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetMinute(Uint8 minute)
|
||||
{
|
||||
// Is the specified minute within range?
|
||||
if (minute >= 60)
|
||||
{
|
||||
STHROWF("Minute value is out of range: %u >= 60", minute);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Minute = minute;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetSecond(Uint8 second)
|
||||
{
|
||||
// Is the specified second within range?
|
||||
if (second >= 60)
|
||||
{
|
||||
STHROWF("Second value is out of range: %u >= 60", second);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Second = second;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Datetime::SetMillisecond(Uint16 millisecond)
|
||||
{
|
||||
// Is the specified millisecond within range?
|
||||
if (millisecond >= 1000)
|
||||
{
|
||||
STHROWF("Millisecond value is out of range: %u >= 1000", millisecond);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Millisecond = millisecond;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddYears(Int32 years)
|
||||
{
|
||||
// Do we have a valid amount of years?
|
||||
if (years)
|
||||
{
|
||||
// Add the specified amount of years
|
||||
SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddMonths(Int32 months)
|
||||
{
|
||||
// Do we have a valid amount of months?
|
||||
if (months)
|
||||
{
|
||||
// Extract the number of years
|
||||
Int32 years = static_cast< Int32 >(months / 12);
|
||||
// Extract the number of months
|
||||
months = (months % 12) + m_Month;
|
||||
// Do we have extra months?
|
||||
if (months >= 12)
|
||||
{
|
||||
// Increase the years
|
||||
++years;
|
||||
// Subtract one year from months
|
||||
months %= 12;
|
||||
}
|
||||
else if (months < 0)
|
||||
{
|
||||
// Decrease the years
|
||||
--years;
|
||||
// Add one year to months
|
||||
months = 12 - months;
|
||||
}
|
||||
// Are there any years to add?
|
||||
if (years)
|
||||
{
|
||||
SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Add the months
|
||||
SetMonth(months);
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddDays(Int32 days)
|
||||
{
|
||||
// Do we have a valid amount of days?
|
||||
if (days)
|
||||
{
|
||||
// Whether the number of days is positive or negative
|
||||
const Int32 dir = days > 0 ? 1 : -1;
|
||||
// Grab current year
|
||||
Int32 year = m_Year;
|
||||
// Calculate the days in the current year
|
||||
Int32 diy = Chrono::DaysInYear(year);
|
||||
// Calculate the day of year
|
||||
Int32 doy = GetDayOfYear() + days;
|
||||
// Calculate the resulting years
|
||||
while (doy > diy || doy < 0)
|
||||
{
|
||||
doy -= diy * dir;
|
||||
year += dir;
|
||||
diy = Chrono::DaysInYear(year);
|
||||
}
|
||||
// Set the obtained year
|
||||
SetYear(year);
|
||||
// Set the obtained day of year
|
||||
SetDayOfYear(doy);
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddHours(Int32 hours)
|
||||
{
|
||||
// Did we even add any hours?
|
||||
if (hours)
|
||||
{
|
||||
// Extract the number of days
|
||||
Int32 days = static_cast< Int32 >(hours / 24);
|
||||
// Extract the number of hours
|
||||
m_Hour += (hours % 24);
|
||||
// Are the hours overlapping with the next day?
|
||||
if (m_Hour >= 24)
|
||||
{
|
||||
// Increase the days
|
||||
++days;
|
||||
// Subtract one day from hours
|
||||
m_Hour %= 24;
|
||||
}
|
||||
// Should we add any days?
|
||||
if (days)
|
||||
{
|
||||
AddDays(days);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddMinutes(Int32 minutes)
|
||||
{
|
||||
// Did we even add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
// Extract the number of hours
|
||||
Int32 hours = static_cast< Int32 >(minutes / 60);
|
||||
// Extract the number of minutes
|
||||
m_Minute += (minutes % 60);
|
||||
// Are the minutes overlapping with the next hour?
|
||||
if (m_Minute >= 60)
|
||||
{
|
||||
// Increase the hours
|
||||
++hours;
|
||||
// Subtract one hour from minutes
|
||||
m_Minute %= 60;
|
||||
}
|
||||
// Should we add any hours?
|
||||
if (hours)
|
||||
{
|
||||
AddHours(hours);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddSeconds(Int32 seconds)
|
||||
{
|
||||
// Did we even add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
// Extract the number of minutes
|
||||
Int32 minutes = static_cast< Int32 >(seconds / 60);
|
||||
// Extract the number of seconds
|
||||
m_Second += (seconds % 60);
|
||||
// Are the seconds overlapping with the next minute?
|
||||
if (m_Second >= 60)
|
||||
{
|
||||
// Increase the minutes
|
||||
++minutes;
|
||||
// Subtract one minute from seconds
|
||||
m_Second %= 60;
|
||||
}
|
||||
// Should we add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
AddMinutes(minutes);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime & Datetime::AddMilliseconds(Int32 milliseconds)
|
||||
{
|
||||
// Did we even add any milliseconds?
|
||||
if (milliseconds)
|
||||
{
|
||||
// Extract the number of seconds
|
||||
Int32 seconds = static_cast< Int32 >(milliseconds / 1000);
|
||||
// Extract the number of milliseconds
|
||||
m_Millisecond += (milliseconds / 1000);
|
||||
// Are the milliseconds overlapping with the next second?
|
||||
if (m_Millisecond >= 1000)
|
||||
{
|
||||
// Increase the seconds
|
||||
++seconds;
|
||||
// Subtract one second from milliseconds
|
||||
m_Millisecond %= 1000;
|
||||
}
|
||||
// Should we add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
AddSeconds(seconds);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndYears(Int32 years)
|
||||
{
|
||||
// Do we have a valid amount of years?
|
||||
if (!years)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Replicate the current date
|
||||
Datetime dt(*this);
|
||||
// Add the specified amount of years
|
||||
dt.SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
// Return the resulted date
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndMonths(Int32 months)
|
||||
{
|
||||
// Do we have a valid amount of months?
|
||||
if (!months)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Extract the number of years
|
||||
Int32 years = static_cast< Int32 >(months / 12);
|
||||
// Extract the number of months
|
||||
months = (months % 12) + m_Month;
|
||||
// Do we have extra months?
|
||||
if (months >= 12)
|
||||
{
|
||||
// Increase the years
|
||||
++years;
|
||||
// Subtract one year from months
|
||||
months %= 12;
|
||||
}
|
||||
else if (months < 0)
|
||||
{
|
||||
// Decrease the years
|
||||
--years;
|
||||
// Add one year to months
|
||||
months = 12 - months;
|
||||
}
|
||||
// Replicate the current date
|
||||
Datetime dt(*this);
|
||||
// Are there any years to add?
|
||||
if (years)
|
||||
{
|
||||
dt.SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
|
||||
}
|
||||
// Add the months
|
||||
dt.SetMonth(months);
|
||||
// Return the resulted date
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndDays(Int32 days)
|
||||
{
|
||||
// Do we have a valid amount of days?
|
||||
if (!days)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Whether the number of days is positive or negative
|
||||
const Int32 dir = days > 0 ? 1 : -1;
|
||||
// Grab current year
|
||||
Int32 year = m_Year;
|
||||
// Calculate the days in the current year
|
||||
Int32 diy = Chrono::DaysInYear(year);
|
||||
// Calculate the day of year
|
||||
Int32 doy = GetDayOfYear() + days;
|
||||
// Calculate the resulting years
|
||||
while (doy > diy || doy < 0)
|
||||
{
|
||||
doy -= diy * dir;
|
||||
year += dir;
|
||||
diy = Chrono::DaysInYear(year);
|
||||
}
|
||||
// Replicate the current date
|
||||
Datetime dt(*this);
|
||||
// Set the obtained year
|
||||
dt.SetYear(year);
|
||||
// Set the obtained day of year
|
||||
dt.SetDayOfYear(doy);
|
||||
// Return the resulted date
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndHours(Int32 hours)
|
||||
{
|
||||
// Did we even add any hours?
|
||||
if (!hours)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Extract the number of days
|
||||
Int32 days = static_cast< Int32 >(hours / 24);
|
||||
// Extract the number of hours
|
||||
hours = m_Hour + (hours % 24);
|
||||
// Are the hours overlapping with the next day?
|
||||
if (hours >= 24)
|
||||
{
|
||||
++days; // Increase the days
|
||||
}
|
||||
// Replicate the current time
|
||||
Datetime dt(*this);
|
||||
// Should we add any days?
|
||||
if (days)
|
||||
{
|
||||
dt.AddDays(days);
|
||||
}
|
||||
// Assign the resulted hours
|
||||
dt.m_Hour = (hours % 24);
|
||||
// Return the result
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndMinutes(Int32 minutes)
|
||||
{
|
||||
// Did we even added any minutes?
|
||||
if (!minutes)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Extract the number of hours
|
||||
Int32 hours = static_cast< Int32 >(minutes / 60);
|
||||
// Extract the number of minutes
|
||||
minutes = m_Minute + (minutes % 60);
|
||||
// Are the minutes overlapping with the next hour?
|
||||
if (minutes >= 60)
|
||||
{
|
||||
++hours; // Increase hours
|
||||
}
|
||||
// Replicate the current time
|
||||
Datetime dt(*this);
|
||||
// Should we add any hours?
|
||||
if (hours)
|
||||
{
|
||||
dt.AddHours(hours);
|
||||
}
|
||||
// Assign the resulted minutes
|
||||
dt.m_Minute = (minutes % 60);
|
||||
// Return the result
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndSeconds(Int32 seconds)
|
||||
{
|
||||
// Did we even added any seconds?
|
||||
if (!seconds)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Extract the number of minutes
|
||||
Int32 minutes = static_cast< Int32 >(seconds / 60);
|
||||
// Extract the number of seconds
|
||||
seconds = m_Second + (seconds % 60);
|
||||
// Are the seconds overlapping with the next minute?
|
||||
if (seconds >= 60)
|
||||
{
|
||||
++minutes; // Increase minutes
|
||||
}
|
||||
// Replicate the current time
|
||||
Datetime dt(*this);
|
||||
// Should we add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
dt.AddMinutes(minutes);
|
||||
}
|
||||
// Assign the resulted seconds
|
||||
dt.m_Second = (seconds % 60);
|
||||
// Return the result
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Datetime Datetime::AndMilliseconds(Int32 milliseconds)
|
||||
{
|
||||
// Did we even added any milliseconds?
|
||||
if (!milliseconds)
|
||||
{
|
||||
return Datetime(*this); // Return the date-time as is
|
||||
}
|
||||
// Extract the number of seconds
|
||||
Int32 seconds = static_cast< Int32 >(milliseconds / 1000);
|
||||
// Extract the number of milliseconds
|
||||
milliseconds = m_Millisecond + (milliseconds % 1000);
|
||||
// Are the milliseconds overlapping with the next second?
|
||||
if (milliseconds >= 1000)
|
||||
{
|
||||
++seconds; // Increase seconds
|
||||
}
|
||||
// Replicate the current time
|
||||
Datetime dt(*this);
|
||||
// Should we add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
dt.AddSeconds(seconds);
|
||||
}
|
||||
// Assign the resulted milliseconds
|
||||
dt.m_Millisecond = (milliseconds % 1000);
|
||||
// Return the result
|
||||
return dt;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Date Datetime::GetDate() const
|
||||
{
|
||||
return Date(m_Year, m_Month, m_Day);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Datetime::GetTime() const
|
||||
{
|
||||
return Time(m_Hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp Datetime::GetTimestamp() const
|
||||
{
|
||||
// Calculate the current day of the year
|
||||
Int32 days = Chrono::DayOfYear(m_Year, m_Month, m_Day);
|
||||
// Calculate all days till the current year
|
||||
for (Int32 year = 0; year < m_Year; --year)
|
||||
{
|
||||
days += Chrono::DaysInYear(year);
|
||||
}
|
||||
// Calculate the microseconds in the resulted days
|
||||
Int64 ms = static_cast< Int64 >(days * 86400000000LL);
|
||||
// Calculate the microseconds in the current time
|
||||
ms += static_cast< Int64 >(m_Hour * 3600000000LL);
|
||||
ms += static_cast< Int64 >(m_Minute * 60000000L);
|
||||
ms += static_cast< Int64 >(m_Second * 1000000L);
|
||||
ms += static_cast< Int64 >(m_Millisecond * 1000L);
|
||||
// Return the resulted timestamp
|
||||
return Timestamp(ms);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(Typename::Str,
|
||||
Class< Datetime >(vm, Typename::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< Uint16 >()
|
||||
.Ctor< Uint16, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8, Uint8, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8, Uint8, Uint8, Uint8 >()
|
||||
.Ctor< Uint16, Uint8, Uint8, Uint8, Uint8, Uint8, Uint16 >()
|
||||
// Static Properties
|
||||
.SetStaticValue(_SC("GlobalDelimiter"), &Datetime::Delimiter)
|
||||
.SetStaticValue(_SC("GlobalDateDelim"), &Datetime::DateDelim)
|
||||
.SetStaticValue(_SC("GlobalTimeDelim"), &Datetime::TimeDelim)
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
||||
.Func(_SC("_tostring"), &Datetime::ToString)
|
||||
.Func(_SC("cmp"), &Datetime::Cmp)
|
||||
// Meta-methods
|
||||
.Func< Datetime (Datetime::*)(const Datetime &) const >(_SC("_add"), &Datetime::operator +)
|
||||
.Func< Datetime (Datetime::*)(const Datetime &) const >(_SC("_sub"), &Datetime::operator -)
|
||||
.Func< Datetime (Datetime::*)(const Datetime &) const >(_SC("_mul"), &Datetime::operator *)
|
||||
.Func< Datetime (Datetime::*)(const Datetime &) const >(_SC("_div"), &Datetime::operator /)
|
||||
// Properties
|
||||
.Prop(_SC("Delimiter"), &Datetime::GetDelimiter, &Datetime::SetDelimiter)
|
||||
.Prop(_SC("DateDelim"), &Datetime::GetDateDelim, &Datetime::SetDateDelim)
|
||||
.Prop(_SC("TimeDelim"), &Datetime::GetTimeDelim, &Datetime::SetTimeDelim)
|
||||
.Prop(_SC("Str"), &Datetime::GetStr, &Datetime::SetStr)
|
||||
.Prop(_SC("DayOfYear"), &Datetime::GetDayOfYear, &Datetime::SetDayOfYear)
|
||||
.Prop(_SC("Year"), &Datetime::GetYear, &Datetime::SetYear)
|
||||
.Prop(_SC("Month"), &Datetime::GetMonth, &Datetime::SetMonth)
|
||||
.Prop(_SC("Day"), &Datetime::GetDay, &Datetime::SetDay)
|
||||
.Prop(_SC("Hour"), &Datetime::GetHour, &Datetime::SetHour)
|
||||
.Prop(_SC("Minute"), &Datetime::GetMinute, &Datetime::SetMinute)
|
||||
.Prop(_SC("Second"), &Datetime::GetSecond, &Datetime::SetSecond)
|
||||
.Prop(_SC("Millisecond"), &Datetime::GetMillisecond, &Datetime::SetMillisecond)
|
||||
.Prop(_SC("LeapYear"), &Datetime::IsThisLeapYear)
|
||||
.Prop(_SC("YearDays"), &Datetime::GetYearDays)
|
||||
.Prop(_SC("MonthDays"), &Datetime::GetMonthDays)
|
||||
.Prop(_SC("Date"), &Datetime::GetDate)
|
||||
.Prop(_SC("Time"), &Datetime::GetTime)
|
||||
.Prop(_SC("Timestamp"), &Datetime::GetTimestamp)
|
||||
// Member Methods
|
||||
.Func(_SC("AddYears"), &Datetime::AddYears)
|
||||
.Func(_SC("AddMonths"), &Datetime::AddMonths)
|
||||
.Func(_SC("AddDays"), &Datetime::AddDays)
|
||||
.Func(_SC("AddHours"), &Datetime::AddHours)
|
||||
.Func(_SC("AddMinutes"), &Datetime::AddMinutes)
|
||||
.Func(_SC("AddSeconds"), &Datetime::AddSeconds)
|
||||
.Func(_SC("AddMillis"), &Datetime::AddMilliseconds)
|
||||
.Func(_SC("AddMilliseconds"), &Datetime::AddMilliseconds)
|
||||
.Func(_SC("AndYears"), &Datetime::AndYears)
|
||||
.Func(_SC("AndMonths"), &Datetime::AndMonths)
|
||||
.Func(_SC("AndDays"), &Datetime::AndDays)
|
||||
.Func(_SC("AndHours"), &Datetime::AndHours)
|
||||
.Func(_SC("AndMinutes"), &Datetime::AndMinutes)
|
||||
.Func(_SC("AndSeconds"), &Datetime::AndSeconds)
|
||||
.Func(_SC("AndMillis"), &Datetime::AndMilliseconds)
|
||||
.Func(_SC("AndMilliseconds"), &Datetime::AndMilliseconds)
|
||||
// Overloaded Methods
|
||||
.Overload< void (Datetime::*)(Uint16) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8, Uint8) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8, Uint8, Uint8) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8, Uint8, Uint8, Uint8) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8, Uint8, Uint8, Uint8, Uint8) >(_SC("Set"), &Datetime::Set)
|
||||
.Overload< void (Datetime::*)(Uint16, Uint8, Uint8, Uint8, Uint8, Uint8, Uint16) >(_SC("Set"), &Datetime::Set)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
580
module/Library/Chrono/Datetime.hpp
Normal file
580
module/Library/Chrono/Datetime.hpp
Normal file
@@ -0,0 +1,580 @@
|
||||
#ifndef _LIBRARY_CHRONO_DATETIME_HPP_
|
||||
#define _LIBRARY_CHRONO_DATETIME_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Helper class used to represent a certain date and time.
|
||||
*/
|
||||
class Datetime
|
||||
{
|
||||
public:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static SQChar Delimiter;
|
||||
static SQChar DateDelim;
|
||||
static SQChar TimeDelim;
|
||||
|
||||
private:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint16 m_Year; // Year
|
||||
Uint8 m_Month; // Month
|
||||
Uint8 m_Day; // Day
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint8 m_Hour; // Hour
|
||||
Uint8 m_Minute; // Minute
|
||||
Uint8 m_Second; // Second
|
||||
Uint16 m_Millisecond; // Millisecond
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar m_Delimiter; // Date and time delimiter when generating strings.
|
||||
SQChar m_DateDelim; // Date component delimiter when generating strings.
|
||||
SQChar m_TimeDelim; // Time component delimiter when generating strings.
|
||||
|
||||
protected:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Compare the values of two instances.
|
||||
*/
|
||||
Int32 Compare(const Datetime & o) const;
|
||||
|
||||
public:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
Datetime()
|
||||
: m_Year(1970)
|
||||
, m_Month(1)
|
||||
, m_Day(1)
|
||||
, m_Hour(0)
|
||||
, m_Minute(0)
|
||||
, m_Second(0)
|
||||
, m_Millisecond(0)
|
||||
, m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic year constructor.
|
||||
*/
|
||||
Datetime(Uint16 year)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, 1, 1, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic year and month constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, 1, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month, Uint8 day)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, day, 0, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date and hour constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month, Uint8 day, Uint8 hour)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, day, hour, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date, hour and minute constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, day, hour, minute, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date and time constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute, Uint8 second)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, day, hour, minute, second, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Speciffic date and precise time constructor.
|
||||
*/
|
||||
Datetime(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
|
||||
: m_Delimiter(Delimiter)
|
||||
, m_DateDelim(DateDelim)
|
||||
, m_TimeDelim(TimeDelim)
|
||||
{
|
||||
Set(year, month, day, hour, minute, second, millisecond);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy constructor.
|
||||
*/
|
||||
Datetime(const Datetime & o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move constructor.
|
||||
*/
|
||||
Datetime(Datetime && o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Destructor.
|
||||
*/
|
||||
~Datetime() = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Copy assignment operator.
|
||||
*/
|
||||
Datetime & operator = (const Datetime & o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Move assignment operator.
|
||||
*/
|
||||
Datetime & operator = (Datetime && o) = default;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Equality comparison operator.
|
||||
*/
|
||||
bool operator == (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) == 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Inequality comparison operator.
|
||||
*/
|
||||
bool operator != (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) != 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Less than comparison operator.
|
||||
*/
|
||||
bool operator < (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) < 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Greater than comparison operator.
|
||||
*/
|
||||
bool operator > (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) > 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Less than or equal comparison operator.
|
||||
*/
|
||||
bool operator <= (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) <= 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Greater than or equal comparison operator.
|
||||
*/
|
||||
bool operator >= (const Datetime & o) const
|
||||
{
|
||||
return Compare(o) >= 0;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Addition operator.
|
||||
*/
|
||||
Datetime operator + (const Datetime & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Subtraction operator.
|
||||
*/
|
||||
Datetime operator - (const Datetime & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Multiplication operator.
|
||||
*/
|
||||
Datetime operator * (const Datetime & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Division operator.
|
||||
*/
|
||||
Datetime operator / (const Datetime & o) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to compare two instances of this type.
|
||||
*/
|
||||
Int32 Cmp(const Datetime & o) const
|
||||
{
|
||||
return Compare(o);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year)
|
||||
{
|
||||
Set(year, m_Month, m_Day, m_Hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month)
|
||||
{
|
||||
Set(year, month, m_Day, m_Hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day)
|
||||
{
|
||||
Set(year, month, day, m_Hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day, Uint8 hour)
|
||||
{
|
||||
Set(year, month, day, hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute)
|
||||
{
|
||||
Set(year, month, day, hour, minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute, Uint8 second)
|
||||
{
|
||||
Set(year, month, day, hour, minute, second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint16 year, Uint8 month, Uint8 day, Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the local delimiter character.
|
||||
*/
|
||||
SQChar GetDelimiter() const
|
||||
{
|
||||
return m_Delimiter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the local delimiter character.
|
||||
*/
|
||||
void SetDelimiter(SQChar c)
|
||||
{
|
||||
m_Delimiter = c;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the local date delimiter character.
|
||||
*/
|
||||
SQChar GetDateDelim() const
|
||||
{
|
||||
return m_DateDelim;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the local date delimiter character.
|
||||
*/
|
||||
void SetDateDelim(SQChar c)
|
||||
{
|
||||
m_DateDelim = c;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the local time delimiter character.
|
||||
*/
|
||||
SQChar GetTimeDelim() const
|
||||
{
|
||||
return m_TimeDelim;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the local time delimiter character.
|
||||
*/
|
||||
void SetTimeDelim(SQChar c)
|
||||
{
|
||||
m_TimeDelim = c;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the values as a string.
|
||||
*/
|
||||
CSStr GetStr() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Extract the values from a string.
|
||||
*/
|
||||
void SetStr(CSStr str);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the day component.
|
||||
*/
|
||||
Uint16 GetDayOfYear() const
|
||||
{
|
||||
return Chrono::DayOfYear(m_Year, m_Month, m_Day);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the day component.
|
||||
*/
|
||||
void SetDayOfYear(Uint16 doy);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the year component.
|
||||
*/
|
||||
Uint16 GetYear() const
|
||||
{
|
||||
return m_Year;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the year component.
|
||||
*/
|
||||
void SetYear(Uint16 year);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the month component.
|
||||
*/
|
||||
Uint8 GetMonth() const
|
||||
{
|
||||
return m_Month;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the month component.
|
||||
*/
|
||||
void SetMonth(Uint8 month);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the day component.
|
||||
*/
|
||||
Uint8 GetDay() const
|
||||
{
|
||||
return m_Day;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the day component.
|
||||
*/
|
||||
void SetDay(Uint8 day);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the hour component.
|
||||
*/
|
||||
Uint8 GetHour() const
|
||||
{
|
||||
return m_Hour;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the hour component.
|
||||
*/
|
||||
void SetHour(Uint8 hour);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the minute component.
|
||||
*/
|
||||
Uint8 GetMinute() const
|
||||
{
|
||||
return m_Minute;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the minute component.
|
||||
*/
|
||||
void SetMinute(Uint8 minute);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the second component.
|
||||
*/
|
||||
Uint8 GetSecond() const
|
||||
{
|
||||
return m_Second;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the second component.
|
||||
*/
|
||||
void SetSecond(Uint8 second);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the millisecond component.
|
||||
*/
|
||||
Uint16 GetMillisecond() const
|
||||
{
|
||||
return m_Millisecond;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the millisecond component.
|
||||
*/
|
||||
void SetMillisecond(Uint16 millisecond);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of years to the current date.
|
||||
*/
|
||||
Datetime & AddYears(Int32 years);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of months to the current date.
|
||||
*/
|
||||
Datetime & AddMonths(Int32 months);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of days to the current date.
|
||||
*/
|
||||
Datetime & AddDays(Int32 days);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of hours to the current time.
|
||||
*/
|
||||
Datetime & AddHours(Int32 hours);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of minutes to the current time.
|
||||
*/
|
||||
Datetime & AddMinutes(Int32 minutes);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of seconds to the current time.
|
||||
*/
|
||||
Datetime & AddSeconds(Int32 seconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of milliseconds to the current time.
|
||||
*/
|
||||
Datetime & AddMilliseconds(Int32 milliseconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of years to obtain a new date.
|
||||
*/
|
||||
Datetime AndYears(Int32 years);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of months to obtain a new date.
|
||||
*/
|
||||
Datetime AndMonths(Int32 months);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of days to obtain a new date.
|
||||
*/
|
||||
Datetime AndDays(Int32 days);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of hours to obtain a new time.
|
||||
*/
|
||||
Datetime AndHours(Int32 hours);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of minutes to obtain a new time.
|
||||
*/
|
||||
Datetime AndMinutes(Int32 minutes);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of seconds to obtain a new time.
|
||||
*/
|
||||
Datetime AndSeconds(Int32 seconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of milliseconds to obtain a new time.
|
||||
*/
|
||||
Datetime AndMilliseconds(Int32 milliseconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* See whether the associated year is a leap year.
|
||||
*/
|
||||
bool IsThisLeapYear() const
|
||||
{
|
||||
return Chrono::IsLeapYear(m_Year);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of days in the associated year.
|
||||
*/
|
||||
Uint16 GetYearDays() const
|
||||
{
|
||||
return Chrono::DaysInYear(m_Year);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the number of days in the associated month.
|
||||
*/
|
||||
Uint8 GetMonthDays() const
|
||||
{
|
||||
return Chrono::DaysInMonth(m_Year, m_Month);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the date from this date-time instance.
|
||||
*/
|
||||
Date GetDate() const;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the time from this date-time instance.
|
||||
*/
|
||||
Time GetTime() const;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Convert this date-time instance to a time-stamp.
|
||||
*/
|
||||
Timestamp GetTimestamp() const;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CHRONO_DATETIME_HPP_
|
||||
464
module/Library/Chrono/Time.cpp
Normal file
464
module/Library/Chrono/Time.cpp
Normal file
@@ -0,0 +1,464 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(Typename, _SC("SqTime"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar Time::Delimiter = ':';
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Time::Compare(const Time & o) const
|
||||
{
|
||||
if (m_Hour < o.m_Hour)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Hour > o.m_Hour)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Minute < o.m_Minute)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Minute > o.m_Minute)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Second < o.m_Second)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Second > o.m_Second)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else if (m_Millisecond < o.m_Millisecond)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
else if (m_Millisecond == o.m_Millisecond)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::operator + (const Time & o) const
|
||||
{
|
||||
return Time(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::operator - (const Time & o) const
|
||||
{
|
||||
return Time(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::operator * (const Time & o) const
|
||||
{
|
||||
return Time(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::operator / (const Time & o) const
|
||||
{
|
||||
return Time(o);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Time::ToString() const
|
||||
{
|
||||
return ToStrF("%02u%c%02u%c%02u%c%u",
|
||||
m_Hour, m_Delimiter,
|
||||
m_Minute, m_Delimiter,
|
||||
m_Second, m_Delimiter,
|
||||
m_Millisecond);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::Set(Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
|
||||
{
|
||||
// Is the specified hour within range?
|
||||
if (hour >= 24)
|
||||
{
|
||||
STHROWF("Hour value is out of range: %u >= 24", hour);
|
||||
}
|
||||
// Is the specified minute within range?
|
||||
else if (minute >= 60)
|
||||
{
|
||||
STHROWF("Minute value is out of range: %u >= 60", minute);
|
||||
}
|
||||
// Is the specified second within range?
|
||||
else if (second >= 60)
|
||||
{
|
||||
STHROWF("Second value is out of range: %u >= 60", second);
|
||||
}
|
||||
// Is the specified millisecond within range?
|
||||
else if (millisecond >= 1000)
|
||||
{
|
||||
STHROWF("Millisecond value is out of range: %u >= 1000", millisecond);
|
||||
}
|
||||
// Now it's safe to assign the values
|
||||
m_Hour = hour;
|
||||
m_Minute = minute;
|
||||
m_Second = second;
|
||||
m_Millisecond = millisecond;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::SetStr(CSStr str)
|
||||
{
|
||||
// The format specifications that will be used to scan the string
|
||||
static SQChar fs[] = _SC(" %u : %u : %u : %u ");
|
||||
// Is the specified string empty?
|
||||
if (!str || *str == '\0')
|
||||
{
|
||||
// Clear the values
|
||||
m_Hour = 0;
|
||||
m_Minute = 0;
|
||||
m_Second = 0;
|
||||
m_Millisecond = 0;
|
||||
// We're done here
|
||||
return;
|
||||
}
|
||||
// Assign the specified delimiter
|
||||
fs[4] = m_Delimiter;
|
||||
fs[9] = m_Delimiter;
|
||||
fs[14] = m_Delimiter;
|
||||
// The sscanf function requires at least 32 bit integers
|
||||
Uint32 hour = 0, minute = 0, second = 0, milli = 0;
|
||||
// Attempt to extract the component values from the specified string
|
||||
sscanf(str, fs, &hour, &minute, &second, &milli);
|
||||
// Clamp the extracted values to the boundaries of associated type and assign them
|
||||
Set(ClampL< Uint32, Uint8 >(hour),
|
||||
ClampL< Uint32, Uint8 >(minute),
|
||||
ClampL< Uint32, Uint8 >(second),
|
||||
ClampL< Uint32, Uint16 >(milli)
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::SetHour(Uint8 hour)
|
||||
{
|
||||
// Is the specified hour within range?
|
||||
if (hour >= 24)
|
||||
{
|
||||
STHROWF("Hour value is out of range: %u >= 24", hour);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Hour = hour;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::SetMinute(Uint8 minute)
|
||||
{
|
||||
// Is the specified minute within range?
|
||||
if (minute >= 60)
|
||||
{
|
||||
STHROWF("Minute value is out of range: %u >= 60", minute);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Minute = minute;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::SetSecond(Uint8 second)
|
||||
{
|
||||
// Is the specified second within range?
|
||||
if (second >= 60)
|
||||
{
|
||||
STHROWF("Second value is out of range: %u >= 60", second);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Second = second;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Time::SetMillisecond(Uint16 millisecond)
|
||||
{
|
||||
// Is the specified millisecond within range?
|
||||
if (millisecond >= 1000)
|
||||
{
|
||||
STHROWF("Millisecond value is out of range: %u >= 1000", millisecond);
|
||||
}
|
||||
// Now it's safe to assign the value
|
||||
m_Millisecond = millisecond;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time & Time::AddHours(Int32 hours)
|
||||
{
|
||||
// Did we even add any hours?
|
||||
if (hours)
|
||||
{
|
||||
// Add the specified amount of hours
|
||||
m_Hour += (hours % 24);
|
||||
// Make sure the value is within range
|
||||
m_Hour %= 24;
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time & Time::AddMinutes(Int32 minutes)
|
||||
{
|
||||
// Did we even add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
// Extract the number of hours
|
||||
Int32 hours = static_cast< Int32 >(minutes / 60);
|
||||
// Extract the number of minutes
|
||||
m_Minute += (minutes % 60);
|
||||
// Are the minutes overlapping with the next hour?
|
||||
if (m_Minute >= 60)
|
||||
{
|
||||
// Increase the hours
|
||||
++hours;
|
||||
// Subtract one hour from minutes
|
||||
m_Minute %= 60;
|
||||
}
|
||||
// Should we add any hours?
|
||||
if (hours)
|
||||
{
|
||||
AddHours(hours);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time & Time::AddSeconds(Int32 seconds)
|
||||
{
|
||||
// Did we even add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
// Extract the number of minutes
|
||||
Int32 minutes = static_cast< Int32 >(seconds / 60);
|
||||
// Extract the number of seconds
|
||||
m_Second += (seconds % 60);
|
||||
// Are the seconds overlapping with the next minute?
|
||||
if (m_Second >= 60)
|
||||
{
|
||||
// Increase the minutes
|
||||
++minutes;
|
||||
// Subtract one minute from seconds
|
||||
m_Second %= 60;
|
||||
}
|
||||
// Should we add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
AddMinutes(minutes);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time & Time::AddMilliseconds(Int32 milliseconds)
|
||||
{
|
||||
// Did we even add any milliseconds?
|
||||
if (milliseconds)
|
||||
{
|
||||
// Extract the number of seconds
|
||||
Int32 seconds = static_cast< Int32 >(milliseconds / 1000);
|
||||
// Extract the number of milliseconds
|
||||
m_Millisecond += (milliseconds / 1000);
|
||||
// Are the milliseconds overlapping with the next second?
|
||||
if (m_Millisecond >= 1000)
|
||||
{
|
||||
// Increase the seconds
|
||||
++seconds;
|
||||
// Subtract one second from milliseconds
|
||||
m_Millisecond %= 1000;
|
||||
}
|
||||
// Should we add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
AddSeconds(seconds);
|
||||
}
|
||||
}
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AndHours(Int32 hours)
|
||||
{
|
||||
// Did we even add any hours?
|
||||
if (hours)
|
||||
{
|
||||
return Time((m_Hour + (hours % 24)) % 24, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
// Return the time as is
|
||||
return Time(*this);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AndMinutes(Int32 minutes)
|
||||
{
|
||||
// Did we even added any minutes?
|
||||
if (!minutes)
|
||||
{
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of hours
|
||||
Int32 hours = static_cast< Int32 >(minutes / 60);
|
||||
// Extract the number of minutes
|
||||
minutes = m_Minute + (minutes % 60);
|
||||
// Are the minutes overlapping with the next hour?
|
||||
if (minutes >= 60)
|
||||
{
|
||||
++hours; // Increase hours
|
||||
}
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any hours?
|
||||
if (hours)
|
||||
{
|
||||
t.AddHours(hours);
|
||||
}
|
||||
// Assign the resulted minutes
|
||||
t.m_Minute = (minutes % 60);
|
||||
// Return the result
|
||||
return t;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AndSeconds(Int32 seconds)
|
||||
{
|
||||
// Did we even added any seconds?
|
||||
if (!seconds)
|
||||
{
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of minutes
|
||||
Int32 minutes = static_cast< Int32 >(seconds / 60);
|
||||
// Extract the number of seconds
|
||||
seconds = m_Second + (seconds % 60);
|
||||
// Are the seconds overlapping with the next minute?
|
||||
if (seconds >= 60)
|
||||
{
|
||||
++minutes; // Increase minutes
|
||||
}
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
t.AddMinutes(minutes);
|
||||
}
|
||||
// Assign the resulted seconds
|
||||
t.m_Second = (seconds % 60);
|
||||
// Return the result
|
||||
return t;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AndMilliseconds(Int32 milliseconds)
|
||||
{
|
||||
// Did we even added any milliseconds?
|
||||
if (!milliseconds)
|
||||
{
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of seconds
|
||||
Int32 seconds = static_cast< Int32 >(milliseconds / 1000);
|
||||
// Extract the number of milliseconds
|
||||
milliseconds = m_Millisecond + (milliseconds % 1000);
|
||||
// Are the milliseconds overlapping with the next second?
|
||||
if (milliseconds >= 1000)
|
||||
{
|
||||
++seconds; // Increase seconds
|
||||
}
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
t.AddSeconds(seconds);
|
||||
}
|
||||
// Assign the resulted milliseconds
|
||||
t.m_Millisecond = (milliseconds % 1000);
|
||||
// Return the result
|
||||
return t;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp Time::GetTimestamp() const
|
||||
{
|
||||
// Calculate the microseconds in the current time
|
||||
Int64 ms = static_cast< Int64 >(m_Hour * 3600000000LL);
|
||||
ms += static_cast< Int64 >(m_Minute * 60000000L);
|
||||
ms += static_cast< Int64 >(m_Second * 1000000L);
|
||||
ms += static_cast< Int64 >(m_Millisecond * 1000L);
|
||||
// Return the resulted timestamp
|
||||
return Timestamp(ms);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(Typename::Str,
|
||||
Class< Time >(vm, Typename::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< Uint8 >()
|
||||
.Ctor< Uint8, Uint8 >()
|
||||
.Ctor< Uint8, Uint8, Uint8 >()
|
||||
.Ctor< Uint8, Uint8, Uint8, Uint16 >()
|
||||
// Static Properties
|
||||
.SetStaticValue(_SC("GlobalDelimiter"), &Time::Delimiter)
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
||||
.Func(_SC("_tostring"), &Time::ToString)
|
||||
.Func(_SC("cmp"), &Time::Cmp)
|
||||
// Meta-methods
|
||||
.Func< Time (Time::*)(const Time &) const >(_SC("_add"), &Time::operator +)
|
||||
.Func< Time (Time::*)(const Time &) const >(_SC("_sub"), &Time::operator -)
|
||||
.Func< Time (Time::*)(const Time &) const >(_SC("_mul"), &Time::operator *)
|
||||
.Func< Time (Time::*)(const Time &) const >(_SC("_div"), &Time::operator /)
|
||||
// Properties
|
||||
.Prop(_SC("Delimiter"), &Time::GetDelimiter, &Time::SetDelimiter)
|
||||
.Prop(_SC("Str"), &Time::GetStr, &Time::SetStr)
|
||||
.Prop(_SC("Hour"), &Time::GetHour, &Time::SetHour)
|
||||
.Prop(_SC("Minute"), &Time::GetMinute, &Time::SetMinute)
|
||||
.Prop(_SC("Second"), &Time::GetSecond, &Time::SetSecond)
|
||||
.Prop(_SC("Millisecond"), &Time::GetMillisecond, &Time::SetMillisecond)
|
||||
.Prop(_SC("Timestamp"), &Time::GetTimestamp)
|
||||
// Member Methods
|
||||
.Func(_SC("AddHours"), &Time::AddHours)
|
||||
.Func(_SC("AddMinutes"), &Time::AddMinutes)
|
||||
.Func(_SC("AddSeconds"), &Time::AddSeconds)
|
||||
.Func(_SC("AddMillis"), &Time::AddMilliseconds)
|
||||
.Func(_SC("AddMilliseconds"), &Time::AddMilliseconds)
|
||||
.Func(_SC("AndHours"), &Time::AndHours)
|
||||
.Func(_SC("AndMinutes"), &Time::AndMinutes)
|
||||
.Func(_SC("AndSeconds"), &Time::AndSeconds)
|
||||
.Func(_SC("AndMillis"), &Time::AndMilliseconds)
|
||||
.Func(_SC("AndMilliseconds"), &Time::AndMilliseconds)
|
||||
// Overloaded Methods
|
||||
.Overload< void (Time::*)(Uint8) >(_SC("Set"), &Time::Set)
|
||||
.Overload< void (Time::*)(Uint8, Uint8) >(_SC("Set"), &Time::Set)
|
||||
.Overload< void (Time::*)(Uint8, Uint8, Uint8) >(_SC("Set"), &Time::Set)
|
||||
.Overload< void (Time::*)(Uint8, Uint8, Uint8, Uint16) >(_SC("Set"), &Time::Set)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
362
module/Library/Chrono/Time.hpp
Normal file
362
module/Library/Chrono/Time.hpp
Normal file
@@ -0,0 +1,362 @@
|
||||
#ifndef _LIBRARY_CHRONO_TIME_HPP_
|
||||
#define _LIBRARY_CHRONO_TIME_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Helper class used to represent a certain time.
|
||||
*/
|
||||
class Time
|
||||
{
|
||||
public:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static SQChar Delimiter;
|
||||
|
||||
protected:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Compare the values of two instances.
|
||||
*/
|
||||
Int32 Compare(const Time & o) const;
|
||||
|
||||
private:
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Uint8 m_Hour; // Hour
|
||||
Uint8 m_Minute; // Minute
|
||||
Uint8 m_Second; // Second
|
||||
Uint16 m_Millisecond; // Millisecond
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQChar m_Delimiter; // Component delimiter when generating strings.
|
||||
|
||||
public:
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Default constructor.
|
||||
*/
|
||||
Time()
|
||||
: m_Hour(0)
|
||||
, m_Minute(0)
|
||||
, m_Second(0)
|
||||
, m_Millisecond(0)
|
||||
, m_Delimiter(Delimiter)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Time(Uint8 hour)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(hour, 0, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Time(Uint8 hour, Uint8 minute)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(hour, minute, 0, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Time(Uint8 hour, Uint8 minute, Uint8 second)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(hour, minute, second, 0);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Base constructor.
|
||||
*/
|
||||
Time(Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
Set(hour, minute, second, millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* String constructor.
|
||||
*/
|
||||
Time(CSStr str)
|
||||
: m_Delimiter(Delimiter)
|
||||
{
|
||||
SetStr(str);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
Int32 Cmp(const Time & o) const
|
||||
{
|
||||
return Compare(o);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* Used by the script engine to convert an instance of this type to a string.
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint8 hour)
|
||||
{
|
||||
Set(hour, m_Minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint8 hour, Uint8 minute)
|
||||
{
|
||||
Set(hour, minute, m_Second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint8 hour, Uint8 minute, Uint8 second)
|
||||
{
|
||||
Set(hour, minute, second, m_Millisecond);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Assign the specified values.
|
||||
*/
|
||||
void Set(Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the local delimiter character.
|
||||
*/
|
||||
SQChar GetDelimiter() const
|
||||
{
|
||||
return m_Delimiter;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the local delimiter character.
|
||||
*/
|
||||
void SetDelimiter(SQChar c)
|
||||
{
|
||||
m_Delimiter = c;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the values as a string.
|
||||
*/
|
||||
CSStr GetStr() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Extract the values from a string.
|
||||
*/
|
||||
void SetStr(CSStr str);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the hour component.
|
||||
*/
|
||||
Uint8 GetHour() const
|
||||
{
|
||||
return m_Hour;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the hour component.
|
||||
*/
|
||||
void SetHour(Uint8 hour);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the minute component.
|
||||
*/
|
||||
Uint8 GetMinute() const
|
||||
{
|
||||
return m_Minute;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the minute component.
|
||||
*/
|
||||
void SetMinute(Uint8 minute);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the second component.
|
||||
*/
|
||||
Uint8 GetSecond() const
|
||||
{
|
||||
return m_Second;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the second component.
|
||||
*/
|
||||
void SetSecond(Uint8 second);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Retrieve the millisecond component.
|
||||
*/
|
||||
Uint16 GetMillisecond() const
|
||||
{
|
||||
return m_Millisecond;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Modify the millisecond component.
|
||||
*/
|
||||
void SetMillisecond(Uint16 millisecond);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of hours to the current time.
|
||||
*/
|
||||
Time & AddHours(Int32 hours);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of minutes to the current time.
|
||||
*/
|
||||
Time & AddMinutes(Int32 minutes);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of seconds to the current time.
|
||||
*/
|
||||
Time & AddSeconds(Int32 seconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of milliseconds to the current time.
|
||||
*/
|
||||
Time & AddMilliseconds(Int32 milliseconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of hours to obtain a new time.
|
||||
*/
|
||||
Time AndHours(Int32 hours);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of minutes to obtain a new time.
|
||||
*/
|
||||
Time AndMinutes(Int32 minutes);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of seconds to obtain a new time.
|
||||
*/
|
||||
Time AndSeconds(Int32 seconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Add the specified amount of milliseconds to obtain a new time.
|
||||
*/
|
||||
Time AndMilliseconds(Int32 milliseconds);
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Convert this time instance to a time-stamp.
|
||||
*/
|
||||
Timestamp GetTimestamp() const;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CHRONO_TIME_HPP_
|
||||
92
module/Library/Chrono/Timer.cpp
Normal file
92
module/Library/Chrono/Timer.cpp
Normal file
@@ -0,0 +1,92 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Timer.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(Typename, _SC("SqTimer"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timer::Timer()
|
||||
: m_Timestamp(Chrono::GetCurrentSysTime())
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Timer::Cmp(const Timer & o) const
|
||||
{
|
||||
if (m_Timestamp == o.m_Timestamp)
|
||||
return 0;
|
||||
else if (m_Timestamp > o.m_Timestamp)
|
||||
return 1;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Timer::ToString() const
|
||||
{
|
||||
return ToStrF("%lld", m_Timestamp);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Timer::Reset()
|
||||
{
|
||||
m_Timestamp = Chrono::GetCurrentSysTime();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp Timer::Restart()
|
||||
{
|
||||
const Int64 now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
|
||||
m_Timestamp = now;
|
||||
return Timestamp(elapsed);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int64 Timer::RestartRaw()
|
||||
{
|
||||
const Int64 now = Chrono::GetCurrentSysTime(), elapsed = now - m_Timestamp;
|
||||
m_Timestamp = now;
|
||||
return elapsed;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp Timer::GetElapsedTime() const
|
||||
{
|
||||
return Timestamp(Chrono::GetCurrentSysTime() - m_Timestamp);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int64 Timer::GetElapsedTimeRaw() const
|
||||
{
|
||||
return (Chrono::GetCurrentSysTime() - m_Timestamp);
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoTimer(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(Typename::Str,
|
||||
Class< Timer >(vm, Typename::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< const Timer & >()
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
||||
.Func(_SC("_tostring"), &Timer::ToString)
|
||||
.Func(_SC("cmp"), &Timer::Cmp)
|
||||
// Properties
|
||||
.Prop(_SC("Elapsed"), &Timer::GetElapsedTime)
|
||||
.Prop(_SC("ElapsedRaw"), &Timer::GetElapsedTimeRaw)
|
||||
// Functions
|
||||
.Func(_SC("Reset"), &Timer::Reset)
|
||||
.Func(_SC("Restart"), &Timer::Restart)
|
||||
.Func(_SC("RestartRaw"), &Timer::RestartRaw)
|
||||
);
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
100
module/Library/Chrono/Timer.hpp
Normal file
100
module/Library/Chrono/Timer.hpp
Normal file
@@ -0,0 +1,100 @@
|
||||
#ifndef _LIBRARY_CHRONO_TIMER_HPP_
|
||||
#define _LIBRARY_CHRONO_TIMER_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
class Timer
|
||||
{
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timer(Int64 t)
|
||||
: m_Timestamp(t)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timer();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timer(const Timer & o)
|
||||
: m_Timestamp(o.m_Timestamp)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
~Timer()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timer & operator = (const Timer o)
|
||||
{
|
||||
m_Timestamp = o.m_Timestamp;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Int32 Cmp(const Timer & b) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void Reset();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp Restart();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Int64 RestartRaw();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp GetElapsedTime() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Int64 GetElapsedTimeRaw() const;
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Int64 m_Timestamp;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CHRONO_TIMER_HPP_
|
||||
180
module/Library/Chrono/Timestamp.cpp
Normal file
180
module/Library/Chrono/Timestamp.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Library/Chrono/Timer.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Numeric/LongInt.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQMODE_DECL_TYPENAME(Typename, _SC("SqTimestamp"))
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Timestamp::Timestamp(const SLongInt & t)
|
||||
: m_Timestamp(t.GetNum())
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Timestamp::Cmp(const Timestamp & o) const
|
||||
{
|
||||
if (m_Timestamp == o.m_Timestamp)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (m_Timestamp > o.m_Timestamp)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Timestamp::ToString() const
|
||||
{
|
||||
return ToStrF("%lld", m_Timestamp);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Timestamp::SetNow()
|
||||
{
|
||||
m_Timestamp = Chrono::GetCurrentSysTime();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SLongInt Timestamp::GetMicroseconds() const
|
||||
{
|
||||
return SLongInt(m_Timestamp);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Timestamp::SetMicroseconds(const SLongInt & ammount)
|
||||
{
|
||||
m_Timestamp = ammount.GetNum();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SLongInt Timestamp::GetMilliseconds() const
|
||||
{
|
||||
return SLongInt(m_Timestamp / 1000L);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
void Timestamp::SetMilliseconds(const SLongInt & ammount)
|
||||
{
|
||||
m_Timestamp = (ammount.GetNum() * 1000L);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetEpochTimeNow()
|
||||
{
|
||||
return Timestamp(Chrono::GetEpochTimeMicro());
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetMicrosecondsRaw(Int64 ammount)
|
||||
{
|
||||
return Timestamp(ammount);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetMicroseconds(const SLongInt & ammount)
|
||||
{
|
||||
return Timestamp(ammount);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetMilliseconds(SQInteger ammount)
|
||||
{
|
||||
return Timestamp(Int64(Int64(ammount) * 1000L));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetSeconds(SQFloat ammount)
|
||||
{
|
||||
return Timestamp(Int64(Float64(ammount) * 1000000L));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetMinutes(SQFloat ammount)
|
||||
{
|
||||
return Timestamp(Int64((Float64(ammount) * 60000000L)));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetHours(SQFloat ammount)
|
||||
{
|
||||
return Timestamp(Int64(Float64(ammount) * 3600000000LL));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetDays(SQFloat ammount)
|
||||
{
|
||||
return Timestamp(Int64(Float64(ammount) * 86400000000LL));
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
static Timestamp SqGetYears(SQFloat ammount)
|
||||
{
|
||||
return Timestamp(Int64(Float64(ammount) * 31557600000000LL));
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(Typename::Str,
|
||||
Class< Timestamp >(vm, Typename::Str)
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< const Timestamp & >()
|
||||
// Core Meta-methods
|
||||
.SquirrelFunc(_SC("_typename"), &Typename::Fn)
|
||||
.Func(_SC("_tostring"), &Timestamp::ToString)
|
||||
.Func(_SC("cmp"), &Timestamp::Cmp)
|
||||
// Meta-methods
|
||||
.Func< Timestamp (Timestamp::*)(const Timestamp &) const >(_SC("_add"), &Timestamp::operator +)
|
||||
.Func< Timestamp (Timestamp::*)(const Timestamp &) const >(_SC("_sub"), &Timestamp::operator -)
|
||||
.Func< Timestamp (Timestamp::*)(const Timestamp &) const >(_SC("_mul"), &Timestamp::operator *)
|
||||
.Func< Timestamp (Timestamp::*)(const Timestamp &) const >(_SC("_div"), &Timestamp::operator /)
|
||||
// Properties
|
||||
.Prop(_SC("Microseconds"), &Timestamp::GetMicroseconds, &Timestamp::SetMicroseconds)
|
||||
.Prop(_SC("MicrosecondsRaw"), &Timestamp::GetMicrosecondsRaw, &Timestamp::SetMicrosecondsRaw)
|
||||
.Prop(_SC("Milliseconds"), &Timestamp::GetMilliseconds, &Timestamp::SetMilliseconds)
|
||||
.Prop(_SC("MillisecondsRaw"), &Timestamp::GetMillisecondsRaw, &Timestamp::SetMillisecondsRaw)
|
||||
.Prop(_SC("SecondsF"), &Timestamp::GetSecondsF, &Timestamp::SetSecondsF)
|
||||
.Prop(_SC("SecondsI"), &Timestamp::GetSecondsI, &Timestamp::SetSecondsI)
|
||||
.Prop(_SC("MinutesF"), &Timestamp::GetMinutesF, &Timestamp::SetMinutesF)
|
||||
.Prop(_SC("MinutesI"), &Timestamp::GetMinutesI, &Timestamp::SetMinutesI)
|
||||
.Prop(_SC("HoursF"), &Timestamp::GetHoursF, &Timestamp::SetHoursF)
|
||||
.Prop(_SC("HoursI"), &Timestamp::GetHoursI, &Timestamp::SetHoursI)
|
||||
.Prop(_SC("DaysF"), &Timestamp::GetDaysF, &Timestamp::SetDaysF)
|
||||
.Prop(_SC("DaysI"), &Timestamp::GetDaysI, &Timestamp::SetDaysI)
|
||||
.Prop(_SC("YearsF"), &Timestamp::GetYearsF, &Timestamp::SetYearsF)
|
||||
.Prop(_SC("YearsI"), &Timestamp::GetYearsI, &Timestamp::SetYearsI)
|
||||
// Member Methods
|
||||
.Func(_SC("SetNow"), &Timestamp::SetNow)
|
||||
// Static Functions
|
||||
.StaticFunc(_SC("GetNow"), &SqGetEpochTimeNow)
|
||||
.StaticFunc(_SC("GetMicrosRaw"), &SqGetMicrosecondsRaw)
|
||||
.StaticFunc(_SC("GetMicrosecondsRaw"), &SqGetMicrosecondsRaw)
|
||||
.StaticFunc(_SC("GetMicros"), &SqGetMicroseconds)
|
||||
.StaticFunc(_SC("GetMicroseconds"), &SqGetMicroseconds)
|
||||
.StaticFunc(_SC("GetMillis"), &SqGetMilliseconds)
|
||||
.StaticFunc(_SC("GetMilliseconds"), &SqGetMilliseconds)
|
||||
.StaticFunc(_SC("GetSeconds"), &SqGetSeconds)
|
||||
.StaticFunc(_SC("GetMinutes"), &SqGetMinutes)
|
||||
.StaticFunc(_SC("GetHours"), &SqGetHours)
|
||||
.StaticFunc(_SC("GetDays"), &SqGetDays)
|
||||
.StaticFunc(_SC("GetYears"), &SqGetYears)
|
||||
);
|
||||
;
|
||||
}
|
||||
|
||||
} // Namespace:: SqMod
|
||||
347
module/Library/Chrono/Timestamp.hpp
Normal file
347
module/Library/Chrono/Timestamp.hpp
Normal file
@@ -0,0 +1,347 @@
|
||||
#ifndef _LIBRARY_CHRONO_TIMESTAMP_HPP_
|
||||
#define _LIBRARY_CHRONO_TIMESTAMP_HPP_
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
class Timer;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
class Timestamp
|
||||
{
|
||||
// --------------------------------------------------------------------------------------------
|
||||
friend class Timer;
|
||||
|
||||
public:
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp()
|
||||
: m_Timestamp(0)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp(Int64 t)
|
||||
: m_Timestamp(t)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
explicit Timestamp(const SLongInt & t);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp(const Timestamp & o)
|
||||
: m_Timestamp(o.m_Timestamp)
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
~Timestamp()
|
||||
{
|
||||
/* ... */
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Timestamp & operator = (const Timestamp o)
|
||||
{
|
||||
m_Timestamp = o.m_Timestamp;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Timestamp operator + (const Timestamp & o) const
|
||||
{
|
||||
return Timestamp(m_Timestamp + o.m_Timestamp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Timestamp operator - (const Timestamp & o) const
|
||||
{
|
||||
return Timestamp(m_Timestamp - o.m_Timestamp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Timestamp operator * (const Timestamp & o) const
|
||||
{
|
||||
return Timestamp(m_Timestamp * o.m_Timestamp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Timestamp operator / (const Timestamp & o) const
|
||||
{
|
||||
return Timestamp(m_Timestamp / o.m_Timestamp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
Int32 Cmp(const Timestamp & b) const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
CSStr ToString() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* ...
|
||||
*/
|
||||
void SetNow();
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
Int64 GetNum() const
|
||||
{
|
||||
return m_Timestamp;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SLongInt GetMicroseconds() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMicroseconds(const SLongInt & ammount);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetMicrosecondsRaw() const
|
||||
{
|
||||
return SQInteger(m_Timestamp);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMicrosecondsRaw(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = ammount;
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SLongInt GetMilliseconds() const;
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMilliseconds(const SLongInt & ammount);
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetMillisecondsRaw() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 1000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMillisecondsRaw(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Int64(ammount) * 1000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQFloat GetSecondsF() const
|
||||
{
|
||||
return SQFloat(m_Timestamp / 1000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetSecondsF(SQFloat ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 1000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetSecondsI() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 1000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetSecondsI(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Int64(ammount) * 1000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQFloat GetMinutesF() const
|
||||
{
|
||||
return SQFloat(m_Timestamp / 60000000.0f);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMinutesF(SQFloat ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 60000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetMinutesI() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 60000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetMinutesI(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Int64(ammount) * 60000000L);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQFloat GetHoursF() const
|
||||
{
|
||||
return SQFloat(m_Timestamp / 3600000000.0d);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetHoursF(SQFloat ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 3600000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetHoursI() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 3600000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetHoursI(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 3600000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQFloat GetDaysF() const
|
||||
{
|
||||
return SQFloat(m_Timestamp / 86400000000.0d);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetDaysF(SQFloat ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 86400000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetDaysI() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 86400000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetDaysI(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 86400000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQFloat GetYearsF() const
|
||||
{
|
||||
return SQFloat(m_Timestamp / 31557600000000.0d);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetYearsF(SQFloat ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 31557600000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
SQInteger GetYearsI() const
|
||||
{
|
||||
return SQInteger(m_Timestamp / 31557600000000LL);
|
||||
}
|
||||
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
void SetYearsI(SQInteger ammount)
|
||||
{
|
||||
m_Timestamp = Int64(Float64(ammount) * 31557600000000LL);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// --------------------------------------------------------------------------------------------
|
||||
Int64 m_Timestamp;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
||||
#endif // _LIBRARY_CHRONO_TIMESTAMP_HPP_
|
||||
Reference in New Issue
Block a user