71 lines
1.3 KiB
C++
71 lines
1.3 KiB
C++
#include "clock.h"
|
|
|
|
#include <cstring>
|
|
|
|
namespace date_independent {
|
|
|
|
namespace {
|
|
|
|
const minutes_t minutes_per_hour = 60;
|
|
const hours_t hours_per_day = 24;
|
|
const total_t minutes_per_day = hours_per_day * minutes_per_hour;
|
|
|
|
inline void wrap_midnight(total_t &total_minutes)
|
|
{
|
|
total_minutes %= minutes_per_day;
|
|
|
|
if (total_minutes < 0)
|
|
total_minutes += minutes_per_day;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
clock::clock(hours_t hours, minutes_t minutes)
|
|
{
|
|
total_minutes_ = minutes_per_hour*hours + minutes;
|
|
wrap_midnight(total_minutes_);
|
|
}
|
|
|
|
bool clock::operator==(const clock &other) const
|
|
{
|
|
return total_minutes_ == other.total_minutes_;
|
|
}
|
|
|
|
bool clock::operator!=(const clock &other) const
|
|
{
|
|
return !(*this == other);
|
|
}
|
|
|
|
clock clock::at(hours_t hours, minutes_t minutes)
|
|
{
|
|
return clock(hours, minutes);
|
|
}
|
|
|
|
clock clock::plus(minutes_t minutes) const
|
|
{
|
|
clock result(*this);
|
|
|
|
result.total_minutes_ += minutes;
|
|
wrap_midnight(result.total_minutes_);
|
|
|
|
return result;
|
|
}
|
|
|
|
clock clock::minus(minutes_t minutes) const
|
|
{
|
|
return plus(-minutes);
|
|
}
|
|
|
|
clock::operator std::string() const
|
|
{
|
|
minutes_t minutes = total_minutes_ % minutes_per_hour;
|
|
hours_t hours = total_minutes_ / minutes_per_hour;
|
|
|
|
char buff[6];
|
|
snprintf(buff, 6, "%.2i:%.2i", hours, minutes);
|
|
|
|
return buff;
|
|
}
|
|
|
|
}
|