Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added the deserialize fix and test for the UTC DateTime #613

Merged
merged 3 commits into from
Jun 12, 2021

Conversation

gjdonkers
Copy link
Contributor

Deserialize the datetime to correct timezone as described on the Microsoft documentation for the "o" Roundtrip

// The example displays the following output:
//    6/15/2009 1:45:30 PM (Unspecified) --> 2009-06-15T13:45:30.0000000
//    6/15/2009 1:45:30 PM (Utc) --> 2009-06-15T13:45:30.0000000Z
//    6/15/2009 1:45:30 PM (Local) --> 2009-06-15T13:45:30.0000000-07:00

https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-date-and-time-format-strings#Roundtrip

value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture);
var style = scalar.Value.EndsWith("Z")
? DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal
: scalar.Value.Length > 27 ? DateTimeStyles.AssumeLocal : DateTimeStyles.None;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Testing the length seems a bit fragile. Maybe a regex would be more robust?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have the skills to create a good regex for this, so I don't know if it would be more robust. The previous implementation is still the same, DateTimeStyles.None is the default. And the 'o' roundtrip is compliant with the ISO 8601, so I thought this won't break other implementations but would only add the correct TimeZone if this is known.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you are a busy man at the moment but is there a chance we can resolve this? What kind of regex are you thinking of?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, I haven't had much free time. I made some experiments and it seems that instead of forcing the adjustment, you can simply pass the DateTimeStyles.RoundtripKind value to the style and you will get the correct kind. Have you tried that instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah.. I've overlooked that one, that is definitely a better solution.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can always use the DateTimeStyles.RoundtripKind, without any test:

value = DateTime.Parse(scalar.Value, CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind);

@aaubry
Copy link
Owner

aaubry commented Jun 12, 2021

Thanks for this and sorry about the slow response time.

@aaubry aaubry merged commit 3bea68e into aaubry:master Jun 12, 2021
@gjdonkers
Copy link
Contributor Author

No problem. I’m glad it’s fixed.

@gjdonkers
Copy link
Contributor Author

One question, when do you think this will be released to the latest nuget?

@aaubry
Copy link
Owner

aaubry commented Jun 13, 2021

This feature has been released in version 11.2.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants