using mbts; using System; using System.Text.RegularExpressions; namespace bts.api { internal enum DateFormat { Standard, MonthAbvDiff2000, MonthCodeDiff2000, MonthCodeLastDigitYear, DayMonthAbvDiff2000, DayMonthCodeDiff2000 } internal static class DateFormatter { #region Private Members private static void _check_month(int month) { if (month < 1 || month > 12) throw new ArgumentException("There are only twelve months in a year", "month"); } private static string[] _month_codes = new string[] { "F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z" }; private static string[] _month_abvs = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; #endregion public static bool TryParse(char c, out DateFormat result) { switch (c) { case 's': result = DateFormat.Standard; return true; case 'a': result = DateFormat.MonthAbvDiff2000; return true; case 'c': result = DateFormat.MonthCodeDiff2000; return true; case 'l': result = DateFormat.MonthCodeLastDigitYear; return true; case 'A': result = DateFormat.DayMonthAbvDiff2000; return true; case 'C': result = DateFormat.DayMonthCodeDiff2000; return true; default: result = DateFormat.Standard; return false; } } /// /// Returns a concise string representation of a date (using single-letter month codes) appropriate for UIs (e.g. March 22, 2019: 22 Mar 19, or 22 H19). /// public static string Format(DateTime date, DateFormat format) { return Format(date.Year, date.Month, date.Day, format); } /// /// Returns a concise string representation of a date (using single-letter month codes) appropriate for UIs (e.g. March 22, 2019: 22 Mar 19, or 22 H19). /// public static string Format(int year, int month, int day, DateFormat format) { switch (format) { case DateFormat.MonthAbvDiff2000: return $"{GetMonthAbv(month)}{year-2000}"; case DateFormat.MonthCodeDiff2000: return $"{GetMonthCode(month)}{year-2000}"; case DateFormat.MonthCodeLastDigitYear: return $"{GetMonthCode(month)}{year % 10}"; case DateFormat.DayMonthAbvDiff2000: return $"{day}{GetMonthAbv(month)}{year - 2000}"; case DateFormat.DayMonthCodeDiff2000: return $"{day}{GetMonthCode(month)}{year - 2000}"; case DateFormat.Standard: goto default; default: return $"{month}/{day}/{year}"; } } public static string Format(string month_code_and_single_digit_year) { string pattern = string.Format("[{0}[0-9]", string.Concat(_month_codes)); if (Regex.Match(month_code_and_single_digit_year, pattern).Success) { int month_index = Array.IndexOf(_month_codes, month_code_and_single_digit_year[0].ToString()); string two_digit_year = string.Empty; int single_digit_year = int.Parse(month_code_and_single_digit_year[1].ToString()); string now_single_digit_decade = (System.DateTime.Now.Year % 100).ToString()[0].ToString(); int now_single_digit_year_int = System.DateTime.Now.Year % 10; if (single_digit_year < now_single_digit_year_int) { // next decade - i.e. H0 means March 2020 when the current year is 2017 string next_decade = (int.Parse(now_single_digit_decade) + 1).ToString(); two_digit_year = string.Format($"{next_decade}{single_digit_year}"); } else { two_digit_year = string.Format($"{now_single_digit_decade}{single_digit_year}"); } return $"{_month_abvs[month_index]}{two_digit_year}"; } return month_code_and_single_digit_year ?? string.Empty; } public static string GetMonthDifference(string MMMyy1, string MMMyy2) { DateTime first_exp = DateTime.ParseExact(MMMyy1, "MMMyy", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")); DateTime second_exp = DateTime.ParseExact(MMMyy2, "MMMyy", System.Globalization.CultureInfo.CreateSpecificCulture("en-US")); int num_months = Math.Abs((first_exp.Month - second_exp.Month) + 12 * (first_exp.Year - second_exp.Year)); return num_months.ToString(); } /// /// Returns the option industry standard month code. /// private static string GetMonthCode(int month) { _check_month(month); return _month_codes[month - 1]; } /// /// Returns the standard month abbreviation (Jan, Feb, Mar, etc.). /// public static string GetMonthAbv(int month) { _check_month(month); return _month_abvs[month - 1]; } #region Extensions public static string SecurityName(this Security security) { return security.ContractCase == Security.ContractOneofCase.Synthetic ? security.Synthetic.Name : security.Symbol; } #endregion } }