public static Tenor FromString(string tenor) { try { Tenor result; if (_tenorCache.TryGetValue(tenor, out result)) return result; if (tenor.Equals("on", StringComparison.InvariantCultureIgnoreCase) || tenor.Equals("o/n", StringComparison.InvariantCultureIgnoreCase)) { result = new Tenor(1, TenorType.BusinessDay); _tenorCache.TryAdd(tenor, result); return result; } char tenorChar = tenor[tenor.Length - 1]; var duration = Int32.Parse(tenor.Substring(0, tenor.Length - 1)); TenorType tenorType = TenorType.CalendarDay; switch (tenorChar) { case 'd': tenorType = TenorType.CalendarDay; break; case 'b': tenorType = TenorType.BusinessDay; break; case 'w': tenorType = TenorType.Week; break; case 'm': tenorType = TenorType.Month; break; case 'y': tenorType = TenorType.Year; break; default: throw new Exception("Unrecognized tenor " + tenor); } result = NormalizeTenor(new Tenor(duration, tenorType)); _tenorCache.TryAdd(tenor, result); return result; } catch (Exception ex) { SLog.log.Error("Error parsing tenor " + tenor, ex); } return null; }
public static DateTime AddTenor(this DateTime self, Tenor tenor, string calendar = null) { var key = string.Format("{0:yyyyMMdd}:{1}:{2}", self, tenor, calendar); return TenorCache.GetOrAdd(key, tenor.Add(self, calendar)); }
public static Tenor NormalizeTenor(Tenor tenor) { if(tenor.Duration == 0) { return new Tenor(0, TenorType.CalendarDay); } else if (tenor.TenorType == TenorType.Month && Math.Abs(tenor.Duration) % 12 == 0) { return new Tenor(tenor.Duration / 12, TenorType.Year); } return tenor; }