This repository has been archived on 2023-05-13. You can view files and clone it, but cannot push or open issues or pull requests.
mipt_cpp/seminar04_templates/04_time/time.cpp
2022-11-02 22:41:16 +03:00

81 lines
1.5 KiB
C++

#include "time.h"
#include <string>
#include <iomanip>
#include <vector>
using std::cout, std::endl, std::vector, std::string_view, std::string;
Time::Time(int hours, int minutes, int seconds)
{
mHours = hours;
mMinutes = minutes;
mSeconds = seconds;
}
Time::Time(string_view s)
{
string buf;
buf[0] = s[0];
buf[1] = s[1];
mHours = stoi(buf);
buf[0] = s[3];
buf[1] = s[4];
mMinutes = stoi(buf);
buf[0] = s[6];
buf[1] = s[7];
mSeconds = stoi(buf);
}
Time::Time():Time(0, 0, 0)
{
};
Time Time::operator+(Time b) const
{
return Time((mHours + b.mHours + (mMinutes + b.mMinutes) / 60) % 24,
(mMinutes + b.mMinutes +
(mSeconds + b.mSeconds) / 60) % 60,
(mSeconds + b.mSeconds) % 60);
}
int Time::hours() const
{
return mHours;
}
int Time::minutes() const
{
return mMinutes;
}
int Time::seconds() const
{
return mSeconds;
}
vector < Time > getTimesFromString(string_view s)
{
vector < Time > res;
for (int i = 0, size = s.size(); i < size; i += 9) {
res.push_back(Time(s.substr(i, 9)));
}
return res;
}
Time sumTimes(vector < Time > &v)
{
Time res;
for (int i = 0, size = v.size(); i < size; ++i) {
res = res + v[i];
}
return res;
}
std::ostream & operator<<(std::ostream & out, Time t)
{
out << std::setw(2) << std::setfill('0') << t.mHours
<< ":"
<< std::setw(2) << std::setfill('0') << t.mMinutes
<< ":" << std::setw(2) << std::setfill('0') << t.mSeconds;
return out;
}