Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/cetusfinance/qwack
Browse files Browse the repository at this point in the history
  • Loading branch information
gavbrennan committed Mar 31, 2021
2 parents 3980001 + 42f2e07 commit ce71579
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
56 changes: 49 additions & 7 deletions src/Qwack.Dates/Frequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/Qwack.Futures/FutureCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit ce71579

Please sign in to comment.