JapaneseCalendar.WinRT.cs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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.Diagnostics;
  5. using Internal.Runtime.Augments;
  6. namespace System.Globalization
  7. {
  8. public partial class JapaneseCalendar : Calendar
  9. {
  10. private static EraInfo[] GetJapaneseEras()
  11. {
  12. int erasCount = WinRTInterop.Callbacks.GetJapaneseEraCount();
  13. if (erasCount < 4)
  14. {
  15. return null;
  16. }
  17. EraInfo[] eras = new EraInfo[erasCount];
  18. int lastMaxYear = GregorianCalendar.MaxYear;
  19. for (int i = erasCount; i > 0; i--)
  20. {
  21. DateTimeOffset dateOffset;
  22. string eraName;
  23. string abbreviatedEraName;
  24. if (!GetJapaneseEraInfo(i, out dateOffset, out eraName, out abbreviatedEraName))
  25. {
  26. return null;
  27. }
  28. DateTime dt = new DateTime(dateOffset.Ticks);
  29. eras[erasCount - i] = new EraInfo(i, dt.Year, dt.Month, dt.Day, dt.Year - 1, 1, lastMaxYear - dt.Year + 1,
  30. eraName, abbreviatedEraName, GetJapaneseEnglishEraName(i)); // era #4 start year/month/day, yearOffset, minEraYear
  31. lastMaxYear = dt.Year;
  32. }
  33. return eras;
  34. }
  35. // PAL Layer ends here
  36. private static readonly string[] s_JapaneseErasEnglishNames = new string[] { "M", "T", "S", "H" };
  37. private static string GetJapaneseEnglishEraName(int era)
  38. {
  39. Debug.Assert(era > 0);
  40. return era <= s_JapaneseErasEnglishNames.Length ? s_JapaneseErasEnglishNames[era - 1] : " ";
  41. }
  42. private static bool GetJapaneseEraInfo(int era, out DateTimeOffset dateOffset, out string eraName, out string abbreviatedEraName)
  43. {
  44. return WinRTInterop.Callbacks.GetJapaneseEraInfo(era, out dateOffset, out eraName, out abbreviatedEraName);
  45. }
  46. }
  47. }