diff --git a/src/Qwack.Dates/Frequency.cs b/src/Qwack.Dates/Frequency.cs index e63578ef..1f0b03db 100644 --- a/src/Qwack.Dates/Frequency.cs +++ b/src/Qwack.Dates/Frequency.cs @@ -26,16 +26,15 @@ public Frequency(int periodCount, DatePeriodType periodType) public static bool TryParse(string frequency, out Frequency output) { - try + var (success, periodType, periodCount) = TrySplitPeriod(frequency); + + if (success) { - output = new Frequency(frequency); + output = new Frequency(periodCount, periodType); return true; } - catch - { - output = new Frequency(0, DatePeriodType.D); - return false; - } + output = new Frequency(0, DatePeriodType.D); + return false; } public DatePeriodType PeriodType { get; set; } @@ -75,6 +74,49 @@ private void SplitPeriod(string period) PeriodCount = int.Parse(period.Substring(0, period.Length - 1)); } + public static (bool success, DatePeriodType periodType, int periodCount) TrySplitPeriod(string period) + { + var periodTypeStr = period[period.Length - 1]; + var periodType = DatePeriodType.Day; + var periodCount = 0; + var success = true; + + switch (periodTypeStr) + { + case 'D': + case 'd': + periodType = DatePeriodType.D; + break; + case 'Y': + case 'y': + periodType = DatePeriodType.Y; + break; + case 'M': + case 'm': + periodType = DatePeriodType.M; + break; + case 'B': + case 'b': + periodType = DatePeriodType.B; + break; + case 'w': + case 'W': + periodType = DatePeriodType.W; + break; + default: + success = false; + break; + } + + var result = int.TryParse(period.Substring(0, period.Length - 1), out var pc); + if (result) + periodCount = pc; + else + success = false; + + return (success, periodType, periodCount); + } + public static bool operator ==(Frequency x, Frequency y) => x.PeriodCount == y.PeriodCount && y.PeriodType == x.PeriodType; public static bool operator !=(Frequency x, Frequency y) => !(x == y); diff --git a/src/Qwack.Futures/FutureCode.cs b/src/Qwack.Futures/FutureCode.cs index 87149591..4d244896 100644 --- a/src/Qwack.Futures/FutureCode.cs +++ b/src/Qwack.Futures/FutureCode.cs @@ -184,6 +184,10 @@ public DateTime GetExpiry() var dateInMonth = new DateTime(YearNumber, monthNum, 1); dayOfMonthToStart = dateInMonth.NthSpecificWeekDay(DayOfWeek.Wednesday, 3).Day; break; + case "FRI3": + var dateInMonth2 = new DateTime(YearNumber, monthNum, 1); + dayOfMonthToStart = dateInMonth2.NthSpecificWeekDay(DayOfWeek.Friday, 3).Day; + break; case "LASTFRI": var dateInMonth1 = new DateTime(YearNumber, monthNum, 1); dayOfMonthToStart = dateInMonth1.NthLastSpecificWeekDay(DayOfWeek.Friday, 1) @@ -218,6 +222,10 @@ public DateTime GetRollDate() var dateInMonth = new DateTime(YearNumber, monthNum, 1); dayOfMonthToStart = dateInMonth.NthSpecificWeekDay(DayOfWeek.Wednesday, 3).Day; break; + case "FRI3": + var dateInMonth2 = new DateTime(YearNumber, monthNum, 1); + dayOfMonthToStart = dateInMonth2.NthSpecificWeekDay(DayOfWeek.Friday, 3).Day; + break; case "LASTFRI": var dateInMonth1 = new DateTime(YearNumber, monthNum, 1); dayOfMonthToStart = dateInMonth1.NthLastSpecificWeekDay(DayOfWeek.Friday, 1)