private static DateTime Aux_GetRoundDate(this DateTime dt, TenorUnit tu) { int nb_units = dt.GetNumberOfUnit(tu); switch (tu) { case TenorUnit.Hour: case TenorUnit.Min: case TenorUnit.Sec: break; case TenorUnit.Month: case TenorUnit.Day: nb_units -= 1; break; case TenorUnit.Year: case TenorUnit.None: nb_units = 0; break; } dt = dt.AddTenor(new Tenor(-nb_units, tu)); TenorUnit tuN = tu.GetLowerUnit(skipWeek: true); if (tuN == TenorUnit.None) { return(dt); } else { return(dt.Aux_GetRoundDate(tuN)); } }
public static int GetNumberOfUnit(this DateTime dt, TenorUnit tnrUnit) { switch (tnrUnit) { case TenorUnit.Sec: return(dt.Second); case TenorUnit.Min: return(dt.Minute); case TenorUnit.Hour: return(dt.Hour); case TenorUnit.Day: return(dt.Day); case TenorUnit.Week: return(dt.Day / 7); case TenorUnit.Month: return(dt.Month); case TenorUnit.Year: return(dt.Year); default: throw new Exception($"Unknown TenorUnit: [{tnrUnit}]"); } }
public static DateTime GetRoundDate(this DateTime dt, TenorUnit tu) { if (tu == TenorUnit.Week) { int daysOffset = ((int)dt.DayOfWeek == 0 ? 7 : (int)dt.DayOfWeek) - 1; DateTime newDate = dt.AddDays(-daysOffset); return(newDate.Aux_GetRoundDate(TenorUnit.Hour)); } else if (tu == TenorUnit.Day) { //if (dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0) //return dt.AddDays(-1); } return(dt.Aux_GetRoundDate(tu.GetLowerUnit(skipWeek: true))); }
public Tenor(string tenorInput) { if (tenorInput == "") { _Unit = TenorUnit.None; } else if (tenorInput.Length == 1) { _Unit = TenorUnit.None; } else { Int32.TryParse(tenorInput.Substring(0, tenorInput.Length - 1), out _Number); _Unit = TenorUnitTools.GetTenorUnitFromChar(tenorInput.LastOrDefault()); } }
public Tenor(string name) { if (name == null) { throw new ArgumentNullException("name", "Must not be null. "); } if (String.IsNullOrWhiteSpace(name)) { throw new ArgumentException("Must not be empty or white space", "name"); } Match match = new Regex(RegexPattern).Match(name); if (!match.Success) { throw new ArgumentException(String.Format("Not a match for a valid tenor. Must match '{0}'. ", RegexPattern), "name"); } Name = name; Value = int.Parse(match.Groups["Value"].Value); Unit = ParseUnit(match.Groups["Unit"].Value); }
public static TenorUnit GetLowerUnit(this TenorUnit tu, bool skipWeek = false) { switch (tu) { case TenorUnit.Sec: return(TenorUnit.None); case TenorUnit.Min: return(TenorUnit.Sec); case TenorUnit.Hour: return(TenorUnit.Min); case TenorUnit.Day: return(TenorUnit.Hour); case TenorUnit.Week: return(TenorUnit.Day); case TenorUnit.Month: if (skipWeek) { return(TenorUnit.Day); } else { return(TenorUnit.Week); } case TenorUnit.Year: return(TenorUnit.Month); default: return(TenorUnit.None); } }
public Tenor(int nb, TenorUnit tu) { _Number = nb; _Unit = tu; }