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

Merge the time utilities under the chrono library.

This commit is contained in:
Sandu Liviu Catalin 2016-03-25 14:28:07 +02:00
parent f3af787bad
commit e167a59f83
17 changed files with 1602 additions and 513 deletions

View File

@ -441,10 +441,20 @@
<Unit filename="../source/Entity/Vehicle.cpp" /> <Unit filename="../source/Entity/Vehicle.cpp" />
<Unit filename="../source/Entity/Vehicle.hpp" /> <Unit filename="../source/Entity/Vehicle.hpp" />
<Unit filename="../source/Exports.cpp" /> <Unit filename="../source/Exports.cpp" />
<Unit filename="../source/Library/Chrono.cpp" />
<Unit filename="../source/Library/Chrono.hpp" />
<Unit filename="../source/Library/Chrono/Date.cpp" />
<Unit filename="../source/Library/Chrono/Date.hpp" />
<Unit filename="../source/Library/Chrono/Datetime.cpp" />
<Unit filename="../source/Library/Chrono/Datetime.hpp" />
<Unit filename="../source/Library/Chrono/Time.cpp" />
<Unit filename="../source/Library/Chrono/Time.hpp" />
<Unit filename="../source/Library/Chrono/Timer.cpp" />
<Unit filename="../source/Library/Chrono/Timer.hpp" />
<Unit filename="../source/Library/Chrono/Timestamp.cpp" />
<Unit filename="../source/Library/Chrono/Timestamp.hpp" />
<Unit filename="../source/Library/Crypt.cpp" /> <Unit filename="../source/Library/Crypt.cpp" />
<Unit filename="../source/Library/Crypt.hpp" /> <Unit filename="../source/Library/Crypt.hpp" />
<Unit filename="../source/Library/Datetime.cpp" />
<Unit filename="../source/Library/Datetime.hpp" />
<Unit filename="../source/Library/FileIO.cpp" /> <Unit filename="../source/Library/FileIO.cpp" />
<Unit filename="../source/Library/FileIO.hpp" /> <Unit filename="../source/Library/FileIO.hpp" />
<Unit filename="../source/Library/Format.cpp" /> <Unit filename="../source/Library/Format.cpp" />
@ -463,8 +473,6 @@
<Unit filename="../source/Library/SysEnv.hpp" /> <Unit filename="../source/Library/SysEnv.hpp" />
<Unit filename="../source/Library/SysPath.cpp" /> <Unit filename="../source/Library/SysPath.cpp" />
<Unit filename="../source/Library/SysPath.hpp" /> <Unit filename="../source/Library/SysPath.hpp" />
<Unit filename="../source/Library/Time.cpp" />
<Unit filename="../source/Library/Time.hpp" />
<Unit filename="../source/Library/Utils.cpp" /> <Unit filename="../source/Library/Utils.cpp" />
<Unit filename="../source/Library/Utils.hpp" /> <Unit filename="../source/Library/Utils.hpp" />
<Unit filename="../source/Logger.cpp" /> <Unit filename="../source/Logger.cpp" />

197
source/Library/Chrono.cpp Normal file
View File

