
#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
