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

Bug: DateTime.Now from .NET 6 will return UTC time, not local time in Ubuntu #63194

Closed
mndarren opened this issue Dec 29, 2021 · 17 comments
Closed
Labels
area-System.DateTime untriaged New issue has not been triaged by the area owner

Comments

@mndarren
Copy link

Description

We upgraded our system from .NET 5 to .NET 6. After that, all our time showing on the UI side has been changed from local time to UTC time. Since our system is running on Ubuntu 20.04 Linux server, from our tests, we found that all DateTime.Now call will return a UTC time, not local time. We think this should be a bug.

We tested .NET 5 before and DateTime.Now works well.

Our current solution is to read timezone from Linux system and to convert UTC time to local time.

Reproduction Steps

image
image

Expected behavior

DateTime.Now should return local time

Actual behavior

DateTime.Now returns the UTC time

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

@dotnet-issue-labeler
Copy link

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

@dotnet-issue-labeler dotnet-issue-labeler bot added the untriaged New issue has not been triaged by the area owner label Dec 29, 2021
@mndarren
Copy link
Author

I checked the area label and I think it should belong to area-Microsoft.CSharp since it is about CSharp function, but unfortunately I don't have permission to add the label.

@ghost
Copy link

ghost commented Dec 29, 2021

Tagging subscribers to this area: @dotnet/area-system-runtime
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

We upgraded our system from .NET 5 to .NET 6. After that, all our time showing on the UI side has been changed from local time to UTC time. Since our system is running on Ubuntu 20.04 Linux server, from our tests, we found that all DateTime.Now call will return a UTC time, not local time. We think this should be a bug.

We tested .NET 5 before and DateTime.Now works well.

Our current solution is to read timezone from Linux system and to convert UTC time to local time.

Reproduction Steps

image
image

Expected behavior

DateTime.Now should return local time

Actual behavior

DateTime.Now returns the UTC time

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Author: mndarren
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

@Clockwork-Muse
Copy link
Contributor

Related: #60469 - Please read that issue and see if that fixes/explains your problem, or please report with the information the diagnostic steps listed there provide.


That aside -

  1.  Console.WriteLine("DateTime.Now (not toString()): "+ DateTime.Now);
    
    The comment is wrong - ToString() is being called implicitly, as it is for all objects/structs that are being combined with strings.
  2. Calling DateTime.Now (or DateTimeOffset.Now) in server code is almost never the correct thing to do, because the timezone/offset of the server is rarely relevant or helpful. It would be better to pick up the timezone as a configuration variable for whatever entity is currently under request, and explicitly get local time via an explicit conversion. If you're logging, you should be logging in UTC (and having reporting tools convert to local times when/if appropriate).

@mndarren
Copy link
Author

The ticket #60469 shows the similar issue (user using .NET 6 testing version), and tarekgh said the problem has solved in .NET 6.0 official version, but I'm using the .NET 6.0.1 version. I got the same issue again, so I don't think this issue has been solved in .NET 6.0.1 version.

DateTime.Now.ToString("G") still returns the UTC time.

  1. This is just a simple test. Console.WriteLine("DateTime.Now (not toString()): "+ DateTime.Now); The comment means there's no toString() in the code. That makes sense for me.
  2. Right. In our real code, we'll not directly use DateTime.Now. Again, this piece of code is for testing only.

@mndarren
Copy link
Author

mndarren commented Dec 29, 2021

Based on the comment from #60469, I ran the following code and the output following:

Console.WriteLine("Hello, World!");
Console.WriteLine($"{RuntimeInformation.OSDescription}");
Console.WriteLine($"{RuntimeInformation.FrameworkDescription}");
Console.WriteLine($".... Invariant:              {typeof(object).Assembly.GetType("System.Globalization.GlobalizationMode").GetProperty("Invariant", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null)} ....");
Console.WriteLine($"{TimeZoneInfo.Local.Id}");

Output:
Hello, World!
Linux 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
.NET 6.0.1
.... Invariant: False ....
America/Chicago

@Clockwork-Muse
Copy link
Contributor

America/Chicago

Does not compute. Can you add Console.WriteLine(DateTime.Now); to that and run it again, please?

Also, what is the output of the question in this comment?

@mndarren
Copy link
Author

image
image
That's wired. It works now.

@mndarren
Copy link
Author

My test server works now. But our Alpha server is still UTC time.
image

That's interesting and wired. :)

@Clockwork-Muse
Copy link
Contributor

What are you running on "locally"? A devcontainer? I think there was an issue about the current local time being mapped into the container. If so, that still makes sense, because a remote server would still be set to UTC/not mapped.

@mndarren
Copy link
Author

mndarren commented Dec 29, 2021

Our system is running in a Ubuntu Docker container in a Ubuntu Host, but we mapped local time from host to container.
I'll rebuild my test server and do more tests to find the reason for this wired situation.

@mndarren
Copy link
Author

The following is the output of running in the docker container:
image
That's the reason to get the UTC time.

@Clockwork-Muse
Copy link
Contributor

Ah, I thought I'd remembered this: #62545

I'd say "use the recommendation at the end of the thread", but barring exceptional circumstances you probably shouldn't be setting the timezone of the container at all.

@mndarren
Copy link
Author

mndarren commented Dec 30, 2021

Thanks Clockwork-Muse!
Based on the reference from #62545, I tried to set mapping -v for localtime (/usr/share/zoneinfo/America/Chicago:/etc/localtime) when docker run command, but that still does not work since /etc/localtime still points to the Etc/UTC.

I'll do more tests.

@Clockwork-Muse
Copy link
Contributor

No, the -v mapping is what you were not supposed to do, but set it some other way inside the container.

That aside, why do you care? By your own admission this isn't going to be part of your final deployment, so you seem to be doing a bunch of work that you're not going to keep.

@mndarren
Copy link
Author

Sorry I didn't explain our application very clearly.
Our system will deploy a whole container, not just deploy something into a container, so -v mapping is so important for us. Actually we use bind mount to map multiple directories between host and container. Others work well, but only this /etc/localtime mapping does not work.

@mndarren
Copy link
Author

From our tests, this issue should be the docker Ubuntu container local time issue that the local time in the container always be set as UTC time. We didn't find why to be set UTC time always yet, but we believe it's not because of .NET 6 version since the local time in the host (Ubuntu) works well.

My solution for this issue is to get timezone from /etc/timezone file, and then calculate the local time via UTC time. Finally, replace all DateTime.Now by our function.

Based on this, I recommend to close this bug ticket. Thanks so much for your help!

@ghost ghost locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.DateTime untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

4 participants