126 lines
3.9 KiB
C++
126 lines
3.9 KiB
C++
/* -----------------------------------------------------------------------------
|
|
GSFramework
|
|
Copyright 2001-2013 Emmanuel Julien. All Rights Reserved.
|
|
----------------------------------------------------------------------------- */
|
|
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
#include "time/ntime.h"
|
|
|
|
using namespace GS;
|
|
|
|
Time Time::Inf(std::numeric_limits <int> ::max());
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
void Time::operator += (const Time &b)
|
|
{
|
|
sec += b.sec;
|
|
nsec += b.nsec;
|
|
Normalize();
|
|
}
|
|
void Time::operator -= (const Time &b)
|
|
{
|
|
sec -= b.sec;
|
|
nsec -= b.nsec;
|
|
Normalize();
|
|
}
|
|
Time Time::operator + (const Time &b) const
|
|
{ return Time(sec + b.sec, nsec + b.nsec); }
|
|
Time Time::operator - (const Time &b) const
|
|
{ return Time(sec - b.sec, nsec - b.nsec); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
bool Time::operator == (const Time &b) const
|
|
{ return (sec == b.sec) && (nsec == b.nsec); }
|
|
bool Time::operator != (const Time &b) const
|
|
{ return (sec != b.sec) || (nsec != b.nsec); }
|
|
bool Time::operator > (const Time &b) const
|
|
{ return (sec > b.sec) || ((sec == b.sec) && (nsec > b.nsec)); }
|
|
bool Time::operator < (const Time &b) const
|
|
{ return (sec < b.sec) || ((sec == b.sec) && (nsec < b.nsec)); }
|
|
bool Time::operator >= (const Time &b) const
|
|
{ return (*this > b) || (*this == b); }
|
|
bool Time::operator <= (const Time &b) const
|
|
{ return (*this < b) || (*this == b); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
float Time::toDay() const
|
|
{ return toHour() / 24.f; }
|
|
float Time::toHour() const
|
|
{ return toMin() / 60.f; }
|
|
float Time::toMin() const
|
|
{ return toSec() / 60.f; }
|
|
float Time::toSec() const
|
|
{ return sec + nsec / 1000000000.f; }
|
|
float Time::toMs() const
|
|
{ return sec * 1000.f + nsec / 1000000.f; }
|
|
float Time::toNs() const
|
|
{ return sec * 1000000000.f + nsec; }
|
|
String Time::toString() const
|
|
{ return String::Format("%02d:%02d:%02d:%03d", int(toHour()) % 60, int(toMin()) % 60, int(toSec()) % 60, int(toMs()) % 1000); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
void Time::setSec(int s)
|
|
{ sec = s; nsec = 0; }
|
|
void Time::setSec(float s)
|
|
{
|
|
double integral, fractional = modf(s, &integral);
|
|
sec = int(integral); nsec = int(fractional * 1000000000.0);
|
|
Normalize();
|
|
}
|
|
void Time::setMs(int m)
|
|
{
|
|
sec = m / 1000; nsec = (m - sec * 1000) * 1000000;
|
|
}
|
|
void Time::setNs(int n)
|
|
{
|
|
sec = 0; nsec = n;
|
|
}
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
Time Time::fromSec(float s)
|
|
{
|
|
double integral, fractional = modf(s, &integral);
|
|
return Time(int(integral), int(fractional * 1000000000.0));
|
|
}
|
|
Time Time::fromSec(int s)
|
|
{ return Time(s); }
|
|
Time Time::fromMs(int m)
|
|
{ return Time(0, m * 1000000); }
|
|
Time Time::fromNs(int n)
|
|
{ return Time(0, n); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
Time Time::Abs() const
|
|
{ return Time(Types::Abs(sec), nsec); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
//------------------------------------------------------------------------------
|
|
void Time::Normalize()
|
|
{
|
|
for (; nsec >= 1000000000; nsec -= 1000000000)
|
|
sec++;
|
|
for (; nsec < 0; nsec += 1000000000)
|
|
sec--;
|
|
}
|
|
Time Time::Normalized() const
|
|
{ return Time(sec, nsec); }
|
|
//------------------------------------------------------------------------------
|
|
|
|
Time::Time(int s, int n)
|
|
{
|
|
sec = s; nsec = n;
|
|
Normalize();
|
|
}
|
|
Time::Time(float s)
|
|
{ setSec(s); }
|
|
Time::Time(int s)
|
|
{ sec = s; nsec = 0; }
|