Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Weekdays setting #845

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions QuickFIXn/Fields/Converters/DateTimeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ public static class DateTimeConverter
public const int NanosPerMicro = 1000;
public const int TicksPerMicrosecond = 10;
public const int NanosecondsPerTick = 100;
public const string DATE_TIME_FORMAT_WITH_NANOSECONDS = "{0:yyyyMMdd-HH:mm:ss}.{1}";
public const string DATE_TIME_FORMAT_WITH_NANOSECONDS = "{0:yyyyMMdd-HH:mm:ss}.{1:000000000}";
public const string DATE_TIME_FORMAT_WITH_MICROSECONDS = "{0:yyyyMMdd-HH:mm:ss.ffffff}";
public const string DATE_TIME_FORMAT_WITH_MILLISECONDS = "{0:yyyyMMdd-HH:mm:ss.fff}";
public const string DATE_TIME_FORMAT_WITHOUT_MILLISECONDS = "{0:yyyyMMdd-HH:mm:ss}";
public const string DATE_ONLY_FORMAT = "{0:yyyyMMdd}";
public const string TIME_ONLY_FORMAT_WITH_NANOSECONDS = "{0:HH:mm:ss}.{1}";
public const string TIME_ONLY_FORMAT_WITH_NANOSECONDS = "{0:HH:mm:ss}.{1:000000000}";
public const string TIME_ONLY_FORMAT_WITH_MICROSECONDS = "{0:HH:mm:ss.ffffff}";
public const string TIME_ONLY_FORMAT_WITH_MILLISECONDS = "{0:HH:mm:ss.fff}";
public const string TIME_ONLY_FORMAT_WITHOUT_MILLISECONDS = "{0:HH:mm:ss}";
Expand Down
37 changes: 36 additions & 1 deletion QuickFIXn/SessionSchedule.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#nullable enable
using System;
using System.Collections.Generic;

namespace QuickFix
{
Expand All @@ -12,6 +13,9 @@ public class SessionSchedule
public DayOfWeek? StartDay { get; }
public DayOfWeek? EndDay { get; }

private readonly bool _isWeekdaySession;
private readonly HashSet<DayOfWeek> _weekdays;

public bool NonStopSession { get; }

public bool UseLocalTime { get; }
Expand Down Expand Up @@ -72,6 +76,9 @@ public bool IsSessionTime(DateTime utc)

DateTime adjusted = AdjustUtcDateTime(utc);

if (_isWeekdaySession)
return CheckWeekDay(adjusted);

return WeeklySession ? CheckDay(adjusted) : CheckTime(adjusted.TimeOfDay);
}

Expand All @@ -93,7 +100,14 @@ public DateTime NextEndTime(DateTime utc)
DateTime d = AdjustUtcDateTime(utc);
DateTime end = DateTime.MinValue;

if (WeeklySession)

if (_isWeekdaySession)
{
end = new DateTime(d.Year, d.Month, d.Day, vEndTime.Hours, vEndTime.Minutes, vEndTime.Seconds, d.Kind);
if (DateTime.Compare(d, end) > 0) // d is later than end
end = end.AddDays(1);
}
else if (WeeklySession)
{
end = new DateTime(d.Year, d.Month, d.Day, vEndTime.Hours, vEndTime.Minutes, vEndTime.Seconds, d.Kind);
while (end.DayOfWeek != EndDay)
Expand Down Expand Up @@ -183,6 +197,18 @@ private bool CheckTime(TimeSpan time)
return true;
}

private bool CheckWeekDay(System.DateTime time)
{
if (NonStopSession)
{
throw new InvalidOperationException("NonStopSession is set -- this should never be called.");
}

if (_weekdays.Contains(time.DayOfWeek))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is faulty. If midnight falls within the session (e.g. it goes from 8am-2am), then it will erroneously return false between midnight-2am.

I'm fixing it in a new branch and will submit a new PR.

return CheckTime(time.TimeOfDay);
return false;
}

/// <summary>
/// </summary>
/// <param name="settings"></param>
Expand All @@ -194,6 +220,15 @@ public SessionSchedule(QuickFix.SettingsDictionary settings)
if (NonStopSession)
return;

if (settings.Has(SessionSettings.WEEK_DAYS))
{
_isWeekdaySession = true;
if (settings.Has(SessionSettings.START_DAY) || settings.Has(SessionSettings.END_DAY) )
throw new ConfigError("Usage of StartDay or EndDay is not compatible with setting Weekdays");

_weekdays = settings.GetDays(SessionSettings.WEEK_DAYS);
}

if (!settings.Has(SessionSettings.START_DAY) && settings.Has(SessionSettings.END_DAY))
throw new QuickFix.ConfigError("EndDay used without StartDay");

Expand Down
1 change: 1 addition & 0 deletions QuickFIXn/SessionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class SessionSettings
public const string TIME_ZONE = "TimeZone";
public const string START_DAY = "StartDay";
public const string END_DAY = "EndDay";
public const string WEEK_DAYS = "Weekdays";
public const string START_TIME = "StartTime";
public const string END_TIME = "EndTime";
public const string HEARTBTINT = "HeartBtInt";
Expand Down
30 changes: 30 additions & 0 deletions QuickFIXn/SettingsDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,36 @@ public bool IsBoolPresentAndTrue(string key) {
return Has(key) && GetBool(key);
}

public HashSet<DayOfWeek> GetDays(string key)
{
string[] weekdayNameArray = GetString(key).Split(",");
var result = new HashSet<DayOfWeek>(weekdayNameArray.Length);
foreach (var weekDayName in weekdayNameArray)
{
string abbr = weekDayName.Trim().Substring(0, 2).ToUpper();
switch (abbr)
{
case "SU": result.Add(DayOfWeek.Sunday);
break;
case "MO": result.Add(DayOfWeek.Monday);
break;
case "TU": result.Add(DayOfWeek.Tuesday);
break;
case "WE": result.Add(DayOfWeek.Wednesday);
break;
case "TH": result.Add(DayOfWeek.Thursday);
break;
case "FR": result.Add(DayOfWeek.Friday);
break;
case "SA": result.Add(DayOfWeek.Saturday);
break;
default: throw new ConfigError("Illegal value " + weekDayName.Trim() + " for " + key);
}
}

return result;
}

public DayOfWeek GetDay(string key) {
string abbr = GetString(key).Substring(0, 2).ToUpperInvariant();
return abbr switch
Expand Down