From 3480388c90d30b86390b2b2ab6a98fa9da3aec34 Mon Sep 17 00:00:00 2001 From: Gavin Brennan Date: Fri, 26 Mar 2021 15:29:48 +0200 Subject: [PATCH 1/2] Fixed future code logic for FRI3 expiry code --- src/Qwack.Futures/FutureCode.cs | 8 ++++++++ 1 file changed, 8 insertions(+) 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) From 42f2e07be7d1920e52cfdea05893e336db49b116 Mon Sep 17 00:00:00 2001 From: Gavin Brennan Date: Sun, 28 Mar 2021 15:13:18 +0200 Subject: [PATCH 2/2] TryParse no longer relies on try/catch --- src/Qwack.Dates/Frequency.cs | 56 +++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 7 deletions(-) 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);