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

System.TimeZoneInfo.ConvertTimeFromUtc bug #27526

Closed
justlearntutors opened this issue Oct 3, 2018 · 3 comments
Closed

System.TimeZoneInfo.ConvertTimeFromUtc bug #27526

justlearntutors opened this issue Oct 3, 2018 · 3 comments

Comments

@justlearntutors
Copy link

We want to convert time.

Example
Localhost
DateTimeOffset TimeZoneInfoConvert = System.TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse("10/9/2018 8:00:00 AM"), System.TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));

Correct result: 10/9/2018 10:00:00 AM +02:00

Server
DateTimeOffset TimeZoneInfoConvert = System.TimeZoneInfo.ConvertTimeFromUtc(DateTime.Parse("10/9/2018 8:00:00 AM"), System.TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard Time"));

Wrong result: 10/9/2018 10:00:00 AM +00:00

@sixlettervariables
Copy link
Contributor

ConvertTimeFromUtc returns a DateTime rather than a DateTimeOffset. Given no DateTimeStyles, the parse result will have a kind of Unspecified. The implicit conversion to a DateTimeOffset from a DateTime of kind Local or Unspecified uses your local time zone offset. It seems that your server's time zone is UTC while your local machine's time zone is W. Europe Standard Time. You could confirm that by examining TimeZoneInfo.Local.BaseUtcOffset on both systems.

@Clockwork-Muse
Copy link
Contributor

To be clear, almost certainly what you want to be doing is something similar to:

// Because that date format is culture specific!
var parsedUtc = DateTimeOffset.Parse("2018-10-09 08:00:00", CultureInfo.InvariantCulture);
DateTimeOffset TimeZoneInfoConvert = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(parsedUtc, "W. Europe Standard Time");

...or you can use NodaTime, and use the appropriate domain types.

@tarekgh
Copy link
Member

tarekgh commented Oct 3, 2018

both @sixlettervariables and @Clockwork-Muse are correct. using the way that @Clockwork-Muse suggested will remove the confusion and be explicit.

@tarekgh tarekgh closed this as completed Oct 3, 2018
@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 3.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants