mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2026-08-03 06:17:10 +02:00
Implemented and improved more types in the Chrono library.
This commit is contained in:
+243
-164
@@ -13,103 +13,11 @@ SQChar Time::Delimiter = ':';
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
SQInteger Time::Typename(HSQUIRRELVM vm)
|
||||
{
|
||||
static const SQChar name[] = _SC("SqChronoTime");
|
||||
static const SQChar name[] = _SC("SqTime");
|
||||
sq_pushstring(vm, name, sizeof(name));
|
||||
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);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
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;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
CSStr Time::GetStr() const
|
||||
{
|
||||
return ToString();
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Int32 Time::Compare(const Time & o) const
|
||||
{
|
||||
@@ -151,14 +59,99 @@ Int32 Time::Compare(const Time & o) const
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
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_Delimiter, m_Hour
|
||||
, m_Delimiter, m_Minute
|
||||
, m_Delimiter, m_Second
|
||||
, m_Delimiter, m_Millisecond
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -211,129 +204,210 @@ void Time::SetMillisecond(Uint16 millisecond)
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AddHours(Int32 hours)
|
||||
Time & Time::AddHours(Int32 hours)
|
||||
{
|
||||
// Replicate the current time
|
||||
Time tm(*this);
|
||||
// Did we even added any hours?
|
||||
if (!hours)
|
||||
// Did we even add any hours?
|
||||
if (hours)
|
||||
{
|
||||
return tm; // Return the time as is
|
||||
// Add the specified amount of hours
|
||||
m_Hour += (hours % 24);
|
||||
// Make sure the value is within range
|
||||
m_Hour %= 24;
|
||||
}
|
||||
// Add the specified amount of hours
|
||||
tm.m_Hour = ((hours % 24) + m_Hour);
|
||||
// Make sure the value is within range
|
||||
tm.m_Hour %= 24;
|
||||
// Return the result
|
||||
return tm;
|
||||
// Allow chaining operations
|
||||
return *this;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AddMinutes(Int32 minutes)
|
||||
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)
|
||||
{
|
||||
// Replicate the current time
|
||||
Time tm(*this);
|
||||
// Did we even added any minutes?
|
||||
if (!minutes)
|
||||
{
|
||||
return tm; // Return the time as is
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of hours
|
||||
Int32 hours = minutes / 60;
|
||||
Int32 hours = static_cast< Int32 >(minutes / 60);
|
||||
// Extract the number of minutes
|
||||
minutes = (minutes % 60) + m_Minute;
|
||||
// Make sure the value is within range
|
||||
minutes = m_Minute + (minutes % 60);
|
||||
// Are the minutes overlapping with the next hour?
|
||||
if (minutes >= 60)
|
||||
{
|
||||
// Increase hours
|
||||
++hours;
|
||||
// Subtract from minutes
|
||||
minutes %= 60;
|
||||
++hours; // Increase hours
|
||||
}
|
||||
// Do we have any hours?
|
||||
if (hours > 0)
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any hours?
|
||||
if (hours)
|
||||
{
|
||||
// Add the hours
|
||||
tm = tm.AddHours(hours);
|
||||
t.AddHours(hours);
|
||||
}
|
||||
// Assign the resulted minutes
|
||||
tm.m_Minute = minutes;
|
||||
t.m_Minute = (minutes % 60);
|
||||
// Return the result
|
||||
return tm;
|
||||
return t;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AddSeconds(Int32 seconds)
|
||||
Time Time::AndSeconds(Int32 seconds)
|
||||
{
|
||||
// Replicate the current time
|
||||
Time tm(*this);
|
||||
// Did we even added any seconds?
|
||||
if (!seconds)
|
||||
{
|
||||
return tm; // Return the time as is
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of minutes
|
||||
Int32 minutes = seconds / 60;
|
||||
Int32 minutes = static_cast< Int32 >(seconds / 60);
|
||||
// Extract the number of seconds
|
||||
seconds = (seconds % 60) + m_Second;
|
||||
// Make sure the value is within range
|
||||
seconds = m_Second + (seconds % 60);
|
||||
// Are the seconds overlapping with the next minute?
|
||||
if (seconds >= 60)
|
||||
{
|
||||
// Increase minutes
|
||||
++minutes;
|
||||
// Subtract from seconds
|
||||
seconds %= 60;
|
||||
++minutes; // Increase minutes
|
||||
}
|
||||
// Do we have any minutes?
|
||||
if (minutes > 0)
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any minutes?
|
||||
if (minutes)
|
||||
{
|
||||
// Add the minutes
|
||||
tm = tm.AddMinutes(minutes);
|
||||
t.AddMinutes(minutes);
|
||||
}
|
||||
// Assign the resulted seconds
|
||||
tm.m_Second = seconds;
|
||||
t.m_Second = (seconds % 60);
|
||||
// Return the result
|
||||
return tm;
|
||||
return t;
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
Time Time::AddMilliseconds(Int32 milliseconds)
|
||||
Time Time::AndMilliseconds(Int32 milliseconds)
|
||||
{
|
||||
// Replicate the current time
|
||||
Time tm(*this);
|
||||
// Did we even added any milliseconds?
|
||||
if (!milliseconds)
|
||||
{
|
||||
return tm; // Return the time as is
|
||||
return Time(*this); // Return the time as is
|
||||
}
|
||||
// Extract the number of seconds
|
||||
Int32 seconds = milliseconds / 1000;
|
||||
Int32 seconds = static_cast< Int32 >(milliseconds / 1000);
|
||||
// Extract the number of milliseconds
|
||||
milliseconds = (milliseconds % 1000) + m_Millisecond;
|
||||
// Make sure the value is within range
|
||||
milliseconds = m_Millisecond + (milliseconds % 1000);
|
||||
// Are the milliseconds overlapping with the next second?
|
||||
if (milliseconds >= 1000)
|
||||
{
|
||||
// Increase seconds
|
||||
++seconds;
|
||||
// Subtract from milliseconds
|
||||
milliseconds %= 1000;
|
||||
++seconds; // Increase seconds
|
||||
}
|
||||
// Do we have any seconds?
|
||||
if (seconds > 0)
|
||||
// Replicate the current time
|
||||
Time t(*this);
|
||||
// Should we add any seconds?
|
||||
if (seconds)
|
||||
{
|
||||
// Add the seconds
|
||||
tm = tm.AddSeconds(seconds);
|
||||
t.AddSeconds(seconds);
|
||||
}
|
||||
// Assign the resulted milliseconds
|
||||
tm.m_Millisecond = milliseconds;
|
||||
t.m_Millisecond = (milliseconds % 1000);
|
||||
// Return the result
|
||||
return tm;
|
||||
return t;
|
||||
}
|
||||
|
||||
// ================================================================================================
|
||||
void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(_SC("SqTime"), Class< Time >(vm, _SC("SqChronoTime"))
|
||||
RootTable(vm).Bind(_SC("SqTime"), Class< Time >(vm, _SC("SqTime"))
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< Uint8 >()
|
||||
@@ -352,18 +426,23 @@ void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
.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("Str"), &Time::GetStr, &Time::SetStr)
|
||||
.Prop(_SC("Delimiter"), &Date::GetDelimiter, &Date::SetDelimiter)
|
||||
// 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)
|
||||
|
||||
Reference in New Issue
Block a user