@ -0,0 +1,197 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Numeric.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <windows.h>
#else
#include <time.h>
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
/* ------------------------------------------------------------------------------------------------
* Used by GetCurrentSysTime to obtain the system frequency on initial call.
*/
LARGE_INTEGER GetFrequency()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency;
}
// ------------------------------------------------------------------------------------------------
Int64 GetCurrentSysTime()
{
// Force the following code to run on first core
// (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx)
HANDLE current_thread = GetCurrentThread();
DWORD_PTR previous_mask = SetThreadAffinityMask(current_thread, 1);
// Get the frequency of the performance counter
// (it is constant across the program lifetime)
static const LARGE_INTEGER frequency = GetFrequency();
// Get the current time
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
// Restore the thread affinity
SetThreadAffinityMask(current_thread, previous_mask);
// Return the current time as microseconds
return Int64(1000000LL * time.QuadPart / frequency.QuadPart);
}
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMicro()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
// Extract the nanoseconds from the resulted timestamp
Uint64 time = ft.dwHighDateTime;
time <<= 32;
time |= ft.dwLowDateTime;
time /= 10;
time -= 11644473600000000ULL;
// Return the resulted timestamp
return Int64(time);
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTickCount()
{
return GetTickCount();
}
// ------------------------------------------------------------------------------------------------
Int64 GetTickCount64()
{
#ifdef _SQ64
return GetTickCount64();
#else
return 0; // Should we fallback to 32 bit?
#endif // _SQ64
}
#else
// ------------------------------------------------------------------------------------------------
Int64 GetCurrentSysTime()
{
// POSIX implementation
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Int64(Uint64(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
}
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMicro()
{
// POSIX implementation
timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return Int64(Uint64(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTickCount()
{
// POSIX implementation
struct timespec time;
if (clock_gettime(CLOCK_MONOTONIC, &time))
{
return 0;
}
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
// ------------------------------------------------------------------------------------------------
Int64 GetTickCount64()
{
struct timespec time;
if (clock_gettime(CLOCK_MONOTONIC, &time))
{
return 0;
}
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMilli()
{
return (GetEpochTimeMicro() / 1000L);
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetEpochTimeMicro()
{
return SLongInt(GetEpochTimeMicro());
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetEpochTimeMilli()
{
return SLongInt(GetEpochTimeMilli());
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetCurrentSysTime()
{
return SLongInt(GetCurrentSysTime());
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqGetTickCount()
{
return GetTickCount();
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetTickCount64()
{
return SLongInt(GetTickCount64());
}
// ------------------------------------------------------------------------------------------------
extern void Register_ChronoDate(HSQUIRRELVM vm, Table & cns);
extern void Register_ChronoDatetime(HSQUIRRELVM vm, Table & cns);
extern void Register_ChronoTime(HSQUIRRELVM vm, Table & cns);
extern void Register_ChronoTimer(HSQUIRRELVM vm, Table & cns);
extern void Register_ChronoTimestamp(HSQUIRRELVM vm, Table & cns);
// ------------------------------------------------------------------------------------------------
void Register_Chrono(HSQUIRRELVM vm)
{
Table cns(vm);
Register_ChronoDate(vm, cns);
Register_ChronoDatetime(vm, cns);
Register_ChronoTime(vm, cns);
Register_ChronoTimer(vm, cns);
Register_ChronoTimestamp(vm, cns);
cns
.Func(_SC("EpochMicro"), &SqGetEpochTimeMicro)
.Func(_SC("EpochMilli"), &SqGetEpochTimeMilli)
.Func(_SC("Current"), &SqGetCurrentSysTime)
.Func(_SC("TickCount"), &SqGetTickCount)
.Func(_SC("TickCount64"), &SqGetTickCount64);
RootTable(vm).Bind(_SC("SqChrono"), cns);
}
} // Namespace:: SqMod

27
source/Library/Chrono.hpp Normal file
View File

@ -0,0 +1,27 @@
#ifndef _LIBRARY_CHRONO_HPP_
#define _LIBRARY_CHRONO_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Retrieve the current time as microseconds.
*/
Int64 GetCurrentSysTime();
/* ------------------------------------------------------------------------------------------------
* Retrieve the epoch time as microseconds.
*/
Int64 GetEpochTimeMicro();
/* ------------------------------------------------------------------------------------------------
* Retrieve the epoch time as milliseconds.
*/
Int64 GetEpochTimeMilli();
} // Namespace:: SqMod
#endif // _LIBRARY_CHRONO_HPP_

View File

@ -0,0 +1,48 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger Date::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqChronoDate");
sq_pushstring(vm, name, sizeof(name));
return 1;
}
// ------------------------------------------------------------------------------------------------
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);
}
// ------------------------------------------------------------------------------------------------
Date Date::operator / (const Date & o) const
{
return Date(o);
}
// ================================================================================================
void Register_ChronoDate(HSQUIRRELVM vm, Table & cns)
{
}
} // Namespace:: SqMod

View File

@ -0,0 +1,273 @@
#ifndef _LIBRARY_CHRONO_DATE_HPP_
#define _LIBRARY_CHRONO_DATE_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class used to represent a certain date.
*/
class Date
{
public:
/* ------------------------------------------------------------------------------------------------
* Default constructor.
*/
Date()
: m_Year(1970)
, m_Month(1)
, m_Day(1)
{
/* ... */
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Date(Uint16 year)
{
Set(year, 1, 1);
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Date(Uint16 year, Uint8 month)
{
Set(year, month, 1);
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Date(Uint16 year, Uint8 month, Uint8 day)
{
Set(year, month, day);
}
/* ------------------------------------------------------------------------------------------------
* String constructor.
*/
Date(CSStr str)
{
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;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* 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 values as a string.
*/
CSStr GetStr() const;
/* ------------------------------------------------------------------------------------------------
* Extract the values from a string.
*/
void SetStr(CSStr str);
/* ------------------------------------------------------------------------------------------------
* 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.
*/
Date AddYears(Int32 years);
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of months.
*/
Date AddMonths(Int32 months);
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of days.
*/
Date AddDays(Int32 days);
protected:
/* ------------------------------------------------------------------------------------------------
* Compare the values of two instances.
*/
Int32 Compare(const Date & o) const;
private:
// ------------------------------------------------------------------------------------------------
Uint16 m_Year; // Year
Uint8 m_Month; // Month
Uint8 m_Day; // Day
};
} // Namespace:: SqMod
#endif // _LIBRARY_CHRONO_DATE_HPP_

View File

@ -0,0 +1,18 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Datetime.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Time.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
// ================================================================================================
void Register_ChronoDatetime(HSQUIRRELVM vm, Table & cns)
{
}
} // Namespace:: SqMod

View File

@ -0,0 +1,16 @@
#ifndef _LIBRARY_CHRONO_DATETIME_HPP_
#define _LIBRARY_CHRONO_DATETIME_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
*
*/
} // Namespace:: SqMod
#endif // _LIBRARY_CHRONO_DATETIME_HPP_

View File

@ -0,0 +1,338 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Time.hpp"
#include "Library/Chrono/Date.hpp"
#include "Library/Chrono/Datetime.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
SQInteger Time::Typename(HSQUIRRELVM vm)
{
static SQChar name[] = _SC("SqChronoTime");
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*/)
{
}
// ------------------------------------------------------------------------------------------------
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;
}
}
// ------------------------------------------------------------------------------------------------
CSStr Time::ToString() const
{
return ToStrF("%02u:%02u:%02u:%u", m_Hour, m_Minute, m_Second, m_Millisecond);
}
// ------------------------------------------------------------------------------------------------
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)
{
// Replicate the current time
Time tm(*this);
// Did we even added any hours?
if (!hours)
{
return tm; // Return the time as is
}
// 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;
}
// ------------------------------------------------------------------------------------------------
Time Time::AddMinutes(Int32 minutes)
{
// Replicate the current time
Time tm(*this);
// Did we even added any minutes?
if (!minutes)
{
return tm; // Return the time as is
}
// Extract the number of hours
Int32 hours = minutes / 60;
// Extract the number of minutes
minutes = (minutes % 60) + m_Minute;
// Make sure the value is within range
if (minutes >= 60)
{
// Increase hours
++hours;
// Subtract from minutes
minutes %= 60;
}
// Do we have any hours?
if (hours > 0)
{
// Add the hours
tm = tm.AddHours(hours);
}
// Assign the resulted minutes
tm.m_Minute = minutes;
// Return the result
return tm;
}
// ------------------------------------------------------------------------------------------------
Time Time::AddSeconds(Int32 seconds)
{
// Replicate the current time
Time tm(*this);
// Did we even added any seconds?
if (!seconds)
{
return tm; // Return the time as is
}
// Extract the number of minutes
Int32 minutes = seconds / 60;
// Extract the number of seconds
seconds = (seconds % 60) + m_Second;
// Make sure the value is within range
if (seconds >= 60)
{
// Increase minutes
++minutes;
// Subtract from seconds
seconds %= 60;
}
// Do we have any minutes?
if (minutes > 0)
{
// Add the minutes
tm = tm.AddMinutes(minutes);
}
// Assign the resulted seconds
tm.m_Second = seconds;
// Return the result
return tm;
}
// ------------------------------------------------------------------------------------------------
Time Time::AddMilliseconds(Int32 milliseconds)
{
// Replicate the current time
Time tm(*this);
// Did we even added any milliseconds?
if (!milliseconds)
{
return tm; // Return the time as is
}
// Extract the number of seconds
Int32 seconds = milliseconds / 1000;
// Extract the number of milliseconds
milliseconds = (milliseconds % 1000) + m_Millisecond;
// Make sure the value is within range
if (milliseconds >= 1000)
{
// Increase seconds
++seconds;
// Subtract from milliseconds
milliseconds %= 1000;
}
// Do we have any seconds?
if (seconds > 0)
{
// Add the seconds
tm = tm.AddSeconds(seconds);
}
// Assign the resulted milliseconds
tm.m_Millisecond = milliseconds;
// Return the result
return tm;
}
// ================================================================================================
void Register_ChronoTime(HSQUIRRELVM vm, Table & cns)
{
RootTable(vm).Bind(_SC("SqTime"), Class< Time >(vm, _SC("SqChronoTime"))
// Constructors
.Ctor()
.Ctor< Uint8 >()
.Ctor< Uint8, Uint8 >()
.Ctor< Uint8, Uint8, Uint8 >()
.Ctor< Uint8, Uint8, Uint8, Uint16 >()
// Core Metamethods
.Func(_SC("_tostring"), &Time::ToString)
.SquirrelFunc(_SC("_typename"), &Time::Typename)
.Func(_SC("_cmp"), &Time::Cmp)
// Metamethods
.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("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)
// 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)
// 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

View File

@ -0,0 +1,309 @@
#ifndef _LIBRARY_CHRONO_TIME_HPP_
#define _LIBRARY_CHRONO_TIME_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
/* ------------------------------------------------------------------------------------------------
* Helper class used to represent a certain time.
*/
class Time
{
public:
/* ------------------------------------------------------------------------------------------------
* Default constructor.
*/
Time()
: m_Hour(0)
, m_Minute(0)
, m_Second(0)
, m_Millisecond(0)
{
/* ... */
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Time(Uint8 hour)
{
Set(hour, 0, 0, 0);
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute)
{
Set(hour, minute, 0, 0);
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute, Uint8 second)
{
Set(hour, minute, second, 0);
}
/* ------------------------------------------------------------------------------------------------
* Base constructor.
*/
Time(Uint8 hour, Uint8 minute, Uint8 second, Uint16 millisecond)
{
Set(hour, minute, second, millisecond);
}
/* ------------------------------------------------------------------------------------------------
* String constructor.
*/
Time(CSStr str)
{
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;
/* --------------------------------------------------------------------------------------------
* Used by the script engine to retrieve the name from instances of this type.
*/
static SQInteger Typename(HSQUIRRELVM vm);
/* ------------------------------------------------------------------------------------------------
* 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 values as a string.
*/
CSStr GetStr() const;
/* ------------------------------------------------------------------------------------------------
* 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.
*/
Time AddHours(Int32 hours);
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of minutes.
*/
Time AddMinutes(Int32 minutes);
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of seconds.
*/
Time AddSeconds(Int32 seconds);
/* ------------------------------------------------------------------------------------------------
* Add the specified amount of milliseconds.
*/
Time AddMilliseconds(Int32 milliseconds);
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
};
} // Namespace:: SqMod
#endif // _LIBRARY_CHRONO_TIME_HPP_

View File

@ -0,0 +1,88 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Chrono.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
Timer::Timer()
: m_Timestamp(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 = GetCurrentSysTime();
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::Restart()
{
const Int64 now = GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return Timestamp(elapsed);
}
// ------------------------------------------------------------------------------------------------
Int64 Timer::RestartRaw()
{
const Int64 now = GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return elapsed;
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::GetElapsedTime() const
{
return Timestamp(GetCurrentSysTime() - m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
Int64 Timer::GetElapsedTimeRaw() const
{
return (GetCurrentSysTime() - m_Timestamp);
}
// ================================================================================================
void Register_ChronoTimer(HSQUIRRELVM vm, Table & cns)
{
RootTable(vm).Bind(_SC("SqTimer"), Class< Timer >(vm, _SC("SqChronoTimer"))
// Constructors
.Ctor()
.Ctor< const Timer & >()
// Core Metamethods
.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

View File

@ -0,0 +1,103 @@
#ifndef _LIBRARY_CHRONO_TIMER_HPP_
#define _LIBRARY_CHRONO_TIMER_HPP_
// ------------------------------------------------------------------------------------------------
#include "SqBase.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
class Timestamp;
/* ------------------------------------------------------------------------------------------------
*
*/
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_

View File

@ -0,0 +1,168 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Chrono/Timestamp.hpp"
#include "Library/Chrono/Timer.hpp"
#include "Library/Chrono.hpp"
#include "Library/Numeric.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
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 = 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(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(_SC("SqTimestamp"), Class< Timestamp >(vm, _SC("SqChronoTimestamp"))
// Constructors
.Ctor()
.Ctor< const Timestamp & >()
// Core Metamethods
.Func(_SC("_tostring"), &Timestamp::ToString)
.Func(_SC("_cmp"), &Timestamp::Cmp)
// Metamethods
.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

View File

@ -1,5 +1,5 @@
#ifndef _LIBRARY_TIME_HPP_ #ifndef _LIBRARY_CHRONO_TIMESTAMP_HPP_
#define _LIBRARY_TIME_HPP_ #define _LIBRARY_CHRONO_TIMESTAMP_HPP_
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
#include "SqBase.hpp" #include "SqBase.hpp"
@ -10,21 +10,6 @@ namespace SqMod {
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
class Timer; class Timer;
/* ------------------------------------------------------------------------------------------------
* Retrieve the current time as microseconds.
*/
Int64 GetCurrentSysTime();
/* ------------------------------------------------------------------------------------------------
*
*/
Int64 GetEpochTimeMicro();
/* ------------------------------------------------------------------------------------------------
*
*/
Int64 GetEpochTimeMilli();
/* ------------------------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------------------------
* *
*/ */
@ -357,94 +342,6 @@ private:
Int64 m_Timestamp; Int64 m_Timestamp;
}; };
/* ------------------------------------------------------------------------------------------------
*
*/
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 } // Namespace:: SqMod
#endif // _LIBRARY_TIME_HPP_ #endif // _LIBRARY_CHRONO_TIMESTAMP_HPP_

View File

@ -1,401 +0,0 @@
// ------------------------------------------------------------------------------------------------
#include "Library/Time.hpp"
#include "Library/Numeric.hpp"
#include "Base/Shared.hpp"
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
#include <windows.h>
#else
#include <time.h>
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
namespace SqMod {
// ------------------------------------------------------------------------------------------------
#ifdef SQMOD_OS_WINDOWS
/* ------------------------------------------------------------------------------------------------
* Used by GetCurrentSysTime to obtain the system frequency on initial call.
*/
LARGE_INTEGER GetFrequency()
{
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
return frequency;
}
// ------------------------------------------------------------------------------------------------
Int64 GetCurrentSysTime()
{
// Force the following code to run on first core
// (see http://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx)
HANDLE current_thread = GetCurrentThread();
DWORD_PTR previous_mask = SetThreadAffinityMask(current_thread, 1);
// Get the frequency of the performance counter
// (it is constant across the program lifetime)
static const LARGE_INTEGER frequency = GetFrequency();
// Get the current time
LARGE_INTEGER time;
QueryPerformanceCounter(&time);
// Restore the thread affinity
SetThreadAffinityMask(current_thread, previous_mask);
// Return the current time as microseconds
return Int64(1000000LL * time.QuadPart / frequency.QuadPart);
}
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMicro()
{
FILETIME ft;
GetSystemTimeAsFileTime(&ft);
// Extract the nanoseconds from the resulted timestamp
Uint64 time = ft.dwHighDateTime;
time <<= 32;
time |= ft.dwLowDateTime;
time /= 10;
time -= 11644473600000000ULL;
// Return the resulted timestamp
return Int64(time);
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTickCount()
{
return GetTickCount();
}
// ------------------------------------------------------------------------------------------------
Int64 GetTickCount64()
{
#ifdef _SQ64
return GetTickCount64();
#else
return 0;
#endif // _SQ64
}
#else
// ------------------------------------------------------------------------------------------------
Int64 GetCurrentSysTime()
{
// POSIX implementation
timespec time;
clock_gettime(CLOCK_MONOTONIC, &time);
return Int64(Uint64(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
}
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMicro()
{
// POSIX implementation
timespec time;
clock_gettime(CLOCK_REALTIME, &time);
return Int64(Uint64(time.tv_sec) * 1000000 + time.tv_nsec / 1000);
}
// ------------------------------------------------------------------------------------------------
Uint32 GetTickCount()
{
// POSIX implementation
struct timespec time;
if (clock_gettime(CLOCK_MONOTONIC, &time))
{
return 0;
}
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
// ------------------------------------------------------------------------------------------------
Int64 GetTickCount64()
{
struct timespec time;
if (clock_gettime(CLOCK_MONOTONIC, &time))
{
return 0;
}
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
#endif // SQMOD_OS_WINDOWS
// ------------------------------------------------------------------------------------------------
Int64 GetEpochTimeMilli()
{
return (GetEpochTimeMicro() / 1000L);
}
// ------------------------------------------------------------------------------------------------
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 = 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);
}
// ------------------------------------------------------------------------------------------------
Timer::Timer()
: m_Timestamp(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 = GetCurrentSysTime();
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::Restart()
{
const Int64 now = GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return Timestamp(elapsed);
}
// ------------------------------------------------------------------------------------------------
Int64 Timer::RestartRaw()
{
const Int64 now = GetCurrentSysTime(), elapsed = now - m_Timestamp;
m_Timestamp = now;
return elapsed;
}
// ------------------------------------------------------------------------------------------------
Timestamp Timer::GetElapsedTime() const
{
return Timestamp(GetCurrentSysTime() - m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
Int64 Timer::GetElapsedTimeRaw() const
{
return (GetCurrentSysTime() - m_Timestamp);
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetEpochTimeMicro()
{
return SLongInt(GetEpochTimeMicro());
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetEpochTimeMilli()
{
return SLongInt(GetEpochTimeMilli());
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetCurrentSysTime()
{
return SLongInt(GetCurrentSysTime());
}
// ------------------------------------------------------------------------------------------------
static Timestamp SqGetEpochTimeNow()
{
return Timestamp(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));
}
// ------------------------------------------------------------------------------------------------
static SQInteger SqGetTickCount()
{
return GetTickCount();
}
// ------------------------------------------------------------------------------------------------
static SLongInt SqGetTickCount64()
{
return SLongInt(GetTickCount64());
}
// ================================================================================================
void Register_Time(HSQUIRRELVM vm)
{
Table timens(vm);
timens.Bind(_SC("Stamp"), Class< Timestamp >(vm, _SC("SqTimestamp"))
/* Constructors */
.Ctor()
.Ctor< const Timestamp & >()
/* Core Metamethods */
.Func(_SC("_tostring"), &Timestamp::ToString)
.Func(_SC("_cmp"), &Timestamp::Cmp)
/* Metamethods */
.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)
/* Functions */
.Func(_SC("SetNow"), &Timestamp::SetNow)
);
timens.Bind(_SC("Timer"), Class< Timer >(vm, _SC("SqTimer"))
/* Constructors */
.Ctor()
.Ctor< const Timer & >()
/* Core Metamethods */
.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)
);
timens
.Func(_SC("EpochMicro"), &SqGetEpochTimeMicro)
.Func(_SC("EpochMilli"), &SqGetEpochTimeMilli)
.Func(_SC("Current"), &SqGetCurrentSysTime)
.Func(_SC("Now"), &SqGetEpochTimeNow)
.Func(_SC("MicrosecondsRaw"), &SqGetMicrosecondsRaw)
.Func(_SC("Microseconds"), &SqGetMicroseconds)
.Func(_SC("Milliseconds"), &SqGetMilliseconds)
.Func(_SC("Seconds"), &SqGetSeconds)
.Func(_SC("Minutes"), &SqGetMinutes)
.Func(_SC("Hours"), &SqGetHours)
.Func(_SC("Days"), &SqGetDays)
.Func(_SC("Years"), &SqGetYears)
.Func(_SC("TickCount"), &SqGetTickCount)
.Func(_SC("TickCount64"), &SqGetTickCount64);
RootTable(vm).Bind(_SC("SqTime"), timens);
}
} // Namespace:: SqMod

View File

@ -34,13 +34,13 @@ extern void Register_CVehicle(HSQUIRRELVM vm);
extern void Register_Entity(HSQUIRRELVM vm); extern void Register_Entity(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
extern void Register_Chrono(HSQUIRRELVM vm);
extern void Register_Crypt(HSQUIRRELVM vm); extern void Register_Crypt(HSQUIRRELVM vm);
extern void Register_Numeric(HSQUIRRELVM vm); extern void Register_Numeric(HSQUIRRELVM vm);
extern void Register_Random(HSQUIRRELVM vm); extern void Register_Random(HSQUIRRELVM vm);
extern void Register_String(HSQUIRRELVM vm); extern void Register_String(HSQUIRRELVM vm);
extern void Register_SysEnv(HSQUIRRELVM vm); extern void Register_SysEnv(HSQUIRRELVM vm);
extern void Register_SysPath(HSQUIRRELVM vm); extern void Register_SysPath(HSQUIRRELVM vm);
extern void Register_Time(HSQUIRRELVM vm);
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
extern void Register_Constants(HSQUIRRELVM vm); extern void Register_Constants(HSQUIRRELVM vm);
@ -78,13 +78,13 @@ bool RegisterAPI(HSQUIRRELVM vm)
Register_CTextdraw(vm); Register_CTextdraw(vm);
Register_CVehicle(vm); Register_CVehicle(vm);
Register_Chrono(vm);
Register_Crypt(vm); Register_Crypt(vm);
Register_Random(vm); Register_Random(vm);
Register_Numeric(vm); Register_Numeric(vm);
Register_String(vm); Register_String(vm);
Register_SysEnv(vm); Register_SysEnv(vm);
Register_SysPath(vm); Register_SysPath(vm);
Register_Time(vm);
Register_Constants(vm); Register_Constants(vm);
Register_Log(vm); Register_Log(vm);