Skip to content

Commit 6019541

Browse files
committed
refactored ASN.1 date/time types by moving all the functionality into base class.
1 parent cc0281d commit 6019541

File tree

3 files changed

+58
-65
lines changed

3 files changed

+58
-65
lines changed

Asn1Parser/Universal/Asn1DateTime.cs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Globalization;
3+
using SysadminsLV.Asn1Parser.Utils;
24

35
namespace SysadminsLV.Asn1Parser.Universal;
46

@@ -9,24 +11,60 @@ public abstract class Asn1DateTime : Asn1Universal {
911
/// <summary>
1012
/// Initializes a new instance of <strong>Asn1DateTime</strong> class.
1113
/// </summary>
12-
protected Asn1DateTime(Asn1Type type) : base(type) { }
14+
protected Asn1DateTime(Asn1Type type) : base(type) {
15+
if (type is not (Asn1Type.UTCTime or Asn1Type.GeneralizedTime)) {
16+
throw new ArgumentException("Invalid ASN type. Must be either, UTCTime or GeneralizedTime.");
17+
}
18+
}
1319
/// <summary>
1420
/// Initializes a new instance of <strong>Asn1DateTime</strong> class from an existing
1521
/// <see cref="Asn1Reader"/> object.
1622
/// </summary>
1723
/// <param name="asn"><see cref="Asn1Reader"/> object in the position that represents ASN.1 date/time object.</param>
1824
/// <param name="type">Optional expected ASN.1 type.</param>
19-
protected Asn1DateTime(Asn1Reader asn, Asn1Type? type) : base(asn, type) { }
25+
protected Asn1DateTime(Asn1Reader asn, Asn1Type type) : base(asn, type) {
26+
if (type is not (Asn1Type.UTCTime or Asn1Type.GeneralizedTime)) {
27+
throw new ArgumentException("Invalid ASN type. Must be either, UTCTime or GeneralizedTime.");
28+
}
29+
m_decode(asn.GetTagRawData());
30+
}
31+
protected Asn1DateTime(Asn1Type type, DateTime time, TimeZoneInfo? zone = null, Boolean preciseTime = false) : this(type) {
32+
m_encode(type, time, zone, preciseTime);
33+
}
2034

2135
/// <summary>
22-
/// Gets the time zone information for the current object.
36+
/// Gets the optional time zone information for the current object. Local time zone is assumed if value is null.
2337
/// </summary>
24-
public TimeZoneInfo ZoneInfo { get; protected set; }
38+
public TimeZoneInfo? ZoneInfo { get; protected set; }
2539
/// <summary>
26-
/// Gets date/time value associated with the current date/time object.
40+
/// Gets date/time value associated with the current date/time object and adjusted to local time zone.
2741
/// </summary>
2842
public DateTime Value { get; protected set; }
2943

44+
void m_encode(Asn1Type type, DateTime time, TimeZoneInfo? zone, Boolean preciseTime) {
45+
time = zone == null
46+
? DateTime.SpecifyKind(time, DateTimeKind.Local)
47+
: TimeZoneInfo.ConvertTimeToUtc(time, zone).ToLocalTime();
48+
Value = time;
49+
ZoneInfo = zone;
50+
Boolean utcTime = type == Asn1Type.UTCTime;
51+
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, utcTime, preciseTime), type)));
52+
}
53+
void m_decode(Byte[] rawData) {
54+
var asn = new Asn1Reader(rawData);
55+
Initialize(asn);
56+
Value = DateTimeUtils.Decode(asn, out TimeZoneInfo zoneInfo);
57+
ZoneInfo = zoneInfo;
58+
}
59+
60+
/// <summary>
61+
/// Gets decoded date/time string value.
62+
/// </summary>
63+
/// <returns>Decoded date/time string value.</returns>
64+
public override String GetDisplayValue() {
65+
return Value.ToString(CultureInfo.InvariantCulture);
66+
}
67+
3068
/// <summary>
3169
/// Encodes a .NET DateTime object to a ASN.1-encoded byte array. This method is designed to conform
3270
/// <see href="http://tools.ietf.org/html/rfc5280">RFC 5280</see> requirement, so dates before 1950 and
Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Globalization;
3-
using SysadminsLV.Asn1Parser.Utils;
42

