Enhanced the UnixDateTimeConverter to... #3248
Closed
+45
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Enhanced the UnixDateTimeConverter to handle values that are milliseconds since Unix Epoch, in addition to normal values (seconds since Unix Epoch).
Why?
In #3157 there are multiple reports (stack traces) of the UnixDateTimeConverter being responsible for incorrect values being put into the method System.DateTime.AddSeconds(Double value).
The root cause is Stripe API responses sometimes return
longvalues that are the milliseconds since Unix Epoch, instead of the seconds since Unix Epoch (typical). I was able to determine this by debugging through the code during a request in a Sandbox - I found that some values (in the responses from the Stripe API) that were being converted by the UnixDateTimeConverter were larger than 253402300799. I noted some of these values, and converted these long values using DateTimeOffset.FromUnixTimeMilliseconds(long milliseconds), and found that the values converted nicely to dates very close to my requests (or data in my requests).In the few requests that I debugged through, most Unix Epoch values are seconds; unfortunately one was milliseconds.
What?
This PR enhances the UnixDateTimeConverter, so that it can automatically handle both types of Unix Epoch values that the Stripe API returns:
It uses a the value 253402300799 as the maximum allowable value for seconds since Unix Epoch, and presumes all values greater than this are milliseconds since Unix Epoch.
Two new unit tests were created for the UnixDateTimeConverter to verify it can handle both cases correctly.
See Also
See my post on Issue 3157 showing the stack trace when the UnixDateTimeConverter.cs attempts to convert values that are milliseconds since Unix Epoch.