1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-28 14:53:58 +00:00
OpenMW/apps/openmw/mwworld/timestamp.cpp

126 lines
2.7 KiB
C++
Raw Normal View History

2012-05-18 22:04:22 +02:00
#include "timestamp.hpp"
#include <cmath>
#include <stdexcept>
#include <string>
2012-05-18 22:04:22 +02:00
2014-05-12 21:04:02 +02:00
#include <components/esm/defs.hpp>
#include "duration.hpp"
2012-05-18 22:04:22 +02:00
namespace MWWorld
{
2022-09-22 21:26:05 +03:00
TimeStamp::TimeStamp(float hour, int day)
: mHour(hour)
, mDay(day)
2012-05-18 22:04:22 +02:00
{
if (hour < 0 || hour >= 24)
throw std::runtime_error("invalid time stamp hour: " + std::to_string(hour));
2012-05-18 22:04:22 +02:00
}
float TimeStamp::getHour() const
{
return mHour;
}
int TimeStamp::getDay() const
{
return mDay;
}
2022-09-22 21:26:05 +03:00
TimeStamp& TimeStamp::operator+=(double hours)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
if (hours < 0)
throw std::runtime_error("can't move time stamp backwards in time");
2012-05-18 22:04:22 +02:00
const Duration duration = Duration::fromHours(mHour + hours);
2012-05-18 22:04:22 +02:00
mHour = duration.getHours();
mDay += duration.getDays();
2012-05-18 22:04:22 +02:00
return *this;
}
2022-09-22 21:26:05 +03:00
bool operator==(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return left.getHour() == right.getHour() && left.getDay() == right.getDay();
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
bool operator!=(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return !(left == right);
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
bool operator<(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
if (left.getDay() < right.getDay())
2012-05-18 22:04:22 +02:00
return true;
2022-09-22 21:26:05 +03:00
if (left.getDay() > right.getDay())
2012-05-18 22:04:22 +02:00
return false;
2022-09-22 21:26:05 +03:00
return left.getHour() < right.getHour();
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
bool operator<=(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return left < right || left == right;
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
bool operator>(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return !(left <= right);
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
bool operator>=(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return !(left < right);
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
TimeStamp operator+(const TimeStamp& stamp, double hours)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return TimeStamp(stamp) += hours;
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
TimeStamp operator+(double hours, const TimeStamp& stamp)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
return TimeStamp(stamp) += hours;
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
double operator-(const TimeStamp& left, const TimeStamp& right)
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
if (left < right)
return -(right - left);
2012-05-18 22:04:22 +02:00
int days = left.getDay() - right.getDay();
double hours = 0;
2022-09-22 21:26:05 +03:00
if (left.getHour() < right.getHour())
2012-05-18 22:04:22 +02:00
{
2022-09-22 21:26:05 +03:00
hours = 24 - right.getHour() + left.getHour();
--days;
2012-05-18 22:04:22 +02:00
}
else
{
2022-09-22 21:26:05 +03:00
hours = left.getHour() - right.getHour();
2012-05-18 22:04:22 +02:00
}
2022-09-22 21:26:05 +03:00
return hours + 24 * days;
2012-05-18 22:04:22 +02:00
}
2014-05-12 21:04:02 +02:00
ESM::TimeStamp TimeStamp::toEsm() const
{
ESM::TimeStamp ret;
ret.mDay = mDay;
ret.mHour = mHour;
return ret;
}
2022-09-22 21:26:05 +03:00
TimeStamp::TimeStamp(const ESM::TimeStamp& esm)
2014-05-12 21:04:02 +02:00
{
mDay = esm.mDay;
mHour = esm.mHour;
}
2012-05-18 22:04:22 +02:00
}