This repository was archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClockTimer.cs
More file actions
57 lines (56 loc) · 1.25 KB
/
ClockTimer.cs
File metadata and controls
57 lines (56 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
namespace WebsiteProxy
{
public static class ClockTimer
{
public static DateTime? NextTime(TimeSpan[] times)
{
return NextTime(times, DateTime.UtcNow);
}
public static DateTime? NextTime(TimeSpan[] times, DateTime now)
{
if (times.Length <= 0)
{
return null;
}
foreach (TimeSpan timeOfDay in new TimeSpan[] { now.TimeOfDay, now.TimeOfDay - TimeSpan.FromDays(1)})
{
TimeSpan? closestTime = null;
foreach (TimeSpan time in times)
{
if (time > timeOfDay)
{
TimeSpan timeUntil = time - timeOfDay;
if (closestTime == null || timeUntil < closestTime)
{
closestTime = timeUntil;
}
}
}
if (closestTime != null)
{
return now + closestTime;
}
}
return null;
}
// https://stackoverflow.com/a/18611442/13347795.
public static void DoAtTime(DateTime time, Action action)
{
DoAfterDelay(time - DateTime.UtcNow, action);
}
public static void DoAfterDelay(TimeSpan delay, Action action)
{
Timer timer = new Timer((x) =>
{
action();
}, null, delay, Timeout.InfiniteTimeSpan);
}
public static void DoAfterDelay(long delay, Action action)
{
Timer timer = new Timer((x) =>
{
action();
}, null, delay, Timeout.Infinite);
}
}
}