-\r
- public class NameAndTimer\r
- {\r
- public string Name { get; set; }\r
- public RingTimer Timer { get; set; }\r
-\r
- public NameAndTimer()\r
- {\r
- Timer = new RingTimer();\r
- }\r
- }\r
-\r
- public class RingTimer\r
- {\r
- private readonly TimeSpan _spare;\r
- private bool _finished;\r
-\r
- public bool IsFinished(DateTime now) => EndTime != DateTime.MinValue && EndTime - now < _spare || _finished;\r
-\r
- public DateTime EndTime { get; private set; }\r
-\r
- public RingTimer(int spare = 60)\r
- {\r
- _spare = TimeSpan.FromSeconds(spare);\r
- }\r
-\r
- public void SetEndTime(double time)\r
- {\r
- SetEndTime((int)time == 0\r
- ? DateTime.MinValue\r
- : new DateTime(1970, 1, 1).ToLocalTime().AddSeconds(time / 1000));\r
- }\r
-\r
- public void SetEndTime(DateTime time)\r
- {\r
- EndTime = time;\r
- _finished = false;\r
- }\r
-\r
- public void Finish()\r
- {\r
- _finished = true;\r
- }\r
-\r
- public bool CheckRing(DateTime prev, DateTime now)\r
- {\r
- return EndTime != DateTime.MinValue && prev != DateTime.MinValue &&\r
- prev < EndTime -_spare && EndTime - _spare <= now;\r
- }\r
-\r
- public string ToString(DateTime now, bool endTime = false)\r
- {\r
- if (EndTime == DateTime.MinValue && !_finished)\r
- return "";\r
- if (endTime)\r
- return EndTime.ToString(@"dd\ HH\:mm", CultureInfo.InvariantCulture);\r
- var rest = _finished || EndTime - now < TimeSpan.Zero ? TimeSpan.Zero : EndTime - now;\r
- return $"{(int)rest.TotalHours:d2}:" + rest.ToString(@"mm\:ss", CultureInfo.InvariantCulture);\r
- }\r
- }\r