1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2025-06-15 22:57:12 +02:00

Implemented and improved more types in the Chrono library.

This commit is contained in:
Sandu Liviu Catalin
2016-06-04 19:17:42 +03:00
parent ba6acb0d19
commit 0093bc2dec
14 changed files with 1994 additions and 523 deletions

View File

@ -10,9 +10,6 @@ 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)
{
@ -21,79 +18,6 @@ SQInteger Date::Typename(HSQUIRRELVM vm)
return 1;
}
// ------------------------------------------------------------------------------------------------
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);
}
// ------------------------------------------------------------------------------------------------
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
{
@ -125,13 +49,76 @@ Int32 Date::Compare(const Date & o) const
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%c%u"
, m_Delimiter, m_Year
, m_Delimiter, m_Month
, m_Delimiter, m_Day
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)
);
}
@ -139,7 +126,7 @@ CSStr Date::ToString() const
void Date::SetDayOfYear(Uint16 doy)
{
// Reverse the given day of year to a full date
Date d = ReverseDayOfyear(m_Year, doy);
Date d = Chrono::ReverseDayOfyear(m_Year, doy);
// Set the obtained month
SetMonth(d.m_Month);
// Set the obtained day
@ -157,7 +144,7 @@ void Date::SetYear(Uint16 year)
// Assign the value
m_Year = year;
// Make sure the new date is valid
if (!ValidDate(m_Year, m_Month, m_Day))
if (!Chrono::ValidDate(m_Year, m_Month, m_Day))
{
m_Month = 1;
m_Day = 1;
@ -175,7 +162,7 @@ void Date::SetMonth(Uint8 month)
// Assign the value
m_Month = month;
// Make sure the month days are in range
if (m_Day > DaysInMonth(m_Year, m_Month))
if (m_Day > Chrono::DaysInMonth(m_Year, m_Month))
{
m_Month = 1; // Fall back to the beginning of the month
}
@ -185,7 +172,7 @@ void Date::SetMonth(Uint8 month)
void Date::SetDay(Uint8 day)
{
// Grab the amount of days in the current month
const Uint8 dim = DaysInMonth(m_Year, m_Month);
const Uint8 dim = Chrono::DaysInMonth(m_Year, m_Month);
// Make sure the day is valid
if (day == 0)
{
@ -200,7 +187,7 @@ void Date::SetDay(Uint8 day)
}
// ------------------------------------------------------------------------------------------------
void Date::AddYears(Int32 years)
Date & Date::AddYears(Int32 years)
{
// Do we have a valid amount of years?
if (years)
@ -208,27 +195,33 @@ void Date::AddYears(Int32 years)
// Add the specified amount of years
SetYear(ConvTo< Uint16 >::From(static_cast< Int32 >(m_Year) + years));
}
// Allow chaining operations
return *this;
}
// ------------------------------------------------------------------------------------------------
void Date::AddMonths(Int32 months)
Date & Date::AddMonths(Int32 months)
{
// Do we have a valid amount of months?
if (!months)
if (months)
{
// Calculate the the years, if any
// Extract the number of years
Int32 years = static_cast< Int32 >(months / 12);
// Calculate the months, if any
// 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?
@ -239,20 +232,22 @@ void Date::AddMonths(Int32 months)
// Add the months
SetMonth(months);
}
// Allow chaining operations
return *this;
}
// ------------------------------------------------------------------------------------------------
void Date::AddDays(Int32 days)
Date & Date::AddDays(Int32 days)
{
// Do we have a valid amount of days?
if (!days)
if (days)
{
// Should we go in a positive or negative direction?
Int32 dir = days > 0 ? 1 : -1;
// 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 = DaysInYear(year);
Int32 diy = Chrono::DaysInYear(year);
// Calculate the day of year
Int32 doy = GetDayOfYear() + days;
// Calculate the resulting years
@ -260,13 +255,15 @@ void Date::AddDays(Int32 days)
{
doy -= diy * dir;
year += dir;
diy = DaysInYear(year);
diy = Chrono::DaysInYear(year);
}
// Set the obtained year
SetYear(year);
// Set the obtained day of year
SetDayOfYear(doy);
}
// Allow chaining operations
return *this;
}
// ------------------------------------------------------------------------------------------------
@ -275,7 +272,7 @@ Date Date::AndYears(Int32 years)
// Do we have a valid amount of years?
if (!years)
{
return Date(*this);
return Date(*this); // Return the date as is
}
// Replicate the current date
Date d(*this);
@ -291,21 +288,25 @@ Date Date::AndMonths(Int32 months)
// Do we have a valid amount of months?
if (!months)
{
return Date(*this);
return Date(*this); // Return the date as is
}
// Calculate the the years, if any
// Extract the number of years
Int32 years = static_cast< Int32 >(months / 12);
// Calculate the months, if any
// 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
@ -327,14 +328,14 @@ Date Date::AndDays(Int32 days)
// Do we have a valid amount of days?
if (!days)
{
return Date(*this);
return Date(*this); // Return the date as is
}
// Should we go in a positive or negative direction?
Int32 dir = days > 0 ? 1 : -1;
// 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 = DaysInYear(year);
Int32 diy = Chrono::DaysInYear(year);
// Calculate the day of year
Int32 doy = GetDayOfYear() + days;
// Calculate the resulting years
@ -342,7 +343,7 @@ Date Date::AndDays(Int32 days)
{
doy -= diy * dir;
year += dir;
diy = DaysInYear(year);
diy = Chrono::DaysInYear(year);
}
// Replicate the current date
Date d(*this);
@ -354,80 +355,6 @@ Date Date::AndDays(Int32 days)
return d;
}
// ------------------------------------------------------------------------------------------------
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);
// 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;
}
// ------------------------------------------------------------------------------------------------
Date Date::ReverseDayOfyear(Uint16 year, Uint16 doy)
{
// The resulted month
Uint8 month = 1;
// Calculate the months till the specified day of year
for (; month < 12; ++month)
{
// Get the number of days in the current month
Uint32 days = DaysInMonth(year, month);
// Can this month fit in the remaining days?
if (days >= doy)
{
break; // The search is complete
}
// Subtract the month days from days of year
doy -= days;
}
// Return the resulted date
return Date(year, month, doy);
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
{
@ -450,8 +377,8 @@ void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
.Func< Date (Date::*)(const Date &) const >(_SC("_div"), &Date::operator /)
// Properties
.Prop(_SC("Delimiter"), &Date::GetDelimiter, &Date::SetDelimiter)
.Prop(_SC("DayOfYear"), &Date::GetDayOfYear, &Date::SetDayOfYear)
.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)
@ -469,13 +396,6 @@ void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
.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)
// Static Functions
.StaticFunc(_SC("IsLeapYear"), &Date::IsLeapYear)
.StaticFunc(_SC("IsValidDate"), &Date::ValidDate)
.StaticFunc(_SC("GetDaysInYear"), &Date::DaysInYear)
.StaticFunc(_SC("GetDaysInMonth"), &Date::DaysInMonth)
.StaticFunc(_SC("GetDayOfYear"), &Date::DayOfYear)
.StaticFunc(_SC("GetReverseDayOfyear"), &Date::ReverseDayOfyear)
);
}