Adding Spectre/Math/Time
This commit is contained in:
parent
4d69ff3a18
commit
6de6028ba4
3 changed files with 153 additions and 1 deletions
101
source/Math/Time.cpp
Normal file
101
source/Math/Time.cpp
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
|
||||
#include <cmath>
|
||||
#include <Spectre/Math/Time.h>
|
||||
|
||||
namespace sp {
|
||||
|
||||
Time::Time(long value) :
|
||||
m_value(value)
|
||||
{
|
||||
}
|
||||
|
||||
double Time::seconds() const
|
||||
{
|
||||
return ((double) m_value) / 1000000.0f;
|
||||
}
|
||||
|
||||
int Time::milliseconds() const
|
||||
{
|
||||
return ((double) m_value) / 1000;
|
||||
}
|
||||
|
||||
long Time::microseconds() const
|
||||
{
|
||||
return m_value;
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// Helper construct functions
|
||||
// ----------------------------
|
||||
Time Time::seconds(double value)
|
||||
{
|
||||
return Time((long) std::round(value * 1000000.0f));
|
||||
}
|
||||
|
||||
Time Time::milliseconds(int value)
|
||||
{
|
||||
return Time((long) (value * 1000));
|
||||
}
|
||||
|
||||
Time Time::microseconds(long value)
|
||||
{
|
||||
return Time(value);
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// Compare
|
||||
// ----------------------------
|
||||
bool operator ==(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() == b.microseconds();
|
||||
}
|
||||
|
||||
bool operator !=(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() != b.microseconds();
|
||||
}
|
||||
|
||||
bool operator <(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() < b.microseconds();
|
||||
}
|
||||
|
||||
bool operator <=(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() <= b.microseconds();
|
||||
}
|
||||
|
||||
bool operator >(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() > b.microseconds();
|
||||
}
|
||||
|
||||
bool operator >=(const Time& a, const Time& b)
|
||||
{
|
||||
return a.microseconds() >= b.microseconds();
|
||||
}
|
||||
|
||||
// ----------------------------
|
||||
// Arithmetic
|
||||
// ----------------------------
|
||||
Time operator +(const Time& a, const Time& b)
|
||||
{
|
||||
return Time(a.microseconds() + b.microseconds());
|
||||
}
|
||||
|
||||
Time& operator +=(Time& a, const Time& b)
|
||||
{
|
||||
return a = a + b;
|
||||
}
|
||||
|
||||
Time operator -(const Time& a, const Time& b)
|
||||
{
|
||||
return Time(a.microseconds() - b.microseconds());
|
||||
}
|
||||
|
||||
Time& operator -=(Time& a, const Time& b)
|
||||
{
|
||||
return a = a - b;
|
||||
}
|
||||
|
||||
} // namespace sp
|
||||
Loading…
Add table
Add a link
Reference in a new issue