fix: Use double precision for X-Reset-After, set CultureInfo when parsing numeric types (#1375)
* Parse double for X-Reset-After instead of float, needs more precision Float did not contain enough precision to store the millisecond unix time value, which resulted in the second and millisecond values being slightly off. This can be easily tested using: ```cs > DateTimeOffset.FromUnixTimeMilliseconds((long)(1470173022.123f * 1000)).Millisecond 160 // wrong > DateTimeOffset.FromUnixTimeMilliseconds((long)(1470173022.123 * 1000)).Millisecond 123 // correct ``` * Parse RateLimit-Reset using an IFormatProvider * State NumberStyle and use CultureInfo.InvariantCulture for any parsing This updates most occurances in the code where a Parse or TryParse method was used to explicitly state the NumberStyle, and to use CultureInfo.InvariantCulture. CultureInfo was used over NumberInfo, as it also works on DateTime parsing too. * Use default format spec in Commands
This commit is contained in:
committed by
Christopher F
parent
3755a027b3
commit
606dac3e1a
@@ -1,6 +1,7 @@
|
||||
using Discord.API;
|
||||
using Discord.API;
|
||||
using Newtonsoft.Json;
|
||||
using System;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Discord.Net.Converters
|
||||
{
|
||||
@@ -23,7 +24,7 @@ namespace Discord.Net.Converters
|
||||
{
|
||||
case JsonToken.String:
|
||||
case JsonToken.Integer:
|
||||
return new EntityOrId<T>(ulong.Parse(reader.ReadAsString()));
|
||||
return new EntityOrId<T>(ulong.Parse(reader.ReadAsString(), NumberStyles.None, CultureInfo.InvariantCulture));
|
||||
default:
|
||||
T obj;
|
||||
if (_innerConverter != null)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
|
||||
namespace Discord.Net
|
||||
{
|
||||
@@ -17,15 +18,15 @@ namespace Discord.Net
|
||||
IsGlobal = headers.TryGetValue("X-RateLimit-Global", out string temp) &&
|
||||
bool.TryParse(temp, out var isGlobal) && isGlobal;
|
||||
Limit = headers.TryGetValue("X-RateLimit-Limit", out temp) &&
|
||||
int.TryParse(temp, out var limit) ? limit : (int?)null;
|
||||
int.TryParse(temp, NumberStyles.None, CultureInfo.InvariantCulture, out var limit) ? limit : (int?)null;
|
||||
Remaining = headers.TryGetValue("X-RateLimit-Remaining", out temp) &&
|
||||
int.TryParse(temp, out var remaining) ? remaining : (int?)null;
|
||||
int.TryParse(temp, NumberStyles.None, CultureInfo.InvariantCulture, out var remaining) ? remaining : (int?)null;
|
||||
Reset = headers.TryGetValue("X-RateLimit-Reset", out temp) &&
|
||||
float.TryParse(temp, out var reset) ? DateTimeOffset.FromUnixTimeMilliseconds((long)(reset * 1000)) : (DateTimeOffset?)null;
|
||||
double.TryParse(temp, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var reset) ? DateTimeOffset.FromUnixTimeMilliseconds((long)(reset * 1000)) : (DateTimeOffset?)null;
|
||||
RetryAfter = headers.TryGetValue("Retry-After", out temp) &&
|
||||
int.TryParse(temp, out var retryAfter) ? retryAfter : (int?)null;
|
||||
int.TryParse(temp, NumberStyles.None, CultureInfo.InvariantCulture, out var retryAfter) ? retryAfter : (int?)null;
|
||||
Lag = headers.TryGetValue("Date", out temp) &&
|
||||
DateTimeOffset.TryParse(temp, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
|
||||
DateTimeOffset.TryParse(temp, CultureInfo.InvariantCulture, DateTimeStyles.None, out var date) ? DateTimeOffset.UtcNow - date : (TimeSpan?)null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user