Time.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Globalization;
  4. using Org.BouncyCastle.Utilities;
  5. namespace Org.BouncyCastle.Asn1.X509
  6. {
  7. public class Time
  8. : Asn1Encodable, IAsn1Choice
  9. {
  10. private readonly Asn1Object time;
  11. public static Time GetInstance(
  12. Asn1TaggedObject obj,
  13. bool explicitly)
  14. {
  15. return GetInstance(obj.GetObject());
  16. }
  17. public Time(
  18. Asn1Object time)
  19. {
  20. if (time == null)
  21. throw new ArgumentNullException("time");
  22. if (!(time is DerUtcTime) && !(time is DerGeneralizedTime))
  23. throw new ArgumentException("unknown object passed to Time");
  24. this.time = time;
  25. }
  26. /**
  27. * creates a time object from a given date - if the date is between 1950
  28. * and 2049 a UTCTime object is Generated, otherwise a GeneralizedTime
  29. * is used.
  30. */
  31. public Time(
  32. DateTime date)
  33. {
  34. #if PORTABLE || NETFX_CORE
  35. string d = date.ToUniversalTime().ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  36. #else
  37. string d = date.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  38. #endif
  39. int year = int.Parse(d.Substring(0, 4));
  40. if (year < 1950 || year > 2049)
  41. {
  42. time = new DerGeneralizedTime(d);
  43. }
  44. else
  45. {
  46. time = new DerUtcTime(d.Substring(2));
  47. }
  48. }
  49. public static Time GetInstance(
  50. object obj)
  51. {
  52. if (obj == null || obj is Time)
  53. return (Time)obj;
  54. if (obj is DerUtcTime)
  55. return new Time((DerUtcTime)obj);
  56. if (obj is DerGeneralizedTime)
  57. return new Time((DerGeneralizedTime)obj);
  58. throw new ArgumentException("unknown object in factory: " + Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  59. }
  60. public string GetTime()
  61. {
  62. if (time is DerUtcTime)
  63. {
  64. return ((DerUtcTime) time).AdjustedTimeString;
  65. }
  66. return ((DerGeneralizedTime) time).GetTime();
  67. }
  68. /// <summary>
  69. /// Return our time as DateTime.
  70. /// </summary>
  71. /// <returns>A date time.</returns>
  72. public DateTime ToDateTime()
  73. {
  74. try
  75. {
  76. if (time is DerUtcTime)
  77. {
  78. return ((DerUtcTime)time).ToAdjustedDateTime();
  79. }
  80. else
  81. {
  82. return ((DerGeneralizedTime)time).ToDateTime();
  83. }
  84. }
  85. catch (FormatException e)
  86. {
  87. // this should never happen
  88. throw new InvalidOperationException("invalid date string: " + e.Message);
  89. }
  90. }
  91. /**
  92. * Produce an object suitable for an Asn1OutputStream.
  93. * <pre>
  94. * Time ::= CHOICE {
  95. * utcTime UTCTime,
  96. * generalTime GeneralizedTime }
  97. * </pre>
  98. */
  99. public override Asn1Object ToAsn1Object()
  100. {
  101. return time;
  102. }
  103. public override string ToString()
  104. {
  105. return GetTime();
  106. }
  107. }
  108. }
  109. #endif