53
namespace SysadminsLV.Asn1Parser.Universal;
64

@@ -14,7 +12,7 @@ public sealed class Asn1GeneralizedTime : Asn1DateTime {
1412
/// Initializes a new instance of the <strong>Asn1GeneralizedTime</strong> class from a date time object
1513
/// to encode and value that indicates whether to include millisecond information.
1614
/// </summary>
17-
/// <param name="time">A <see cref="DateTime"/> object.</param>
15+
/// <param name="time">A <see cref="DateTime"/> object in local time zone.</param>
1816
/// <param name="preciseTime">
1917
/// <strong>True</strong> if encoded value should contain millisecond information, otherwise <strong>False</strong>.
2018
/// </param>
@@ -23,14 +21,15 @@ public Asn1GeneralizedTime(DateTime time, Boolean preciseTime) : this(time, null
2321
/// Initializes a new instance of the <strong>Asn1GeneralizedTime</strong> class from a date time object
2422
/// to encode, time zone information and value that indicates whether to include millisecond information.
2523
/// </summary>
26-
/// <param name="time">A <see cref="DateTime"/> object.</param>
24+
/// <param name="time">
25+
/// A <see cref="DateTime"/> object in destination time zone if zone information is provided.
26+
/// Local time zone is assumed if zone information is not provided (null).
27+
/// </param>
2728
/// <param name="zone">A <see cref="TimeZoneInfo"/> object that represents time zone information.</param>
2829
/// <param name="preciseTime">
2930
/// <strong>True</strong> if encoded value should contain millisecond information, otherwise <strong>False</strong>.
3031
/// </param>
31-
public Asn1GeneralizedTime(DateTime time, TimeZoneInfo zone = null, Boolean preciseTime = false) : base(TYPE) {
32-
m_encode(time, zone, preciseTime);
33-
}
32+
public Asn1GeneralizedTime(DateTime time, TimeZoneInfo? zone = null, Boolean preciseTime = false) : base(TYPE, time, zone, preciseTime) { }
3433
/// <summary>
3534
/// Initializes a new instance of the <strong>Asn1GeneralizedTime</strong> class from an existing
3635
/// <see cref="Asn1Reader"/> object.
@@ -39,9 +38,7 @@ public Asn1GeneralizedTime(DateTime time, TimeZoneInfo zone = null, Boolean prec
3938
/// <exception cref="Asn1InvalidTagException">
4039
/// The current state of <strong>ASN1</strong> object is not Generalized Time.
4140
/// </exception>
42-
public Asn1GeneralizedTime(Asn1Reader asn) : base(asn, TYPE) {
43-
m_decode(asn.GetTagRawData());
44-
}
41+
public Asn1GeneralizedTime(Asn1Reader asn) : base(asn, TYPE) { }
4542
/// <summary>
4643
/// Initializes a new instance of the <strong>Asn1GeneralizedTime</strong> class from a byte array that
4744
/// represents encoded UTC time.
@@ -51,21 +48,4 @@ public Asn1GeneralizedTime(Asn1Reader asn) : base(asn, TYPE) {
5148
/// The current state of <strong>ASN1</strong> object is not Generalized Time.
5249
/// </exception>
5350
public Asn1GeneralizedTime(Byte[] rawData) : this(new Asn1Reader(rawData)) { }
54-
55-
void m_encode(DateTime time, TimeZoneInfo zone, Boolean preciseTime) {
56-
Value = time;
57-
ZoneInfo = zone;
58-
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, false, preciseTime), TYPE)));
59-
}
60-
void m_decode(Byte[] rawData) {
61-
var asn = new Asn1Reader(rawData);
62-
Initialize(asn);
63-
Value = DateTimeUtils.Decode(asn, out TimeZoneInfo zoneInfo);
64-
ZoneInfo = zoneInfo;
65-
}
66-
67-
/// <inheritdoc/>
68-
public override String GetDisplayValue() {
69-
return Value.ToString(CultureInfo.InvariantCulture);
70-
}
7151
}
Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
using System;
2-
using System.Globalization;
3-
using SysadminsLV.Asn1Parser.Utils;
42

53
namespace SysadminsLV.Asn1Parser.Universal;
64

