Skip to content

Commit a2e4068

Browse files
committed
addressed date/time fraction encoding (#18)
1 parent 6019541 commit a2e4068

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

Asn1Parser/Utils/DateTimeUtils.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
using System;
2+
using System.Globalization;
23
using System.Linq;
34
using System.Text;
45

56
namespace SysadminsLV.Asn1Parser.Utils;
67

78
static class DateTimeUtils {
8-
9-
public static Byte[] Encode(DateTime time, TimeZoneInfo zone, Boolean UTC, Boolean usePrecise) {
9+
public static Byte[] Encode(DateTime time, TimeZoneInfo? zone, Boolean UTC, Boolean usePrecise) {
1010
String suffix = String.Empty;
1111
String preValue;
1212
String format = UTC
1313
? UTCFormat
1414
: GtFormat;
1515
if (usePrecise) {
16-
suffix += $".{time.Millisecond:D3}";
16+
// encode milliseconds using minimum bytes, i.e. do not encode trailing zeros
17+
// in worst case, when milliseconds is zero, then omit entire fraction despite
18+
// it was requested. See ITU-T X.690, section 11.7
19+
suffix += (time.Millisecond / 1000d).ToString(CultureInfo.InvariantCulture).Substring(1);
1720
}
1821
if (zone == null) {
1922
preValue = time.ToUniversalTime().ToString(format) + suffix + "Z";
@@ -34,7 +37,7 @@ public static Byte[] Encode(DateTime time, TimeZoneInfo zone, Boolean UTC, Boole
3437
return rawData;
3538
}
3639
// rawData is pure value without header
37-
public static DateTime Decode(Asn1Reader asn, out TimeZoneInfo zone) {
40+
public static DateTime Decode(Asn1Reader asn, out TimeZoneInfo? zone) {
3841
var SB = new StringBuilder();
3942
for (Int32 i = asn.PayloadStartOffset; i < asn.PayloadStartOffset + asn.PayloadLength; i++) {
4043
SB.Append(Convert.ToChar(asn[i]));

0 commit comments

Comments
 (0)