Time.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.Cms
  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. string d = date.ToString("yyyyMMddHHmmss", CultureInfo.InvariantCulture) + "Z";
  36. int year = int.Parse(d.Substring(0, 4));
  37. if (year < 1950 || year > 2049)
  38. {
  39. time = new DerGeneralizedTime(d);
  40. }
  41. else
  42. {
  43. time = new DerUtcTime(d.Substring(2));
  44. }
  45. }
  46. public static Time GetInstance(
  47. object obj)
  48. {
  49. if (obj == null || obj is Time)
  50. return (Time)obj;
  51. if (obj is DerUtcTime)
  52. return new Time((DerUtcTime)obj);
  53. if (obj is DerGeneralizedTime)
  54. return new Time((DerGeneralizedTime)obj);
  55. throw new ArgumentException("unknown object in factory: " + BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.GetTypeName(obj), "obj");
  56. }
  57. public string TimeString
  58. {
  59. get
  60. {
  61. if (time is DerUtcTime)
  62. {
  63. return ((DerUtcTime)time).AdjustedTimeString;
  64. }
  65. else
  66. {
  67. return ((DerGeneralizedTime)time).GetTime();
  68. }
  69. }
  70. }
  71. public DateTime Date
  72. {
  73. get
  74. {
  75. try
  76. {
  77. if (time is DerUtcTime)
  78. {
  79. return ((DerUtcTime)time).ToAdjustedDateTime();
  80. }
  81. return ((DerGeneralizedTime)time).ToDateTime();
  82. }
  83. catch (FormatException e)
  84. {
  85. // this should never happen
  86. throw new InvalidOperationException("invalid date string: " + e.Message);
  87. }
  88. }
  89. }
  90. /**
  91. * Produce an object suitable for an Asn1OutputStream.
  92. * <pre>
  93. * Time ::= CHOICE {
  94. * utcTime UTCTime,
  95. * generalTime GeneralizedTime }
  96. * </pre>
  97. */
  98. public override Asn1Object ToAsn1Object()
  99. {
  100. return time;
  101. }
  102. }
  103. }
  104. #pragma warning restore
  105. #endif