Skip to content

Commit c54414a

Browse files
committed
added UTC-compatible time zone coercion to null
1 parent 0f0c85e commit c54414a

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Asn1Parser/Utils/DateTimeUtils.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +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);
2223
if (zone == null) {
2324
preValue = time.ToUniversalTime().ToString(format) + suffix + "Z";
2425
} else {
@@ -46,7 +47,15 @@ public static DateTime Decode(Asn1Reader asn, out TimeZoneInfo? zone) {
4647

4748
return extractDateTime(SB.ToString(), out zone);
4849
}
49-
50+
51+
static TimeZoneInfo? coerceTimeZone(TimeZoneInfo? zone) {
52+
// if zone is explicitly specified, but its offset against UTC is zero, we do not encode zone.
53+
if ((zone?.BaseUtcOffset.TotalMinutes ?? 0) == 0) {
54+
return null;
55+
}
56+
57+
return zone;
58+
}
5059
static DateTime extractDateTime(String strValue, out TimeZoneInfo? zone) {
5160
zone = null;
5261
Boolean hasZone = extractZoneShift(strValue, out Int32 hours, out Int32 minutes, out Int32 zoneDelimiter);

tests/Asn1Parser.Tests/Asn1DateTimeTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public class Asn1DateTimeTests {
1313
public void TestZuluSimple() {
1414
var dt = DateTime.ParseExact("2024-08-07 16:12:37", "yyyy-MM-dd HH:mm:ss", null);
1515
var gt = new Asn1GeneralizedTime(dt);
16+
Assert.AreEqual(DateTimeKind.Local, gt.Value.Kind);
1617
assertDateTimeEncode(Asn1Type.GeneralizedTime, gt, dt, "yyyyMMddHHmmssZ");
1718
}
1819
[TestMethod, Description("Test date/time with fractions and fraction is zero")]

0 commit comments

Comments
 (0)