1
0
mirror of https://github.com/JKorf/CryptoExchange.Net synced 2025-10-27 08:27:19 +00:00

Compare commits

..

No commits in common. "61130ef54e670f99e95f33c342e4789a8270d676" and "d433ff7475c578d0fff8896565b4457145ff5e3e" have entirely different histories.

View File

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