@@ -14,7 +12,7 @@ public sealed class Asn1UtcTime : Asn1DateTime {
1412
/// Initializes a new instance of the <strong>Asn1UtcTime</strong> class from a date time object
1513
/// to encode and value that indicates whether to include millisecond information.
1614
/// </summary>
17-
/// <param name="time">A <see cref="DateTime"/> object.</param>
15+
/// <param name="time">A <see cref="DateTime"/> object in local time zone.</param>
1816
/// <param name="preciseTime">
1917
/// <strong>True</strong> if encoded value should contain millisecond information, otherwise <strong>False</strong>.
2018
/// </param>
@@ -23,14 +21,15 @@ public Asn1UtcTime(DateTime time, Boolean preciseTime) : this(time, null, precis
2321
/// Initializes a new instance of the <strong>Asn1UtcTime</strong> class from a date time object
2422
/// to encode, time zone information and value that indicates whether to include millisecond information.
2523
/// </summary>
26-
/// <param name="time">A <see cref="DateTime"/> object.</param>
24+
/// <param name="time">
25+
/// A <see cref="DateTime"/> object in destination time zone if zone information is provided.
26+
/// Local time zone is assumed if zone information is not provided (null).
27+
/// </param>
2728
/// <param name="zone">A <see cref="TimeZoneInfo"/> object that represents time zone information.</param>
2829
/// <param name="preciseTime">
2930
/// <strong>True</strong> if encoded value should contain millisecond information, otherwise <strong>False</strong>.
3031
/// </param>
31-
public Asn1UtcTime(DateTime time, TimeZoneInfo zone = null, Boolean preciseTime = false) : base(TYPE) {
32-
m_encode(time, zone, preciseTime);
33-
}
32+
public Asn1UtcTime(DateTime time, TimeZoneInfo? zone = null, Boolean preciseTime = false) : base(TYPE, time, zone, preciseTime) { }
3433
/// <summary>
3534
/// Initializes a new instance of the <strong>Asn1UtcTime</strong> class from an existing
3635
/// <see cref="Asn1Reader"/> object.
@@ -39,9 +38,7 @@ public Asn1UtcTime(DateTime time, TimeZoneInfo zone = null, Boolean preciseTime
3938
/// <exception cref="Asn1InvalidTagException">
4039
/// The current state of <strong>ASN1</strong> object is not UTC time.
4140
/// </exception>
42-
public Asn1UtcTime(Asn1Reader asn) : base(asn, TYPE) {
43-
m_decode(asn.GetTagRawData());
44-
}
41+
public Asn1UtcTime(Asn1Reader asn) : base(asn, TYPE) { }
4542
/// <summary>
4643
/// Initializes a new instance of the <strong>Asn1UtcTime</strong> class from a byte array that
4744
/// represents encoded UTC time.
@@ -50,27 +47,5 @@ public Asn1UtcTime(Asn1Reader asn) : base(asn, TYPE) {
5047
/// <exception cref="Asn1InvalidTagException">
5148
/// The current state of <strong>ASN1</strong> object is not UTC time.
5249
/// </exception>
53-
public Asn1UtcTime(Byte[] rawData) : base(new Asn1Reader(rawData), TYPE) {
54-
m_decode(rawData);
55-
}
56-
57-
void m_encode(DateTime time, TimeZoneInfo zone, Boolean preciseTime) {
58-
Value = time;
59-
ZoneInfo = zone;
60-
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, true, preciseTime), TYPE)));
61-
}
62-
void m_decode(Byte[] rawData) {
63-
var asn = new Asn1Reader(rawData);
64-
Initialize(asn);
65-
Value = DateTimeUtils.Decode(asn, out TimeZoneInfo zoneInfo);
66-
ZoneInfo = zoneInfo;
67-
}
68-
69-
/// <summary>
70-
/// Gets decoded date/time string value.
71-
/// </summary>
72-
/// <returns>Decoded date/time string value.</returns>
73-
public override String GetDisplayValue() {
74-
return Value.ToString(CultureInfo.InvariantCulture);
75-
}
50+
public Asn1UtcTime(Byte[] rawData) : base(new Asn1Reader(rawData), TYPE) { }
7651
}

0 commit comments

Comments
 (0)