Skip to content

Commit 8cec2a5

Browse files
committed
fixed issue with zero-shift timezone and added unit test
1 parent 8780783 commit 8cec2a5

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

Asn1Parser/Universal/Asn1DateTime.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,14 @@ protected Asn1DateTime(Asn1Type type, DateTime time, TimeZoneInfo? zone = null,
4242
public DateTime Value { get; protected set; }
4343

4444
void m_encode(Asn1Type type, DateTime time, TimeZoneInfo? zone, Boolean preciseTime) {
45+
zone = DateTimeUtils.CoerceTimeZone(zone);
4546
time = zone == null
4647
? DateTime.SpecifyKind(time, DateTimeKind.Local)
4748
: TimeZoneInfo.ConvertTimeToUtc(time, zone).ToLocalTime();
4849
Value = time;
49-
ZoneInfo = zone;
5050
Boolean utcTime = type == Asn1Type.UTCTime;
51-
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, zone, utcTime, preciseTime), type)));
51+
Initialize(new Asn1Reader(Asn1Utils.Encode(DateTimeUtils.Encode(time, ref zone, utcTime, preciseTime), type)));
52+
ZoneInfo = zone;
5253
}
5354
void m_decode(Byte[] rawData) {
5455
var asn = new Asn1Reader(rawData);

Asn1Parser/Utils/DateTimeUtils.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
namespace SysadminsLV.Asn1Parser.Utils;
88

99
static class DateTimeUtils {
10-
public static Byte[] Encode(DateTime time, TimeZoneInfo? zone, Boolean UTC, Boolean usePrecise) {
10+
public static Byte[] Encode(DateTime time, ref TimeZoneInfo? zone, Boolean UTC, Boolean usePrecise) {
1111
String suffix = String.Empty;
1212
String preValue;
1313
String format = UTC
@@ -19,7 +19,7 @@ public static Byte[] Encode(DateTime time, TimeZoneInfo? zone, Boolean UTC, Bool
1919
// it was requested. See ITU-T X.690, section 11.7
2020
suffix += (time.Millisecond / 1000d).ToString(CultureInfo.InvariantCulture).Substring(1);
2121
}
22-
zone = coerceTimeZone(zone);
22+
zone = CoerceTimeZone(zone);
2323
if (zone == null) {
2424
preValue = time.ToUniversalTime().ToString(format) + suffix + "Z";
2525
} else {
@@ -48,7 +48,7 @@ public static DateTime Decode(Asn1Reader asn, out TimeZoneInfo? zone) {
4848
return extractDateTime(SB.ToString(), out zone);
4949
}
5050

51-
static TimeZoneInfo? coerceTimeZone(TimeZoneInfo? zone) {
51+
public static TimeZoneInfo? CoerceTimeZone(TimeZoneInfo? zone) {
5252
// if zone is explicitly specified, but its offset against UTC is zero, we do not encode zone.
5353
if ((zone?.BaseUtcOffset.TotalMinutes ?? 0) == 0) {
5454
return null;

tests/Asn1Parser.Tests/Asn1DateTimeTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ public void TestZuluSimple() {
1515
var gt = new Asn1GeneralizedTime(dt);
1616
assertDateTimeEncode(Asn1Type.GeneralizedTime, gt, dt, "yyyyMMddHHmmssZ");
1717
}
18+
[TestMethod, Description("Tests explicit zero-shift timezone, must result in Zulu time.")]
19+
public void TestUtcTimeZone() {
20+
var zone = TimeZoneInfo.FindSystemTimeZoneById("Greenwich Standard Time");
21+
var dt = DateTime.ParseExact("2024-08-07 16:12:37", "yyyy-MM-dd HH:mm:ss", null);
22+
var gt = new Asn1GeneralizedTime(dt, zone);
23+
assertDateTimeEncode(Asn1Type.GeneralizedTime, gt, dt, "yyyyMMddHHmmssZ");
24+
}
1825
[TestMethod, Description("Test date/time with fractions and fraction is zero")]
1926
public void TestZuluFraction0() {
2027
var dt = DateTime.ParseExact("2024-08-07 16:12:37.0", "yyyy-MM-dd HH:mm:ss.f", null);

0 commit comments

Comments
 (0)