Time.cs 3.5 KB

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