63 lines
1.8 KiB
C++
63 lines
1.8 KiB
C++
#include "Time.h"
|
|
#include <string>
|
|
|
|
Time::Time() : hour(0), minute(0), second(0) {}
|
|
|
|
Time::Time(unsigned int hour) : Time(hour, 0, 0) {}
|
|
|
|
Time::Time(unsigned int hour, unsigned int minute, unsigned int second) : hour(hour), minute(minute), second(second) {
|
|
if (hour >= MAX_HOUR || minute >= 60 || second >= 60) {
|
|
this->hour = 0;
|
|
this->minute = 0;
|
|
this->second = 0;
|
|
}
|
|
}
|
|
|
|
std::string Time::toString() const {
|
|
return std::to_string(hour) + ":" + std::to_string(minute) + ":" + std::to_string(second);
|
|
}
|
|
|
|
std::string Time::toString12HourFormat() const {
|
|
std::string ret;
|
|
if (hour > NOON_HOUR) ret = std::to_string(hour - 12); //" PM";
|
|
if (hour == NOON_HOUR) ret = std::to_string(hour); //" PM";
|
|
if (hour < NOON_HOUR) ret = std::to_string(hour); // " AM";
|
|
if (hour == 0) ret = std::to_string(12); //AM
|
|
|
|
ret = ret + ":" + std::to_string(minute) + ":" + std::to_string(second);
|
|
return ret;
|
|
}
|
|
|
|
bool Time::isTimeValid(unsigned int hour, unsigned int minute, unsigned int second) {
|
|
return !(hour >= MAX_HOUR || minute >= 60 || second >= 60);
|
|
}
|
|
|
|
Time Time::parse(const std::string &time) {
|
|
unsigned int h = std::stoi(time.substr(0, 2));
|
|
unsigned int m = std::stoi(time.substr(3, 2));
|
|
unsigned int s = std::stoi(time.substr(6, 2));
|
|
return {h, m, s};
|
|
}
|
|
|
|
const Time* Time::maxTime(const Time *time1, const Time *time2) {
|
|
unsigned int timeSec1 = time1->second + time1->minute * 60 + time1->hour * 3600;
|
|
unsigned int timeSec2 = time2->second + time2->minute * 60 + time2->hour * 3600;
|
|
return (timeSec1 > timeSec2) ? time1 : time2;
|
|
}
|
|
|
|
unsigned int Time::getHour() const {
|
|
return hour;
|
|
}
|
|
|
|
unsigned int Time::getMinute() const {
|
|
return minute;
|
|
}
|
|
|
|
unsigned int Time::getSecond() const {
|
|
return second;
|
|
}
|
|
|
|
|
|
|
|
|