* move DateFormat enum into sep file * sort members * use new lang feats * run formatter
112 lines
5.2 KiB
C#
112 lines
5.2 KiB
C#
namespace bts.api {
|
|
using System;
|
|
using System.Globalization;
|
|
using System.Text.RegularExpressions;
|
|
|
|
public static class DateFormatter {
|
|
static readonly string[] _month_codes = new string[] {
|
|
"F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z"
|
|
};
|
|
static readonly string[] _month_abvs = new string[] {
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
};
|
|
|
|
public static bool TryParse(char c, out DateFormat result) {
|
|
switch (c) {
|
|
case 's': result = DateFormat.Standard; break;
|
|
case 'a': result = DateFormat.MonthAbvDiff2000; break;
|
|
case 'c': result = DateFormat.MonthCodeDiff2000; break;
|
|
case 'l': result = DateFormat.MonthCodeLastDigitYear; break;
|
|
case 'A': result = DateFormat.DayMonthAbvDiff2000; break;
|
|
case 'C': result = DateFormat.DayMonthCodeDiff2000; break;
|
|
default:
|
|
result = DateFormat.Standard;
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
public static string Format(DateTime date, DateFormat format) =>
|
|
Format(date.Year, date.Month, date.Day, format);
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
public static string Format(uint year, uint month, uint day, DateFormat format) =>
|
|
Format((int)year, (int)month, (int)day, format);
|
|
|
|
/// <summary>
|
|
/// 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).
|
|
/// </summary>
|
|
public static string Format(int year, int month, int day, DateFormat format) =>
|
|
format switch {
|
|
DateFormat.MonthAbvDiff2000 => $"{GetMonthAbv(month)}{year - 2000}",
|
|
DateFormat.MonthCodeDiff2000 => $"{GetMonthCode(month)}{year - 2000}",
|
|
DateFormat.MonthCodeLastDigitYear => $"{GetMonthCode(month)}{year % 10}",
|
|
DateFormat.DayMonthAbvDiff2000 => $"{day}{GetMonthAbv(month)}{year - 2000}",
|
|
DateFormat.DayMonthCodeDiff2000 => $"{day}{GetMonthCode(month)}{year - 2000}",
|
|
_ => $"{month}/{day}/{year}",
|
|
};
|
|
|
|
public static string Format(string month_code_and_single_digit_year) {
|
|
string pattern = $"[{string.Concat(_month_codes)}[0-9]";
|
|
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());
|
|
int single_digit_year = int.Parse(month_code_and_single_digit_year[1].ToString());
|
|
string now_single_digit_decade = (DateTime.Now.Year % 100).ToString()[0].ToString();
|
|
int now_single_digit_year_int = DateTime.Now.Year % 10;
|
|
string two_digit_year;
|
|
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) {
|
|
var culture = CultureInfo.CreateSpecificCulture("en-US");
|
|
DateTime first_exp = DateTime.ParseExact(MMMyy1, "MMMyy", culture);
|
|
DateTime second_exp = DateTime.ParseExact(MMMyy2, "MMMyy", culture);
|
|
int num_months = Math.Abs(first_exp.Month - second_exp.Month + 12 * (first_exp.Year - second_exp.Year));
|
|
return num_months.ToString();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the option industry standard month code.
|
|
/// </summary>
|
|
public static string GetMonthCode(int month) {
|
|
_check_month(month);
|
|
return _month_codes[month - 1];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the standard month abbreviation (Jan, Feb, Mar, etc.).
|
|
/// </summary>
|
|
public static string GetMonthAbv(int month) {
|
|
_check_month(month);
|
|
return _month_abvs[month - 1];
|
|
}
|
|
|
|
public static string SecurityName(this Security security) =>
|
|
security.ContractCase == Security.ContractOneofCase.Synthetic ?
|
|
security.Synthetic.Name : security.Symbol;
|
|
|
|
static void _check_month(int month) {
|
|
if (month < 1 || month > 12) {
|
|
throw new ArgumentException("There are only twelve months in a year", nameof(month));
|
|
}
|
|
}
|
|
}
|
|
}
|