-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
129 lines (97 loc) · 5.42 KB
/
Copy pathProgram.cs
File metadata and controls
129 lines (97 loc) · 5.42 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
using System;
using System.Threading.Tasks;
namespace H.Necessaire.Samples.DateTimeDerivatives
{
internal class Program
{
static async Task Main(string[] args)
{
await RunDailyAlarmSampleViaPartialDateTime();
UpdateConsoleLine();
await RunBusinessHoursSampleViaPartialPeriodOfTime();
UpdateConsoleLine();
await RunRoadtripPlanningSampleViaApproximatePeriodOfTime();
UpdateConsoleLine();
}
static async Task RunDailyAlarmSampleViaPartialDateTime()
{
// Easily configure a daily alarm via PartialDateTime
PartialDateTime dailyAlarm = new PartialDateTime { Hour = 10, Minute = 30, DateTimeKind = DateTimeKind.Local, };
//And here's how it's being used;
TimelinePlayer timelinePlayer = new TimelinePlayer(
periodOfTime: (DateTime.Today.AddDays(-2), DateTime.Today.AddDays(2).AddMilliseconds(-1)),
increment: TimeSpan.FromMinutes(30),
delayBetweenRounds: TimeSpan.FromSeconds(.1)
);
await timelinePlayer.Play(async now =>
{
UpdateConsoleLine(now.PrintDateAndTime());
if (dailyAlarm.IsMatchingDateTime(now))//This is the key line of code that checks if the current time matches the daily alarm
{
using (new Highlight()) UpdateConsoleLine($"⏰🔔 DAILY ALARM ringing on {now.PrintDateAndTime()}");
await Task.Delay(TimeSpan.FromSeconds(2));
}
});
}
static async Task RunBusinessHoursSampleViaPartialPeriodOfTime()
{
// Easily configure the business hours via PartialPeriodOfTime
PartialPeriodOfTime businessHours = new PartialPeriodOfTime
{
From = new PartialDateTime { Hour = 9, Minute = 0, DateTimeKind = DateTimeKind.Local, },
To = new PartialDateTime { Hour = 21, Minute = 0, DateTimeKind = DateTimeKind.Local, },
}
.OnWeekDays([DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday])
;
//And here's how it's being used;
TimelinePlayer timelinePlayer = new TimelinePlayer(
periodOfTime: (DateTime.Today.AddDays(-14), DateTime.Today.AddDays(2).AddMilliseconds(-1)),
increment: TimeSpan.FromHours(1),
delayBetweenRounds: TimeSpan.FromSeconds(.15)
);
await timelinePlayer.Play(now =>
{
if (businessHours.OnDateAndTime(now).IsSurelyInactive(asOf: now))
using (new Highlight(ConsoleColor.Red)) UpdateConsoleLine($"⛔ CLOSED on {now.PrintDateAndTime()}");
if (businessHours.OnDateAndTime(now).IsSurelyActive(asOf: now))
using (new Highlight(ConsoleColor.Green)) UpdateConsoleLine($"✅ OPEN on {now.PrintDateAndTime()}");
return Task.CompletedTask;
});
}
static async Task RunRoadtripPlanningSampleViaApproximatePeriodOfTime()
{
//Plan a roadtrip for the summer, that will start sometime in April and end sometime in August
ApproximatePeriodOfTime roadtripPlanning = new ApproximatePeriodOfTime
{
StartPeriod = new PartialDateTime { Year = DateTime.Today.Year, Month = 4, DateTimeKind = DateTimeKind.Local, },
EndPeriod = new PartialDateTime { Year = DateTime.Today.Year, Month = 8, DateTimeKind = DateTimeKind.Local, },
};
TimelinePlayer timelinePlayer = new TimelinePlayer(
periodOfTime: (new DateTime(DateTime.Today.Year, 2, 15), new DateTime(DateTime.Today.Year, 9, 20)),
increment: TimeSpan.FromDays(1),
delayBetweenRounds: TimeSpan.FromSeconds(.15)
);
await timelinePlayer.Play(now =>
{
if (now < roadtripPlanning)
using (new Highlight(ConsoleColor.Red)) UpdateConsoleLine($"⛔🚗 NOT YET DEPARTED on {now.PrintDateAndTime()}");
else if (roadtripPlanning.HasPossiblyStarted(asOf: now) && !roadtripPlanning.HasSurelyStarted(asOf: now))
using (new Highlight(ConsoleColor.Yellow)) UpdateConsoleLine($"⚙️🚗 PLANNING on {now.PrintDateAndTime()}");
else if (roadtripPlanning.IsSurelyActive(asOf: now))
using (new Highlight(ConsoleColor.Green)) UpdateConsoleLine($"🚗 ROADTRIPPING on {now.PrintDateAndTime()}");
else if (roadtripPlanning.HasPossiblyEnded(asOf: now) && !roadtripPlanning.HasSurelyEnded(asOf: now))
using (new Highlight(ConsoleColor.Yellow)) UpdateConsoleLine($"⚙️🚗 RETURNING on {now.PrintDateAndTime()}");
else if (now > roadtripPlanning)
using (new Highlight(ConsoleColor.Red)) UpdateConsoleLine($"⛔🚗 DONE on {now.PrintDateAndTime()}");
return Task.CompletedTask;
});
}
static void UpdateConsoleLine(string newText = "")
{
Console.SetCursorPosition(0, Console.CursorTop == 0 ? 0 : Console.CursorTop - 1);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);
Console.WriteLine(newText);
}
}
}