Issue #256: added TimeStamp class
parent
124ea77612
commit
a4343bfa18
@ -0,0 +1,108 @@
|
||||
|
||||
#include "timestamp.hpp"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
TimeStamp::TimeStamp (float hour, int day)
|
||||
: mHour (hour), mDay (day)
|
||||
{
|
||||
if (hour<0 || hour>=24 || day<0)
|
||||
throw std::runtime_error ("invalid time stamp");
|
||||
}
|
||||
|
||||
float TimeStamp::getHour() const
|
||||
{
|
||||
return mHour;
|
||||
}
|
||||
|
||||
int TimeStamp::getDay() const
|
||||
{
|
||||
return mDay;
|
||||
}
|
||||
|
||||
TimeStamp& TimeStamp::operator+= (double hours)
|
||||
{
|
||||
if (hours<0)
|
||||
throw std::runtime_error ("can't move time stamp backwards in time");
|
||||
|
||||
hours += mHour;
|
||||
|
||||
mHour = static_cast<float> (std::fmod (hours, 24));
|
||||
|
||||
mDay += hours / 24;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator== (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
return left.getHour()==right.getHour() && left.getDay()==right.getDay();
|
||||
}
|
||||
|
||||
bool operator!= (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
return !(left==right);
|
||||
}
|
||||
|
||||
bool operator< (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
if (left.getDay()<right.getDay())
|
||||
return true;
|
||||
|
||||
if (left.getDay()>right.getDay())
|
||||
return false;
|
||||
|
||||
return left.getHour()<right.getHour();
|
||||
}
|
||||
|
||||
bool operator<= (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
return left<right || left==right;
|
||||
}
|
||||
|
||||
bool operator> (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
return !(left<=right);
|
||||
}
|
||||
|
||||
bool operator>= (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
return !(left<right);
|
||||
}
|
||||
|
||||
TimeStamp operator+ (const TimeStamp& stamp, double hours)
|
||||
{
|
||||
return TimeStamp (stamp) + hours;
|
||||
}
|
||||
|
||||
TimeStamp operator+ (double hours, const TimeStamp& stamp)
|
||||
{
|
||||
return TimeStamp (stamp) + hours;
|
||||
}
|
||||
|
||||
double operator- (const TimeStamp& left, const TimeStamp& right)
|
||||
{
|
||||
if (left<right)
|
||||
return -(right-left);
|
||||
|
||||
int days = left.getDay() - right.getDay();
|
||||
|
||||
double hours = 0;
|
||||
|
||||
if (left.getHour()<right.getHour())
|
||||
{
|
||||
hours = 24-right.getHour()+left.getHour();
|
||||
++days;
|
||||
}
|
||||
else
|
||||
{
|
||||
hours = left.getHour()-right.getHour();
|
||||
}
|
||||
|
||||
return hours + 24*days;
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#ifndef GAME_MWWORLD_TIMESTAMP_H
|
||||
#define GAME_MWWORLD_TIMESTAMP_H
|
||||
|
||||
namespace MWWorld
|
||||
{
|
||||
/// \brief In-game time stamp
|
||||
///
|
||||
/// This class is based on the global variables GameHour and DaysPassed.
|
||||
class TimeStamp
|
||||
{
|
||||
float mHour;
|
||||
int mDay;
|
||||
|
||||
public:
|
||||
|
||||
explicit TimeStamp (float hour = 0, int day = 0);
|
||||
///< \oaram hour [0, 23)
|
||||
/// \param day >=0
|
||||
|
||||
float getHour() const;
|
||||
|
||||
int getDay() const;
|
||||
|
||||
TimeStamp& operator+= (double hours);
|
||||
///< \param hours >=0
|
||||
};
|
||||
|
||||
bool operator== (const TimeStamp& left, const TimeStamp& right);
|
||||
bool operator!= (const TimeStamp& left, const TimeStamp& right);
|
||||
|
||||
bool operator< (const TimeStamp& left, const TimeStamp& right);
|
||||
bool operator<= (const TimeStamp& left, const TimeStamp& right);
|
||||
|
||||
bool operator> (const TimeStamp& left, const TimeStamp& right);
|
||||
bool operator>= (const TimeStamp& left, const TimeStamp& right);
|
||||
|
||||
TimeStamp operator+ (const TimeStamp& stamp, double hours);
|
||||
TimeStamp operator+ (double hours, const TimeStamp& stamp);
|
||||
|
||||
double operator- (const TimeStamp& left, const TimeStamp& right);
|
||||
///< Returns the difference between \a left and \a right in in-game hours.
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue