18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
70 public ReadOnlyCollection<MarketHoursSegment>
Segments {
get; }
94 &&
Segments[0].Start == TimeSpan.Zero
98 for (var i = 0; i <
Segments.Count; i++)
116 public LocalMarketHours(
DayOfWeek day, TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose)
117 : this(day,
MarketHoursSegment.GetMarketHoursSegments(extendedMarketOpen, marketOpen, marketClose, extendedMarketClose))
130 : this(day, marketOpen, marketOpen, marketClose, marketClose)
142 public TimeSpan?
GetMarketOpen(TimeSpan time,
bool extendedMarketHours, TimeSpan? previousDayLastSegment =
null)
144 var previousSegment = previousDayLastSegment;
145 bool prevSegmentIsFromPrevDay =
true;
146 for (var i = 0; i <
Segments.Count; i++)
155 previousSegment = segment.End;
156 prevSegmentIsFromPrevDay =
false;
167 return segment.Start;
170 previousSegment = segment.End;
171 prevSegmentIsFromPrevDay =
false;
188 public TimeSpan?
GetMarketClose(TimeSpan time,
bool extendedMarketHours, TimeSpan? nextDaySegmentStart =
null)
190 return GetMarketClose(time, extendedMarketHours, lastClose:
false, nextDaySegmentStart);
203 public TimeSpan?
GetMarketClose(TimeSpan time,
bool extendedMarketHours,
bool lastClose, TimeSpan? nextDaySegmentStart =
null)
205 TimeSpan? potentialResult =
null;
206 TimeSpan? nextSegment;
207 bool nextSegmentIsFromNextDay =
false;
208 for (var i = 0; i <
Segments.Count; i++)
218 var potentialNextSegment =
Segments[i+1];
221 if (potentialNextSegment.State !=
MarketHoursState.Market && !extendedMarketHours)
232 nextSegment = nextDaySegmentStart;
233 nextSegmentIsFromNextDay =
true;
241 potentialResult = segment.End;
249 return potentialResult;
258 public bool IsOpen(TimeSpan time,
bool extendedMarketHours)
260 for (var i = 0; i <
Segments.Count; i++)
268 if (segment.Contains(time))
285 public bool IsOpen(TimeSpan start, TimeSpan end,
bool extendedMarketHours)
289 return IsOpen(start, extendedMarketHours);
292 for (var i = 0; i <
Segments.Count; i++)
302 if (segment.Overlaps(start, end))
323 return _closedSunday;
325 return _closedMonday;
327 return _closedTuesday;
329 return _closedWednesday;
331 return _closedThursday;
333 return _closedFriday;
335 return _closedSaturday;
337 throw new ArgumentOutOfRangeException(nameof(dayOfWeek));
357 return _openWednesday;
359 return _openThursday;
363 return _openSaturday;
365 throw new ArgumentOutOfRangeException(nameof(dayOfWeek));
377 public static bool IsContinuousMarketOpen(TimeSpan? previousSegmentEnd, TimeSpan? nextSegmentStart,
bool prevSegmentIsFromPrevDay =
true)
379 if (previousSegmentEnd !=
null && nextSegmentStart !=
null)
381 if (prevSegmentIsFromPrevDay)
384 return previousSegmentEnd.Value ==
Time.
OneDay && nextSegmentStart.Value == TimeSpan.Zero;
388 return previousSegmentEnd.Value == nextSegmentStart.Value;