11using System ;
2+ using System . Globalization ;
23using System . Linq ;
34using System . Text ;
45
56namespace SysadminsLV . Asn1Parser . Utils ;
67
78static 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