-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimePeriod.cs
236 lines (205 loc) · 7.4 KB
/
TimePeriod.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* Copyright (c) 2012-2022 Jens-Uwe Rossbach
*
* This code is licensed under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace ClockIn
{
/// <summary>
/// Represents a certain period of time
/// </summary>
public class TimePeriod : INotifyPropertyChanged
{
private DateTime startTime;
private DateTime endTime;
/// <summary>
/// Event notifies when a property from this time period has changed.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Default constructor of the class
/// </summary>
public TimePeriod()
{
DateTime now = DateTime.Now;
SetupTime(now, now);
}
/// <summary>
/// Constructs the time period from a start and end time.
/// </summary>
/// <param name="start">Start time</param>
/// <param name="end">End time</param>
public TimePeriod(DateTime start, DateTime end)
{
if (endTime < startTime)
{
SetupTime(start, start);
}
else
{
SetupTime(start, end);
}
}
/// <summary>
/// Constructs the time period from its string representation.
/// </summary>
/// <param name="periodString">String representation of the time period</param>
public TimePeriod(string periodString)
{
string[] parts = periodString.Split('|');
if (parts.Length != 2)
{
throw new ArgumentException("Period string must contain start and end time");
}
startTime = DateTime.Parse(parts[0]);
endTime = DateTime.Parse(parts[1]);
}
/// <summary>
/// Constructs the time period by cloning another one.
/// </summary>
/// <param name="other">Time period to be cloned</param>
public TimePeriod(TimePeriod other)
{
startTime = other.StartTime;
endTime = other.EndTime;
}
/// <summary>
/// Start time of the time period
/// </summary>
public DateTime StartTime
{
get => startTime;
set
{
DateTime val = value.Truncate(TimeSpan.FromMinutes(1));
if (startTime != val)
{
if (val > endTime)
{
startTime = endTime;
}
else
{
startTime = val;
}
NotifyPropertyChanged();
}
}
}
/// <summary>
/// End time of the time period
/// </summary>
public DateTime EndTime
{
get => endTime;
set
{
DateTime val = value.Truncate(TimeSpan.FromMinutes(1));
if (endTime != val)
{
if (val < startTime)
{
endTime = startTime;
}
else
{
endTime = val;
}
NotifyPropertyChanged();
}
}
}
/// <summary>
/// Time span of the time period
/// </summary>
public TimeSpan Duration => endTime - startTime;
/// <summary>
/// Copies another time period to this one.
/// </summary>
/// <param name="other">Time period to copy</param>
public void CopyFrom(TimePeriod other)
{
bool notify = false;
if (startTime != other.StartTime)
{
startTime = other.StartTime;
notify = true;
}
if (endTime != other.EndTime)
{
endTime = other.EndTime;
notify = true;
}
if (notify)
{
NotifyPropertyChanged();
}
}
/// <summary>
/// Checks if this time period intersects with another one.
/// </summary>
/// <param name="other">Time period to check for intersection</param>
/// <returns>true if time period intersects with the other one, false otherwise</returns>
public bool Intersecting(TimePeriod other) => Math.Min(endTime.Ticks, other.EndTime.Ticks) > Math.Max(startTime.Ticks, other.StartTime.Ticks);
/// <summary>
/// Returns the intersection between this time period and another one.
/// </summary>
/// <param name="other">Time period to get intersection for</param>
/// <returns>Intersection with other time period as time span</returns>
public TimeSpan GetIntersection(TimePeriod other)
{
if (Intersecting(other))
{
return new TimeSpan(Math.Min(endTime.Ticks, other.EndTime.Ticks) - Math.Max(startTime.Ticks, other.StartTime.Ticks));
}
else
{
return TimeSpan.Zero;
}
}
/// <summary>
/// Converts the time period into its string representation.
/// </summary>
/// <returns>String representation of the time period</returns>
public override string ToString() => startTime.ToString("s") + "|" + endTime.ToString("s");
/// <summary>
/// Initializes the time period from a start and end time.
/// </summary>
/// <param name="start">Start time</param>
/// <param name="end">End time</param>
private void SetupTime(DateTime start, DateTime end)
{
startTime = start.Truncate(TimeSpan.FromMinutes(1));
endTime = end.Truncate(TimeSpan.FromMinutes(1));
}
/// <summary>
/// Notifies the change of a property from this time period.
/// </summary>
/// <param name="propertyName">Name of the property that has changed</param>
private void NotifyPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}