/
DateTimeWindow.cs
290 lines (243 loc) · 10.4 KB
/
DateTimeWindow.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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// --------------------------------------------------------------------------
// Copyright (c) 2014 Will Perry, Microsoft
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// --------------------------------------------------------------------------
namespace WillPe.Utils
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
public class DateTimeWindow
{
private static readonly Regex StringFormatExpression = new Regex(@"^\[(?<start>[^ ]+) TO (?<end>[^\]]+)\]$", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private readonly DateTime? end;
private readonly DateTime? start;
public DateTimeWindow(DateTime? start, DateTimeRange range)
: this(start, start.HasValue ? (start.Value + range) : (DateTime?)null)
{
}
public DateTimeWindow(DateTime? start, DateTime? end)
{
this.start = start;
this.end = end;
}
/// <summary>
/// Determines how to operate in cases where the start and end times do not align with supplied bucket granularities exactly
/// </summary>
public enum EdgeMode
{
/// <summary>
/// If the start and end times are not aligned with buckets add extra buckets to fully cover the requested window
/// </summary>
Grow,
/// <summary>
/// Shrink the window if the start and end times are not aligned with buckets and only return buckets which are wholly within the requested range
/// </summary>
Shrink
}
public DateTime? End
{
get { return this.end; }
}
public DateTime? Start
{
get { return this.start; }
}
public static DateTimeWindow Find(DateTime value, DateTimeRange range)
{
// TODO: this implementation is naive
var rangeStr = range.ToString("S");
switch (rangeStr)
{
case "1m":
case "5m":
case "15m":
{
var timestamp = value.ToUnixTime();
var bucketTimestamp = timestamp - (timestamp % (60 * (int)range.Magnitude));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
case "1h":
case "4h":
case "8h":
{
var timestamp = value.ToUnixTime();
var bucketTimestamp = timestamp - (timestamp % (60 * 60 * (int)range.Magnitude));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
case "1d":
{
var timestamp = value.ToUnixTime();
var bucketTimestamp = timestamp - (timestamp % (60 * 60 * 24));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
case "1w":
{
var weekStart = value.AddDays(-(int)value.DayOfWeek);
var weekStartTimestamp = weekStart.ToUnixTime();
var bucketTimestamp = weekStartTimestamp - (weekStartTimestamp % (60 * 60 * 24));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
case "1n":
{
var monthStart = value.AddDays(1 - value.Day);
var monthStartTimestamp = monthStart.ToUnixTime();
var bucketTimestamp = monthStartTimestamp - (monthStartTimestamp % (60 * 60 * 24));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
case "1y":
{
var yearStart = value.AddDays(1 - value.DayOfYear);
var yearStartTimestamp = yearStart.ToUnixTime();
var bucketTimestamp = yearStartTimestamp - (yearStartTimestamp % (60 * 60 * 24));
return new DateTimeWindow(UnixTimestamp.ToDateTime((int)bucketTimestamp), range);
}
default:
throw new ArgumentOutOfRangeException("range", "Unsupported range, '" + range + "'. Valid values are: 1m, 5m, 15m, 1h, 4h, 8h, 1d, 1w, 1n, 1y");
}
}
public static IReadOnlyCollection<DateTimeWindow> GetWindowsBetween(DateTime start, DateTime end, IReadOnlyCollection<DateTimeRange> granularities, EdgeMode edges = EdgeMode.Shrink)
{
var result = new List<DateTimeWindow>();
if (start >= end || granularities.Count == 0)
{
return result;
}
var ranges = granularities.OrderByDescending(g => g).ToArray();
AppendWindowsBetween(start, end, ranges, 0, result);
// In 'Grow' mode, ensure the returned list of buckets **at least** covers the start and end times
if (edges == EdgeMode.Grow)
{
var smallestRange = ranges.Last();
if (result.Count == 0 || result[0].start > start)
{
// No buckets fit, or start bucket starts too late.
// Add the smallest bucket starting before the start time
var startWindow = DateTimeWindow.Find(start, smallestRange);
result.Insert(0, startWindow);
}
var lastWindow = result.Last();
if (lastWindow.end < end)
{
// End bucket finishes too early.
// Add the smallest bucket starting before the end time
var finalWindow = DateTimeWindow.Find(end, smallestRange);
result.Add(finalWindow);
}
}
return result.AsReadOnly();
}
public static DateTimeWindow Parse(string value)
{
DateTimeWindow result;
if (DateTimeWindow.TryParse(value, out result))
{
return result;
}
throw new FormatException("Invalid window format: '" + value + "'.");
}
public static bool TryParse(string value, out DateTimeWindow result)
{
if (string.IsNullOrEmpty(value))
{
throw new ArgumentNullException("value");
}
var match = StringFormatExpression.Match(value);
if (!match.Success)
{
result = null;
return false;
}
var startExpr = match.Groups["start"].Value;
DateTime? start = null;
if (startExpr != "*" && !DateMath.TryEvaluate(startExpr, out start))
{
result = null;
return false;
}
var endExpr = match.Groups["end"].Value;
DateTime? end = null;
if (endExpr != "*" && !DateMath.TryEvaluate(endExpr, out end))
{
result = null;
return false;
}
result = new DateTimeWindow(start, end);
return true;
}
public static DateTimeWindow operator +(DateTimeWindow value, DateTimeRange range)
{
return new DateTimeWindow(value.start + range, value.end + range);
}
public static DateTimeWindow operator -(DateTimeWindow value, DateTimeRange range)
{
return new DateTimeWindow(value.start - range, value.end - range);
}
public override bool Equals(object obj)
{
var other = obj as DateTimeWindow;
if (other == null)
{
return false;
}
return this.end.Equals(other.end) && this.start.Equals(other.start);
}
public override int GetHashCode()
{
return this.end.GetHashCode() ^ this.start.GetHashCode();
}
public override string ToString()
{
return string.Format("[{0} TO {1}]", this.start.HasValue ? this.start.Value.ToString("s") : "*", this.end.HasValue ? this.end.Value.ToString("s") : "*");
}
private static void AppendWindowsBetween(DateTime start, DateTime end, DateTimeRange[] ranges, int rangeIndex, ICollection<DateTimeWindow> result)
{
if (start >= end || rangeIndex >= ranges.Length)
{
return;
}
var currentRange = ranges[rangeIndex];
var window = DateTimeWindow.Find(start, currentRange);
if (window.Start < start)
{
if (window.Start + currentRange <= end)
{
window += currentRange;
// Append buckets with a smaller range that are required before this one
AppendWindowsBetween(start, window.Start.Value, ranges, rangeIndex + 1, result);
}
else
{
// This bucket is too large to fit in the interval, try with a smaller range
AppendWindowsBetween(start, end, ranges, rangeIndex + 1, result);
return;
}
}
// Append buckets of this size until no more fit
var windowEnd = window.Start + currentRange;
while (windowEnd <= end)
{
result.Add(window);
window += currentRange;
windowEnd = windowEnd + currentRange;
}
if (window.Start < end)
{
// Append smaller buckets to fill the gap between bucket.Timestamp and end
AppendWindowsBetween(window.Start.Value, end, ranges, rangeIndex + 1, result);
}
}
}
}