JapaneseCalendar.Unix.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. namespace System.Globalization
  7. {
  8. public partial class JapaneseCalendar : Calendar
  9. {
  10. private static EraInfo[] GetJapaneseEras()
  11. {
  12. if (GlobalizationMode.Invariant)
  13. {
  14. return null;
  15. }
  16. string[] eraNames;
  17. if (!CalendarData.EnumCalendarInfo("ja-JP", CalendarId.JAPAN, CalendarDataType.EraNames, out eraNames))
  18. {
  19. return null;
  20. }
  21. string[] abbrevEnglishEraNames;
  22. if (!CalendarData.EnumCalendarInfo("en", CalendarId.JAPAN, CalendarDataType.AbbrevEraNames, out abbrevEnglishEraNames))
  23. {
  24. return null;
  25. }
  26. List<EraInfo> eras = new List<EraInfo>();
  27. int lastMaxYear = GregorianCalendar.MaxYear;
  28. int latestEra = Interop.Globalization.GetLatestJapaneseEra();
  29. for (int i = latestEra; i >= 0; i--)
  30. {
  31. DateTime dt;
  32. if (!GetJapaneseEraStartDate(i, out dt))
  33. {
  34. return null;
  35. }
  36. if (dt < JapaneseCalendar.calendarMinValue)
  37. {
  38. // only populate the Eras that are valid JapaneseCalendar date times
  39. break;
  40. }
  41. eras.Add(new EraInfo(i, dt.Year, dt.Month, dt.Day, dt.Year - 1, 1, lastMaxYear - dt.Year + 1,
  42. eraNames[i], GetAbbreviatedEraName(eraNames, i), abbrevEnglishEraNames[i]));
  43. lastMaxYear = dt.Year;
  44. }
  45. // remap the Era numbers, now that we know how many there will be
  46. for (int i = 0; i < eras.Count; i++)
  47. {
  48. eras[i].era = eras.Count - i;
  49. }
  50. return eras.ToArray();
  51. }
  52. // PAL Layer ends here
  53. private static string GetAbbreviatedEraName(string[] eraNames, int eraIndex)
  54. {
  55. // This matches the behavior on Win32 - only returning the first character of the era name.
  56. // See Calendar.EraAsString(Int32) - https://msdn.microsoft.com/en-us/library/windows/apps/br206751.aspx
  57. return eraNames[eraIndex].Substring(0, 1);
  58. }
  59. private static bool GetJapaneseEraStartDate(int era, out DateTime dateTime)
  60. {
  61. Debug.Assert(!GlobalizationMode.Invariant);
  62. dateTime = default(DateTime);
  63. int startYear;
  64. int startMonth;
  65. int startDay;
  66. bool result = Interop.Globalization.GetJapaneseEraStartDate(
  67. era,
  68. out startYear,
  69. out startMonth,
  70. out startDay);
  71. if (result)
  72. {
  73. dateTime = new DateTime(startYear, startMonth, startDay);
  74. }
  75. return result;
  76. }
  77. }
  78. }