mirror of
https://github.com/VCMP-SqMod/SqMod.git
synced 2025-06-15 22:57:12 +02:00
Furher implementation and improvement of the Chrono types and also exposed them to the module API.
Tighten the safety of exported functions to avoid exceptions leaking outside the host plugin.
This commit is contained in:
@ -1,7 +1,8 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -355,6 +356,20 @@ Date Date::AndDays(Int32 days)
|
||||
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*/)
|
||||
{
|
||||
@ -385,6 +400,7 @@ void Register_ChronoDate(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
.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)
|
||||
|
@ -351,6 +351,10 @@ public:
|
||||
return Chrono::DaysInMonth(m_Year, m_Month);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* Convert this date instance to a time-stamp.
|
||||
*/
|
||||
Timestamp GetTimestamp() const;
|
||||
};
|
||||
|
||||
} // Namespace:: SqMod
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -716,6 +717,39 @@ Datetime Datetime::AndMilliseconds(Int32 milliseconds)
|
||||
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*/)
|
||||
{
|
||||
@ -758,6 +792,9 @@ void Register_ChronoDatetime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
.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)
|
||||
|
@ -564,6 +564,20 @@ public:
|
||||
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
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include "Library/Chrono/Time.hpp"
|
||||
#include "Library/Chrono/Date.hpp"
|
||||
#include "Library/Chrono/Datetime.hpp"
|
||||
#include "Library/Chrono/Timestamp.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -404,6 +405,18 @@ Time Time::AndMilliseconds(Int32 milliseconds)
|
||||
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*/)
|
||||
{
|
||||
@ -432,6 +445,7 @@ void Register_ChronoTime(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
.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)
|
||||
|
@ -355,6 +355,11 @@ public:
|
||||
* 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
|
||||
|
@ -7,9 +7,6 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
class Timestamp;
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
*
|
||||
*/
|
||||
|
@ -1,8 +1,10 @@
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
#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.hpp"
|
||||
#include "Base/Shared.hpp"
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
namespace SqMod {
|
||||
@ -18,11 +20,17 @@ Timestamp::Timestamp(const SLongInt & t)
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
@ -118,7 +126,7 @@ static Timestamp SqGetYears(SQFloat ammount)
|
||||
// ================================================================================================
|
||||
void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & /*cns*/)
|
||||
{
|
||||
RootTable(vm).Bind(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqChronoTimestamp"))
|
||||
RootTable(vm).Bind(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqTimestamp"))
|
||||
// Constructors
|
||||
.Ctor()
|
||||
.Ctor< const Timestamp & >()
|
||||
|
Reference in New Issue
Block a user