18 using System.Collections.Generic;
19 using System.Collections.ObjectModel;
54 public ReadOnlyCollection<MarketHoursSegment>
Segments {
get; }
78 &&
Segments[0].Start == TimeSpan.Zero
82 for (var i = 0; i <
Segments.Count; i++)
100 public LocalMarketHours(
DayOfWeek day, TimeSpan extendedMarketOpen, TimeSpan marketOpen, TimeSpan marketClose, TimeSpan extendedMarketClose)
101 : this(day,
MarketHoursSegment.GetMarketHoursSegments(extendedMarketOpen, marketOpen, marketClose, extendedMarketClose))
114 : this(day, marketOpen, marketOpen, marketClose, marketClose)
126 public TimeSpan?
GetMarketOpen(TimeSpan time,
bool extendedMarketHours, TimeSpan? previousDayLastSegment =
null)
128 var previousSegment = previousDayLastSegment;
129 bool prevSegmentIsFromPrevDay =
true;
130 for (var i = 0; i <
Segments.Count; i++)
139 previousSegment = segment.End;
140 prevSegmentIsFromPrevDay =
false;
151 return segment.Start;
154 previousSegment = segment.End;
155 prevSegmentIsFromPrevDay =
false;
172 public TimeSpan?
GetMarketClose(TimeSpan time,
bool extendedMarketHours, TimeSpan? nextDaySegmentStart =
null)
174 TimeSpan? nextSegment;
175 bool nextSegmentIsFromNextDay =
false;
176 for (var i = 0; i <
Segments.Count; i++)
186 var potentialNextSegment =
Segments[i+1];
189 if (potentialNextSegment.State !=
MarketHoursState.Market && !extendedMarketHours)
200 nextSegment = nextDaySegmentStart;
201 nextSegmentIsFromNextDay =
true;
221 public bool IsOpen(TimeSpan time,
bool extendedMarketHours)
223 for (var i = 0; i <
Segments.Count; i++)
231 if (segment.Contains(time))
248 public bool IsOpen(TimeSpan start, TimeSpan end,
bool extendedMarketHours)
252 return IsOpen(start, extendedMarketHours);
255 for (var i = 0; i <
Segments.Count; i++)
265 if (segment.Overlaps(start, end))
304 public static bool IsContinuousMarketOpen(TimeSpan? previousSegmentEnd, TimeSpan? nextSegmentStart,
bool prevSegmentIsFromPrevDay =
true)
306 if (previousSegmentEnd !=
null && nextSegmentStart !=
null)
308 if (prevSegmentIsFromPrevDay)
311 return previousSegmentEnd.Value ==
Time.
OneDay && nextSegmentStart.Value == TimeSpan.Zero;
315 return previousSegmentEnd.Value == nextSegmentStart.Value;