1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 08:47:17 +01:00

Added a custom delimiter for time and date and also the option to extract values from strings.

This commit is contained in:
Sandu Liviu Catalin 2016-03-26 18:18:41 +02:00
parent b5215b38e3
commit a4cf2dae85
7 changed files with 387 additions and 18 deletions

View File

@ -1,12 +1,18 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar Date::Delimiter = '-';
// ------------------------------------------------------------------------------------------------
const Uint8 Date::MonthLengths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// ------------------------------------------------------------------------------------------------
SQInteger Date::Typename(HSQUIRRELVM vm)
{
@ -39,10 +45,221 @@ Date Date::operator / (const Date & o) const
return Date(o);
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & cns)
// ------------------------------------------------------------------------------------------------
void Date::Set(Uint16 year, Uint8 month, Uint8 day)
{
if (!ValidDate(year, month, day))
{
STHROWF("Invalid date: %04u%c%02u%c%02u%c%u"
, m_Delimiter, m_Year
, m_Delimiter, m_Month
, m_Delimiter, m_Day
);
}
}
// ------------------------------------------------------------------------------------------------
CSStr Date::GetStr() const
{
return ToString();
}
// ------------------------------------------------------------------------------------------------
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)
);
}
// ------------------------------------------------------------------------------------------------
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;
}
// ------------------------------------------------------------------------------------------------
CSStr Date::ToString() const
{
return ToStrF("%04u%c%02u%c%02u%c%u"
, m_Delimiter, m_Year
, m_Delimiter, m_Month
, m_Delimiter, m_Day
);
}
// ------------------------------------------------------------------------------------------------
void Date::SetYear(Uint16 year)
{
}
// ------------------------------------------------------------------------------------------------
void Date::SetMonth(Uint8 month)
{
}
// ------------------------------------------------------------------------------------------------
void Date::SetDay(Uint8 day)
{
}
// ------------------------------------------------------------------------------------------------
Date Date::AddYears(Int32 years)
{
return Date(*this);
}
// ------------------------------------------------------------------------------------------------
Date Date::AddMonths(Int32 months)
{
return Date(*this);
}
// ------------------------------------------------------------------------------------------------
Date Date::AddDays(Int32 days)
{
return Date(*this);
}
// ------------------------------------------------------------------------------------------------
bool Date::ValidDate(Uint16 year, Uint8 month, Uint8 day)
{
// Is this a valid date?
if (year == 0 || month == 0 || day == 0)
{
return false;
}
// Is the month within range?
else if (month > 12)
{
return false;
}
// Return whether the day inside the month
return day <= DaysInMonth(year, month);
}
// ------------------------------------------------------------------------------------------------
Uint8 Date::DaysInMonth(Uint16 year, Uint8 month)
{
// Is the specified month within range?
if (month > 12)
{
STHROWF("Month value is out of range: %u > 12", month);
}
// Obtain the days in this month
Uint8 days = MonthLengths[month - 1];
// Should we account for January?
if (month == 2 && IsLeapYear(year))
{
++days;
}
// Return the resulted days
return days;
}
// ------------------------------------------------------------------------------------------------
Uint16 Date::DayOfYear(Uint16 year, Uint8 month, Uint8 day)
{
// Start with 0 days
Uint16 doy = 0;
// Cumulate the days in months
for (Uint8 m = 1; m < month; ++month)
{
doy += DaysInMonth(year, m);
}
// Add the specified days
doy += day;
// Return the result
return doy;
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(_SC("SqDate"), Class< Date >(vm, _SC("SqChronoDate"))
// Constructors
.Ctor()
.Ctor< Uint16 >()
.Ctor< Uint16, Uint8 >()
.Ctor< Uint16, Uint8, Uint8 >()
// Static Properties
.SetStaticValue(_SC("GlobalDelimiter"), &Date::Delimiter)
// Core Metamethods
.Func(_SC("_tostring"), &Date::ToString)
.SquirrelFunc(_SC("_typename"), &Date::Typename)
.Func(_SC("_cmp"), &Date::Cmp)
// Metamethods
.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("Year"), &Date::GetYear, &Date::SetYear)
.Prop(_SC("Month"), &Date::GetMonth, &Date::SetMonth)
.Prop(_SC("Day"), &Date::GetDay, &Date::SetDay)
.Prop(_SC("Str"), &Date::GetStr, &Date::SetStr)
.Prop(_SC("Delimiter"), &Date::GetDelimiter, &Date::SetDelimiter)
.Prop(_SC("LeapYear"), &Date::IsThisLeapYear)
.Prop(_SC("YearDays"), &Date::GetYearDays)
.Prop(_SC("MonthDays"), &Date::GetMonthDays)
// Member Methods
.Func(_SC("AddYears"), &Date::AddYears)
.Func(_SC("AddMonths"), &Date::AddMonths)
.Func(_SC("AddDays"), &Date::AddDays)
// 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

View File

@ -14,6 +14,12 @@ class Date
{
public:
// ------------------------------------------------------------------------------------------------
static SQChar Delimiter;
// ------------------------------------------------------------------------------------------------
static const Uint8 MonthLengths[12];
/* ------------------------------------------------------------------------------------------------
* Default constructor.
*/
@ -21,6 +27,7 @@ public:
: m_Year(1970)
, m_Month(1)
, m_Day(1)
, m_Delimiter(Delimiter)
{
/* ... */
}
@ -29,6 +36,7 @@ public:
* Base constructor.
*/
Date(Uint16 year)
: m_Delimiter(Delimiter)
{
Set(year, 1, 1);
}
@ -37,6 +45,7 @@ public:
* Base constructor.
*/
Date(Uint16 year, Uint8 month)
: m_Delimiter(Delimiter)
{
Set(year, month, 1);
}
@ -45,6 +54,7 @@ public:
* Base constructor.
*/
Date(Uint16 year, Uint8 month, Uint8 day)
: m_Delimiter(Delimiter)
{
Set(year, month, day);
}
@ -53,6 +63,7 @@ public:
* String constructor.
*/
Date(CSStr str)
: m_Delimiter(Delimiter)
{
SetStr(str);
}
@ -168,6 +179,22 @@ public:
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* Retrieve the local delimiter character.
*/
SQChar GetDelimiter() const
{
return m_Delimiter;
}
/* ------------------------------------------------------------------------------------------------
* Modify the local delimiter character.
*/
void SetDelimiter(SQChar c)
{
m_Delimiter = c;
}
/* ------------------------------------------------------------------------------------------------
* Assign the specified values.
*/
@ -253,6 +280,30 @@ public:
*/
Date AddDays(Int32 days);
/* ------------------------------------------------------------------------------------------------
* See whether the associated year is a leap year.
*/
bool IsThisLeapYear() const
{
return IsLeapYear(m_Year);
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the number of days in the associated year.
*/
Uint16 GetYearDays() const
{
return DaysInYear(m_Year);
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the number of days in the associated month.
*/
Uint8 GetMonthDays() const
{
return DaysInMonth(m_Year, m_Month);
}
protected:
/* ------------------------------------------------------------------------------------------------
@ -266,6 +317,42 @@ private:
Uint16 m_Year; // Year
Uint8 m_Month; // Month
Uint8 m_Day; // Day
// ------------------------------------------------------------------------------------------------
SQChar m_Delimiter; // Component delimiter when generating strings.
public:
/* ------------------------------------------------------------------------------------------------
* See whether the specified year is a leap year.
*/
static bool IsLeapYear(Uint16 year)
{
return !(year % 400) || (!(year % 4) && (year % 100));
}
/* ------------------------------------------------------------------------------------------------
* See whether the specified date is valid.
*/
static bool ValidDate(Uint16 year, Uint8 month, Uint8 day);
/* ------------------------------------------------------------------------------------------------
* retrieve the number of days in the specified year.
*/
static Uint16 DaysInYear(Uint16 year)
{
return IsLeapYear(year) ? 366 : 365;
}
/* ------------------------------------------------------------------------------------------------
* Retrieve the number of days in the specified month.
*/
static Uint8 DaysInMonth(Uint16 year, Uint8 month);
/* ------------------------------------------------------------------------------------------------
* Retrieve the number/position of the specified day in the specified year and month.
*/
static Uint16 DayOfYear(Uint16 year, Uint8 month, Uint8 day);
};
} // Namespace:: SqMod

View File

@ -10,7 +10,7 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------
// ================================================================================================
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & cns)
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
{
}

View File

@ -7,6 +7,9 @@
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQChar Time::Delimiter = ':';
// ------------------------------------------------------------------------------------------------
SQInteger Time::Typename(HSQUIRRELVM vm)
{
@ -76,9 +79,35 @@ CSStr Time::GetStr() const
}
// ------------------------------------------------------------------------------------------------
void Time::SetStr(CSStr /*str*/)
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)
);
}
// ------------------------------------------------------------------------------------------------
@ -125,7 +154,12 @@ Int32 Time::Compare(const Time & o) const
// ------------------------------------------------------------------------------------------------
CSStr Time::ToString() const
{
return ToStrF("%02u:%02u:%02u:%u", m_Hour, m_Minute, m_Second, m_Millisecond);
return ToStrF("%02u%c%02u%c%02u%c%u"
, m_Delimiter, m_Hour
, m_Delimiter, m_Minute
, m_Delimiter, m_Second
, m_Delimiter, m_Millisecond
);
}
// ------------------------------------------------------------------------------------------------
@ -297,7 +331,7 @@ Time Time::AddMilliseconds(Int32 milliseconds)
}
// ================================================================================================
void Register_ChronoTime(HSQUIRRELVM vm, Table & cns)
void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(_SC("SqTime"), Class< Time >(vm, _SC("SqChronoTime"))
// Constructors
@ -306,6 +340,8 @@ void Register_ChronoTime(HSQUIRRELVM vm, Table & cns)
.Ctor< Uint8, Uint8 >()
.Ctor< Uint8, Uint8, Uint8 >()
.Ctor< Uint8, Uint8, Uint8, Uint16 >()
// Static Properties
.SetStaticValue(_SC("GlobalDelimiter"), &Time::Delimiter)
// Core Metamethods
.Func(_SC("_tostring"), &Time::ToString)
.SquirrelFunc(_SC("_typename"), &Time::Typename)
@ -321,6 +357,7 @@ void Register_ChronoTime(HSQUIRRELVM vm, Table & cns)
.Prop(_SC("Second"), &Time::GetSecond, &Time::SetSecond)
.Prop(_SC("Millisecond"), &Time::GetMillisecond, &Time::SetMillisecond)
.Prop(_SC("Str"), &Time::GetStr, &Time::SetStr)
.Prop(_SC("Delimiter"), &Date::GetDelimiter, &Date::SetDelimiter)
// Member Methods
.Func(_SC("AddHours"), &Time::AddHours)
.Func(_SC("AddMinutes"), &Time::AddMinutes)

View File

@ -14,6 +14,9 @@ class Time
{
public:
// ------------------------------------------------------------------------------------------------
static SQChar Delimiter;
/* ------------------------------------------------------------------------------------------------
* Default constructor.
*/
@ -22,6 +25,7 @@ public:
, m_Minute(0)
, m_Second(0)
, m_Millisecond(0)
, m_Delimiter(Delimiter)
{
/* ... */
}
@ -30,6 +34,7 @@ public:
* Base constructor.
*/
Time(Uint8 hour)
: m_Delimiter(Delimiter)
{
Set(hour, 0, 0, 0);
}
@ -38,6 +43,7 @@ public:
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute)
: m_Delimiter(Delimiter)
{
Set(hour, minute, 0, 0);
}
@ -46,6 +52,7 @@ public:
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute, Uint8 second)
: m_Delimiter(Delimiter)
{
Set(hour, minute, second, 0);
}
@ -54,6 +61,7 @@ public:
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
: m_Delimiter(Delimiter)
{
Set(hour, minute, second, millisecond);
}
@ -62,6 +70,7 @@ public:
* String constructor.
*/
Time(CSStr str)
: m_Delimiter(Delimiter)
{
SetStr(str);
}
@ -177,6 +186,22 @@ public:
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* Retrieve the local delimiter character.
*/
SQChar GetDelimiter() const
{
return m_Delimiter;
}
/* ------------------------------------------------------------------------------------------------
* Modify the local delimiter character.
*/
void SetDelimiter(SQChar c)
{
m_Delimiter = c;
}
/* ------------------------------------------------------------------------------------------------
* Assign the specified values.
*/
@ -302,6 +327,9 @@ private:
Uint8 m_Minute; // Minute
Uint8 m_Second; // Second
Uint16 m_Millisecond; // Millisecond
// ------------------------------------------------------------------------------------------------
SQChar m_Delimiter; // Component selimiter when generating strings.
};
} // Namespace:: SqMod

View File

@ -66,7 +66,7 @@ Int64 Timer::GetElapsedTimeRaw() const
}
// ================================================================================================
void Register_ChronoTimer(HSQUIRRELVM vm, Table & cns)
void Register_ChronoTimer(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(_SC("SqTimer"), Class< Timer >(vm, _SC("SqChronoTimer"))
// Constructors

View File

@ -117,7 +117,7 @@ static Timestamp SqGetYears(SQFloat ammount)
}
// ================================================================================================
void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & cns)
void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & /*cns*/)
{
RootTable(vm).Bind(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqChronoTimestamp"))
// Constructors