Skip to content

Commit

Permalink
Merge pull request #590 from quickfixgo/weekdays
Browse files Browse the repository at this point in the history
Adding Weekdays Configuration Option
  • Loading branch information
ackleymi authored Oct 31, 2023
2 parents 9d2a3c2 + d5746f1 commit efa0bdd
Show file tree
Hide file tree
Showing 7 changed files with 1,137 additions and 366 deletions.
1 change: 1 addition & 0 deletions config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
EndTime string = "EndTime"
StartDay string = "StartDay"
EndDay string = "EndDay"
Weekdays string = "Weekdays"
TimeZone string = "TimeZone"
DataDictionary string = "DataDictionary"
TransportDataDictionary string = "TransportDataDictionary"
Expand Down
10 changes: 8 additions & 2 deletions config/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,22 @@ Time of day that this FIX session becomes deactivated. Valid Values:
# StartDay
For week long sessions, the starting day of week for the session. Use in combination with StartTime. Valid Values:
For week long sessions, the starting day of week for the session. Use in combination with StartTime. Incompatible with Weekdays. Valid Values:
Full day of week in English, or 3 letter abbreviation (i.e. Monday and Mon are valid)
# EndDay
For week long sessions, the ending day of week for the session. Use in combination with EndTime. Valid Values:
For week long sessions, the ending day of week for the session. Use in combination with EndTime. Incompatible with Weekdays. Valid Values:
Full day of week in English, or 3 letter abbreviation (i.e. Monday and Mon are valid)
# Weekdays
For daily sessions that are only active on specific days of the week. Use in combination with StartTime and EndTime. Incompatible with StartDay and EndDay. Valid Values:
Comma delimited list of days of the week in English, or 3 letter abbreviation (e.g. "Monday,Tuesday,Wednesday" or "Mon,Tue,Wed" would both be valid values).
# EnableLastMsgSeqNumProcessed
Add the last message sequence number processed in the header (optional tag 369). Valid Values:
Expand Down
43 changes: 34 additions & 9 deletions internal/time_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,42 +36,67 @@ func ParseTimeOfDay(str string) (TimeOfDay, error) {
// TimeRange represents a time band in a given time zone.
type TimeRange struct {
startTime, endTime TimeOfDay
weekdays []time.Weekday
startDay, endDay *time.Weekday
loc *time.Location
}

// NewUTCTimeRange returns a time range in UTC.
func NewUTCTimeRange(start, end TimeOfDay) *TimeRange {
return NewTimeRangeInLocation(start, end, time.UTC)
func NewUTCTimeRange(start, end TimeOfDay, weekdays []time.Weekday) (*TimeRange, error) {
return NewTimeRangeInLocation(start, end, weekdays, time.UTC)
}

// NewTimeRangeInLocation returns a time range in a given location.
func NewTimeRangeInLocation(start, end TimeOfDay, loc *time.Location) *TimeRange {
func NewTimeRangeInLocation(start, end TimeOfDay, weekdays []time.Weekday, loc *time.Location) (*TimeRange, error) {

if loc == nil {
panic("time: missing Location in call to NewTimeRangeInLocation")
return nil, errors.New("time: missing Location in call to NewTimeRangeInLocation")
}

return &TimeRange{startTime: start, endTime: end, loc: loc}
return &TimeRange{
startTime: start,
endTime: end,
weekdays: weekdays,
loc: loc,
}, nil
}

// NewUTCWeekRange returns a weekly TimeRange.
func NewUTCWeekRange(startTime, endTime TimeOfDay, startDay, endDay time.Weekday) *TimeRange {
func NewUTCWeekRange(startTime, endTime TimeOfDay, startDay, endDay time.Weekday) (*TimeRange, error) {
return NewWeekRangeInLocation(startTime, endTime, startDay, endDay, time.UTC)
}

// NewWeekRangeInLocation returns a time range in a given location.
func NewWeekRangeInLocation(startTime, endTime TimeOfDay, startDay, endDay time.Weekday, loc *time.Location) *TimeRange {
r := NewTimeRangeInLocation(startTime, endTime, loc)
func NewWeekRangeInLocation(startTime, endTime TimeOfDay, startDay, endDay time.Weekday, loc *time.Location) (*TimeRange, error) {
r, err := NewTimeRangeInLocation(startTime, endTime, []time.Weekday{}, loc)
if err != nil {
return nil, err
}
r.startDay = &startDay
r.endDay = &endDay

return r
return r, nil
}

func (r *TimeRange) isInTimeRange(t time.Time) bool {
t = t.In(r.loc)
ts := NewTimeOfDay(t.Clock()).d

if len(r.weekdays) > 0 {
found := false

for _, weekday := range r.weekdays {
if t.Weekday() == weekday {
found = true
break
}
}

if !found {
return false
}
}

if r.startTime.d < r.endTime.d {
return r.startTime.d <= ts && ts <= r.endTime.d
}
Expand Down
Loading

0 comments on commit efa0bdd

Please sign in to comment.