using System;
namespace TimeWpfApp
{
public class TimeModel
{
private int _hour;
private int _minute;
private int _second;
public int Hour
{
get => _hour;
set
{
if (value < 0 || value > 23)
throw new ArgumentOutOfRangeException("Hour", "Часы должны быть от 0 до 23");
_hour = value;
}
}
public int Minute
{
get => _minute;
set
{
if (value < 0 || value > 59)
throw new ArgumentOutOfRangeException("Minute", "Минуты должны быть от 0 до 59");
_minute = value;
}
}
public int Second
{
get => _second;
set
{
if (value < 0 || value > 59)
throw new ArgumentOutOfRangeException("Second", "Секунды должны быть от 0 до 59");
_second = value;
}
}
public TimeModel(int hour = 0, int minute = 0, int second = 0)
{
Hour = hour;
Minute = minute;
Second = second;
}
public void AddHours(int hours)
{
int totalHours = _hour + hours;
_hour = totalHours % 24;
if (_hour < 0) _hour += 24;
}
public void AddMinutes(int minutes)
{
long totalSeconds = (long)_hour * 3600 + _minute * 60 + _second + (long)minutes * 60;
Normalize(totalSeconds);
}
public void AddSeconds(int seconds)
{
long totalSeconds = (long)_hour * 3600 + _minute * 60 + _second + seconds;
Normalize(totalSeconds);
}
private void Normalize(long totalSeconds)
{
const int secondsInDay = 86400;
totalSeconds %= secondsInDay;
if (totalSeconds < 0) totalSeconds += secondsInDay;
_hour = (int)(totalSeconds / 3600);
_minute = (int)((totalSeconds % 3600) / 60);
_second = (int)(totalSeconds % 60);
}
public override string ToString()
{
return $"{_hour:D2}:{_minute:D2}:{_second:D2}";
}
}
}