diff --git a/CryptoExchange.Net/Converters/SystemTextJson/DateTimeConverter.cs b/CryptoExchange.Net/Converters/SystemTextJson/DateTimeConverter.cs index e491add..b88ce98 100644 --- a/CryptoExchange.Net/Converters/SystemTextJson/DateTimeConverter.cs +++ b/CryptoExchange.Net/Converters/SystemTextJson/DateTimeConverter.cs @@ -14,8 +14,8 @@ namespace CryptoExchange.Net.Converters.SystemTextJson { private static readonly DateTime _epoch = new(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); private const long _ticksPerSecond = TimeSpan.TicksPerMillisecond * 1000; - private const double _ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000d; - private const double _ticksPerNanosecond = TimeSpan.TicksPerMillisecond / 1000d / 1000; + private const decimal _ticksPerMicrosecond = TimeSpan.TicksPerMillisecond / 1000m; + private const decimal _ticksPerNanosecond = TimeSpan.TicksPerMillisecond / 1000m / 1000; /// public override bool CanConvert(Type typeToConvert) @@ -45,11 +45,11 @@ namespace CryptoExchange.Net.Converters.SystemTextJson if (reader.TokenType is JsonTokenType.Number) { - var longValue = reader.GetDouble(); - if (longValue == 0 || longValue < 0) + var decValue = reader.GetDecimal(); + if (decValue == 0 || decValue < 0) return default; - return ParseFromDouble(longValue); + return ParseFromDecimal(decValue); } else if (reader.TokenType is JsonTokenType.String) { @@ -57,7 +57,7 @@ namespace CryptoExchange.Net.Converters.SystemTextJson if (string.IsNullOrWhiteSpace(stringValue) || stringValue == "-1" || stringValue == "0001-01-01T00:00:00Z" - || double.TryParse(stringValue, out var doubleVal) && doubleVal == 0) + || decimal.TryParse(stringValue, out var decVal) && decVal == 0) { return default; } @@ -90,18 +90,22 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// Parse a long value to datetime /// - /// - /// - public static DateTime ParseFromDouble(double longValue) - { - if (longValue < 19999999999) - return ConvertFromSeconds(longValue); - if (longValue < 19999999999999) - return ConvertFromMilliseconds(longValue); - if (longValue < 19999999999999999) - return ConvertFromMicroseconds(longValue); + public static DateTime ParseFromDouble(double value) + => ParseFromDecimal((decimal)value); - return ConvertFromNanoseconds(longValue); + /// + /// Parse a long value to datetime + /// + public static DateTime ParseFromDecimal(decimal value) + { + if (value < 19999999999) + return ConvertFromSeconds(value); + if (value < 19999999999999) + return ConvertFromMilliseconds(value); + if (value < 19999999999999999) + return ConvertFromMicroseconds(value); + + return ConvertFromNanoseconds(value); } /// @@ -152,19 +156,19 @@ namespace CryptoExchange.Net.Converters.SystemTextJson return new DateTime(year + 2000, month, day, 0, 0, 0, DateTimeKind.Utc); } - if (double.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var doubleValue)) + if (decimal.TryParse(stringValue, NumberStyles.Float, CultureInfo.InvariantCulture, out var decimalValue)) { // Parse 1637745563.000 format - if (doubleValue <= 0) + if (decimalValue <= 0) return default; - if (doubleValue < 19999999999) - return ConvertFromSeconds(doubleValue); - if (doubleValue < 19999999999999) - return ConvertFromMilliseconds((long)doubleValue); - if (doubleValue < 19999999999999999) - return ConvertFromMicroseconds((long)doubleValue); + if (decimalValue < 19999999999) + return ConvertFromSeconds(decimalValue); + if (decimalValue < 19999999999999) + return ConvertFromMilliseconds(decimalValue); + if (decimalValue < 19999999999999999) + return ConvertFromMicroseconds(decimalValue); - return ConvertFromNanoseconds((long)doubleValue); + return ConvertFromNanoseconds(decimalValue); } if (stringValue.Length == 10) @@ -188,54 +192,54 @@ namespace CryptoExchange.Net.Converters.SystemTextJson /// /// Convert a seconds since epoch (01-01-1970) value to DateTime /// - /// - /// - public static DateTime ConvertFromSeconds(double seconds) => _epoch.AddTicks((long)Math.Round(seconds * _ticksPerSecond)); - /// - /// Convert a milliseconds since epoch (01-01-1970) value to DateTime - /// - /// - /// - public static DateTime ConvertFromMilliseconds(double milliseconds) => _epoch.AddTicks((long)Math.Round(milliseconds * TimeSpan.TicksPerMillisecond)); - /// - /// Convert a microseconds since epoch (01-01-1970) value to DateTime - /// - /// - /// - public static DateTime ConvertFromMicroseconds(double microseconds) => _epoch.AddTicks((long)Math.Round(microseconds * _ticksPerMicrosecond)); + public static DateTime ConvertFromSeconds(decimal seconds) => _epoch.AddTicks((long)Math.Round(seconds * _ticksPerSecond)); /// /// Convert a nanoseconds since epoch (01-01-1970) value to DateTime /// - /// - /// - public static DateTime ConvertFromNanoseconds(double nanoseconds) => _epoch.AddTicks((long)Math.Round(nanoseconds * _ticksPerNanosecond)); + public static DateTime ConvertFromSeconds(double seconds) => ConvertFromSeconds((decimal)seconds); + /// + /// Convert a milliseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromMilliseconds(decimal milliseconds) => _epoch.AddTicks((long)Math.Round(milliseconds * TimeSpan.TicksPerMillisecond)); + /// + /// Convert a nanoseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromMilliseconds(double milliseconds) => ConvertFromMilliseconds((decimal)milliseconds); + /// + /// Convert a microseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromMicroseconds(decimal microseconds) => _epoch.AddTicks((long)Math.Round(microseconds * _ticksPerMicrosecond)); + /// + /// Convert a nanoseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromMicroseconds(double microseconds) => ConvertFromMicroseconds((decimal)microseconds); + /// + /// Convert a nanoseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromNanoseconds(decimal nanoseconds) => _epoch.AddTicks((long)Math.Round(nanoseconds * _ticksPerNanosecond)); + /// + /// Convert a nanoseconds since epoch (01-01-1970) value to DateTime + /// + public static DateTime ConvertFromNanoseconds(double nanoseconds) => ConvertFromNanoseconds((decimal)nanoseconds); /// /// Convert a DateTime value to seconds since epoch (01-01-1970) value /// - /// - /// [return: NotNullIfNotNull("time")] public static long? ConvertToSeconds(DateTime? time) => time == null ? null : (long)Math.Round((time.Value - _epoch).TotalSeconds); /// /// Convert a DateTime value to milliseconds since epoch (01-01-1970) value /// - /// - /// [return: NotNullIfNotNull("time")] public static long? ConvertToMilliseconds(DateTime? time) => time == null ? null : (long)Math.Round((time.Value - _epoch).TotalMilliseconds); /// /// Convert a DateTime value to microseconds since epoch (01-01-1970) value /// - /// - /// [return: NotNullIfNotNull("time")] public static long? ConvertToMicroseconds(DateTime? time) => time == null ? null : (long)Math.Round((time.Value - _epoch).Ticks / _ticksPerMicrosecond); /// /// Convert a DateTime value to nanoseconds since epoch (01-01-1970) value /// - /// - /// [return: NotNullIfNotNull("time")] public static long? ConvertToNanoseconds(DateTime? time) => time == null ? null : (long)Math.Round((time.Value - _epoch).Ticks / _ticksPerNanosecond); }