DateCodeConverter.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace LocalhostMES.Helpers
  5. {
  6. /// <summary>
  7. /// 年/月/日与单字符编码互转(用于本地 SN 等场景)。
  8. /// </summary>
  9. public static class DateCodeConverter
  10. {
  11. private static readonly Dictionary<int, string> yearToCode = new Dictionary<int, string>
  12. {
  13. {2018, "1"}, {2019, "2"}, {2020, "3"}, {2021, "4"}, {2022, "5"},
  14. {2023, "6"}, {2024, "7"}, {2025, "8"}, {2026, "9"}, {2027, "A"},
  15. {2028, "B"}, {2029, "C"}, {2030, "D"}, {2031, "E"}, {2032, "F"},
  16. {2033, "G"}, {2034, "H"}, {2035, "J"}, {2036, "K"}, {2037, "L"},
  17. {2038, "M"}, {2039, "N"}, {2040, "P"}, {2041, "R"}, {2042, "S"},
  18. {2043, "T"}, {2044, "V"}, {2045, "W"}, {2046, "X"}, {2047, "Y"},
  19. {2048, "1"}, {2049, "2"}, {2050, "3"}, {2051, "4"}, {2052, "5"},
  20. {2053, "6"}, {2054, "7"}, {2055, "8"}, {2056, "9"}, {2057, "A"}
  21. };
  22. private static readonly Dictionary<int, string> monthToCode = new Dictionary<int, string>
  23. {
  24. {1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"},
  25. {7, "7"}, {8, "8"}, {9, "9"}, {10, "A"}, {11, "B"}, {12, "C"}
  26. };
  27. private static readonly Dictionary<int, string> dayToCode = new Dictionary<int, string>
  28. {
  29. {1, "1"}, {2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"}, {8, "8"}, {9, "9"},
  30. {10, "A"}, {11, "B"}, {12, "C"}, {13, "D"}, {14, "E"}, {15, "F"}, {16, "G"},
  31. {17, "H"}, {18, "J"}, {19, "K"}, {20, "L"}, {21, "M"}, {22, "N"}, {23, "P"}, {24, "R"},
  32. {25, "S"}, {26, "T"}, {27, "U"}, {28, "V"}, {29, "W"}, {30, "X"}, {31, "Y"}
  33. };
  34. private static readonly Dictionary<string, int> codeToYear;
  35. private static readonly Dictionary<string, int> codeToMonth;
  36. private static readonly Dictionary<string, int> codeToDay;
  37. static DateCodeConverter()
  38. {
  39. codeToYear = yearToCode
  40. .GroupBy(kv => kv.Value)
  41. .ToDictionary(g => g.Key, g => g.Last().Key);
  42. codeToMonth = monthToCode
  43. .ToDictionary(kv => kv.Value, kv => kv.Key);
  44. codeToDay = dayToCode
  45. .ToDictionary(kv => kv.Value, kv => kv.Key);
  46. }
  47. public static string GetYearCode(int year)
  48. {
  49. return yearToCode.TryGetValue(year, out string code) ? code : "未知年份";
  50. }
  51. public static int? GetYearByCode(string code)
  52. {
  53. return codeToYear.TryGetValue(code, out int year) ? year : (int?)null;
  54. }
  55. public static List<int> GetAllYearsByCode(string code)
  56. {
  57. return yearToCode
  58. .Where(kv => kv.Value == code)
  59. .Select(kv => kv.Key)
  60. .ToList();
  61. }
  62. public static string GetMonthCode(int month)
  63. {
  64. if (month < 1 || month > 12)
  65. return "无效月份";
  66. return monthToCode.TryGetValue(month, out string code) ? code : "未知月份";
  67. }
  68. public static int? GetMonthByCode(string code)
  69. {
  70. return codeToMonth.TryGetValue(code, out int month) ? month : (int?)null;
  71. }
  72. public static string GetDayCode(int day)
  73. {
  74. if (day < 1 || day > 31)
  75. return "无效日期";
  76. return dayToCode.TryGetValue(day, out string code) ? code : "未知日期";
  77. }
  78. public static int? GetDayByCode(string code)
  79. {
  80. return codeToDay.TryGetValue(code, out int day) ? day : (int?)null;
  81. }
  82. public static string ConvertToDateCode(DateTime date)
  83. {
  84. string yearCode = GetYearCode(date.Year);
  85. string monthCode = GetMonthCode(date.Month);
  86. string dayCode = GetDayCode(date.Day);
  87. if (yearCode == "未知年份" || monthCode == "无效月份" || dayCode == "无效日期" || dayCode == "未知日期")
  88. {
  89. return "无效日期";
  90. }
  91. return $"{yearCode}{monthCode}{dayCode}";
  92. }
  93. public static DateTime? ConvertFromDateCode(string dateCode)
  94. {
  95. if (string.IsNullOrEmpty(dateCode) || dateCode.Length != 3)
  96. return null;
  97. string yearCode = dateCode.Substring(0, 1);
  98. string monthCode = dateCode.Substring(1, 1);
  99. string dayCode = dateCode.Substring(2, 1);
  100. var year = GetYearByCode(yearCode);
  101. var month = GetMonthByCode(monthCode);
  102. var day = GetDayByCode(dayCode);
  103. if (!year.HasValue || !month.HasValue || !day.HasValue)
  104. return null;
  105. try
  106. {
  107. return new DateTime(year.Value, month.Value, day.Value);
  108. }
  109. catch (ArgumentOutOfRangeException)
  110. {
  111. return null;
  112. }
  113. }
  114. public static List<DateTime> ConvertAllFromDateCode(string dateCode)
  115. {
  116. var result = new List<DateTime>();
  117. if (string.IsNullOrEmpty(dateCode) || dateCode.Length != 3)
  118. return result;
  119. string yearCode = dateCode.Substring(0, 1);
  120. string monthCode = dateCode.Substring(1, 1);
  121. string dayCode = dateCode.Substring(2, 1);
  122. var allYears = GetAllYearsByCode(yearCode);
  123. var month = GetMonthByCode(monthCode);
  124. var day = GetDayByCode(dayCode);
  125. if (allYears.Count == 0 || !month.HasValue || !day.HasValue)
  126. return result;
  127. foreach (var year in allYears)
  128. {
  129. try
  130. {
  131. result.Add(new DateTime(year, month.Value, day.Value));
  132. }
  133. catch (ArgumentOutOfRangeException)
  134. {
  135. }
  136. }
  137. return result;
  138. }
  139. }
  140. }