-
Notifications
You must be signed in to change notification settings - Fork 0
/
SectionTime.cs
211 lines (193 loc) · 6.51 KB
/
SectionTime.cs
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using cAlgo.API;
namespace SectionTime
{
public class UTCZones
{
public enum CustomTimeZones
{
AppTimeOffset,
ServerTime,
Minus12h00m,
Minus11h30m,
Minus1100m,
Minus10h30m,
Minus1000m,
Minus9h30m,
Minus9h00m,
Minus8h30m,
Minus8h00m,
Minus7h30m,
Minus7h00m,
Minus6h30m,
Minus6h00m,
Minus5h30m,
Minus5h00m,
Minus4h30m,
Minus4h00m,
Minus3h30m,
Minus3h00m,
Minus2h30m,
Minus2h00m,
Minus1h30m,
Minus1h00m,
Minus0h30m,
UTC,
Plus0h30m,
Plus1h00m,
Plus1h30m,
Plus2h00m,
Plus2h30m,
Plus3h00m,
Plus3h30m,
Plus4h00m,
Plus4h30m,
Plus5h00m,
Plus5h30m,
Plus6h00m,
Plus6h30m,
Plus7h00m,
Plus7h30m,
Plus8h00m,
Plus8h30m,
Plus9h00m,
Plus9h30m,
Plus10h00m,
Plus10h30m,
Plus11h00m,
Plus11h30m,
Plus12h00m,
Plus12h30m,
Plus13h00m,
Plus13h30m,
Plus14h00m
}
public static TimeSpan ToTimeSpan(CustomTimeZones timeZone, Robot _cbot)
{
switch (timeZone)
{
case CustomTimeZones.AppTimeOffset:
return _cbot.Application.UserTimeOffset;
case CustomTimeZones.ServerTime:
return _cbot.TimeZone.GetUtcOffset(_cbot.Server.TimeInUtc);
case CustomTimeZones.UTC:
return TimeSpan.Zero;
default:
string timeZoneString = timeZone.ToString();
string numberPart = timeZoneString.Replace("Minus", "").Replace("Plus", "");
string hours = numberPart.Substring(0, numberPart.IndexOf("h"));
string minutes = numberPart.Substring(numberPart.IndexOf("h") + 1, numberPart.IndexOf("m") - numberPart.IndexOf("h") - 1);
if (int.TryParse(hours, out int h) && int.TryParse(minutes, out int m))
{
TimeSpan result = new TimeSpan(h, m, 0);
if (timeZoneString.StartsWith("Minus"))
{
result = result.Negate();
}
return result;
}
else
{
throw new ArgumentException("TimeZone invalid.");
}
}
}
}
public class RangeTime
{
private readonly Robot _cbot;
public DateTime StartDateTime { get; private set; }
public DateTime EndDateTime { get; private set; }
public DateTime CurrentDatetime { get; private set; }
public TimeSpan CurrentTimeSpan { get; private set; }
public int StartHour { get; private set; }
public int StartMinute { get; private set; }
public int StartSecond { get; private set; }
public int EndHour { get; private set; }
public int EndMinute { get; private set; }
public int EndSecond { get; private set; }
public RangeTime(Robot robot, int startHour, int startMinute, int startSecond, int endHour, int endMinute, int endSecond)
{
_cbot = robot;
StartHour = startHour;
StartMinute = startMinute;
StartSecond = startSecond;
EndHour = endHour;
EndMinute = endMinute;
EndSecond = endSecond;
CurrentTimeSpan = TimeSpan.Zero;
}
public void AppUserTimeOffsetChanged(UserTimeOffsetChangedEventArgs args)
{
if (args == null) { _cbot.Print("UserTimeOffsetChangedEventArgs is null"); return; }
_cbot.Print("User timezone changed to " + args.UserTimeOffset.ToString());
UpdateUserTimeOffset();
}
public void UpdateUserTimeOffset()
{
CurrentTimeSpan = _cbot.Application.UserTimeOffset;
}
public void UpdateUserTimeOffset(TimeSpan timeSpan)
{
CurrentTimeSpan = timeSpan;
}
public void UpdateUserTimeOffset(UTCZones.CustomTimeZones timeZone)
{
TimeSpan timeSpan = UTCZones.ToTimeSpan(timeZone, _cbot);
CurrentTimeSpan = timeSpan;
_cbot.Print($"User select {timeZone} converted to {timeSpan} with respect to UTC");
}
public bool VerifyFormattingTime()
{
if (
(StartHour < 0 && StartHour >= 24) &&
(StartMinute < 0 && StartMinute >= 60) &&
(StartSecond < 0 && StartSecond >= 60) &&
(EndHour < 0 && EndHour >= 24) &&
(EndMinute < 0 && EndMinute >= 60) &&
(EndSecond < 0 && EndSecond >= 60)
) { return (false); }
return true;
}
public bool VerifyInsideInterval()
{
if (CurrentDatetime >= StartDateTime && CurrentDatetime <= EndDateTime)
{
return true;
}
return false;
}
public bool VerifyInsideInterval(DateTime currentDatetime)
{
if (currentDatetime >= StartDateTime && currentDatetime <= EndDateTime) { return true; }
return false;
}
public bool UpdateDatetimeInterval()
{
CurrentDatetime = _cbot.Server.TimeInUtc + CurrentTimeSpan;
if (!VerifyFormattingTime() || CurrentDatetime == DateTime.MinValue)
{
_cbot.Stop();
return false;
}
StartDateTime = new DateTime(
CurrentDatetime.Year,
CurrentDatetime.Month,
CurrentDatetime.Day,
StartHour,
StartMinute,
StartSecond,
DateTimeKind.Utc
);
EndDateTime = new DateTime(
CurrentDatetime.Year,
CurrentDatetime.Month,
CurrentDatetime.Day,
EndHour,
EndMinute,
EndSecond,
DateTimeKind.Utc
);
return true;
}
}
}