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

DateTime.Now on Linux has wrong timezone #60469

Closed
Tracked by #64603
ivan7wl opened this issue Oct 15, 2021 · 18 comments
Closed
Tracked by #64603

DateTime.Now on Linux has wrong timezone #60469

ivan7wl opened this issue Oct 15, 2021 · 18 comments
Labels
area-System.DateTime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Milestone

Comments

@ivan7wl
Copy link

ivan7wl commented Oct 15, 2021

Description

When use DateTime.Now on my Linux machine, dotnet assume the timezone is UTC.

Reproduction Steps

Using Ubuntu 20.04:

$ timedatectl status
               Local time: 五 2021-10-15 21:07:22 CST
           Universal time: 五 2021-10-15 13:07:22 UTC
                 RTC time: 五 2021-10-15 13:07:22    
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes                       
              NTP service: active                    
          RTC in local TZ: no

However the code:

DateTime localNow = DateTime.Now;
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"localNow: {localNow}, {localNow.Kind}, utcNow: {utcNow}, {utcNow.Kind}");
Console.WriteLine($"TimeZoneInfo.Local: {TimeZoneInfo.Local}");

Produces output:

localNow: 10/15/2021 13:07:25, Local, utcNow: 10/15/2021 13:07:25, Utc
TimeZoneInfo.Local: (UTC) Coordinated Universal Time

Expected behavior

DateTime.Now has the proper local time.

Actual behavior

DateTime.Now always equal to UTC time, no matter what the system timezone is.

Regression?

No response

Known Workarounds

No response

Configuration

Using dotnet sdk 6.0.100-rc.1.21463.6 on Linux (Ubuntu 20.04).

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21463.6
 Commit:    e627d556a1

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /opt/dotnet/sdk/6.0.100-rc.1.21463.6/

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

.NET SDKs installed:
  5.0.401 [/opt/dotnet/sdk]
  6.0.100-rc.1.21463.6 [/opt/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.10 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.10 [/opt/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Other information

UPDATE:
I found this issue may relate to RTC settings:

$ timedatectl set-local-rtc 1 --adjust-system-clock
$ sudo reboot
...
$ timedatectl status
               Local time: 五 2021-10-15 23:25:15 CST
           Universal time: 五 2021-10-15 15:25:15 UTC
                 RTC time: 五 2021-10-15 15:25:16    
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes                       
              NTP service: active                    
          RTC in local TZ: yes                       

Warning: The system is configured to read the RTC time in the local time zone.
         This mode cannot be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
$ dotnet run
localNow: 10/15/2021 23:25:25, Local, utcNow: 10/15/2021 15:25:25, Utc
TimeZoneInfo.Local: (UTC+08:00) CST

But this is weird, when I use timedatectl set-local-rtc 0 --adjust-system-clock to set it back, this issue cannot be reproduced anymore.

@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 Oct 15, 2021
@danmoseley
Copy link
Member

@tarekgh

@ghost
Copy link

ghost commented Oct 15, 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

When use DateTime.Now on my Linux machine, dotnet assume the timezone is UTC.

Reproduction Steps

Using Ubuntu 20.04:

$ timedatectl status
               Local time: 五 2021-10-15 21:07:22 CST
           Universal time: 五 2021-10-15 13:07:22 UTC
                 RTC time: 五 2021-10-15 13:07:22    
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes                       
              NTP service: active                    
          RTC in local TZ: no

However the code:

DateTime localNow = DateTime.Now;
DateTime utcNow = DateTime.UtcNow;
Console.WriteLine($"localNow: {localNow}, {localNow.Kind}, utcNow: {utcNow}, {utcNow.Kind}");
Console.WriteLine($"TimeZoneInfo.Local: {TimeZoneInfo.Local}");

Produces output:

localNow: 10/15/2021 13:07:25, Local, utcNow: 10/15/2021 13:07:25, Utc
TimeZoneInfo.Local: (UTC) Coordinated Universal Time

Expected behavior

DateTime.Now has the proper local time.

Actual behavior

DateTime.Now always equal to UTC time, no matter what the system timezone is.

Regression?

No response

Known Workarounds

No response

Configuration

Using dotnet sdk 6.0.100-rc.1.21463.6 on Linux (Ubuntu 20.04).

$ dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.100-rc.1.21463.6
 Commit:    e627d556a1

Runtime Environment:
 OS Name:     ubuntu
 OS Version:  20.04
 OS Platform: Linux
 RID:         ubuntu.20.04-x64
 Base Path:   /opt/dotnet/sdk/6.0.100-rc.1.21463.6/

Host (useful for support):
  Version: 6.0.0-rc.1.21451.13
  Commit:  d7619cd4b1

.NET SDKs installed:
  5.0.401 [/opt/dotnet/sdk]
  6.0.100-rc.1.21463.6 [/opt/dotnet/sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 5.0.10 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.0-rc.1.21452.15 [/opt/dotnet/shared/Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 5.0.10 [/opt/dotnet/shared/Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.0-rc.1.21451.13 [/opt/dotnet/shared/Microsoft.NETCore.App]

To install additional .NET runtimes or SDKs:
  https://aka.ms/dotnet-download

Other information

No response

Author: ivan7wl
Assignees: -
Labels:

area-System.Runtime, untriaged

Milestone: -

@hez2010
Copy link
Contributor

hez2010 commented Oct 15, 2021

I cannot reproduce it with .NET 6 rc2, could you try again with rc2?

@ivan7wl
Copy link
Author

ivan7wl commented Oct 15, 2021

I cannot reproduce it with .NET 6 rc2, could you try again with rc2?

Hi @hez2010, thanks for the reply. I also tried on dotnet 5.0.401 and 6.0.100-rc.2.21505.57, still has this issue.

@hez2010
Copy link
Contributor

hez2010 commented Oct 15, 2021

@ivan7wl
Can you execute following commands and see what are being printed in your environment?

echo $TZ
echo $TZDIR
ls -l /etc/localtime
date

Since .NET reads time zone information from /etc/localtime and $TZDIR/$TZ, this might be an issue related to your environment.

@ivan7wl
Copy link
Author

ivan7wl commented Oct 15, 2021

I cannot reproduce it with .NET 6 rc2, could you try again with rc2?

Hi, please checkout the UPDATE in Other Information.

@ivan7wl
Copy link
Author

ivan7wl commented Oct 15, 2021

@ivan7wl Can you execute following commands and see what are being printed in your environment?

echo $TZ
echo $TZDIR
ls -l /etc/localtime
date

Hi @hez2010 , I cannot reproduce this issue anymore, after I run the timedatectl command.
But I've check $TZ and etc/localtime when I first found this issue, $TZ is empty, and etc/localtime is linked to /usr/share/zoneinfo/Asia/Shanghai.

@ivan7wl
Copy link
Author

ivan7wl commented Oct 15, 2021

I don't know why but things are get more and more complicated, I can now reproduce this on 6.0.100-rc.2.21505.57, but cannot reproduce on 5.0.401.

test_project_50$ dotnet run
localNow: 10/15/2021 23:59:19, Local, utcNow: 10/15/2021 15:59:19, Utc
TimeZoneInfo.Local: (UTC+08:00) CST
test_project_50$ cd ../test_project_60/
test_project_60$ dotnet run
localNow: 10/15/2021 15:59:28, Local, utcNow: 10/15/2021 15:59:28, Utc
TimeZoneInfo.Local: (UTC) Coordinated Universal Time

@tarekgh tarekgh removed the untriaged New issue has not been triaged by the area owner label Oct 15, 2021
@tarekgh tarekgh added this to the Future milestone Oct 15, 2021
@tarekgh
Copy link
Member

tarekgh commented Oct 15, 2021

@ivan7wl

Could you please try to create a simple Console application from scratch using dotnet new Console and paste the following code in the main and send the result?

    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}");

please run this console app against .NET 6.0 and 5.0.

if you still see the issue, please share to us the file /usr/share/zoneinfo/Asia/Shanghai also try to change the TZ setting on that machine to any other zone just for testing if the issue is with Shanghai TZ only or it is generic issue on the whole machine? I'll try to look offline too.

@tarekgh tarekgh modified the milestones: Future, 7.0.0 Oct 15, 2021
@ivan7wl
Copy link
Author

ivan7wl commented Oct 15, 2021

@tarekgh

localNow: 10/16/2021 00:37:00, Local, utcNow: 10/15/2021 16:37:00, Utc
TimeZoneInfo.Local: (UTC+08:00) CST
Linux 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021
.NET 5.0.10
.... Invariant:              True ....
Asia/Shanghai
localNow: 10/15/2021 16:37:23, Local, utcNow: 10/15/2021 16:37:23, Utc
TimeZoneInfo.Local: (UTC) Coordinated Universal Time
Linux 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021
.NET 6.0.0-rc.2.21480.5
.... Invariant:              True ....
UTC

Then I changed the timezone:

localNow: 10/15/2021 09:41:39, Local, utcNow: 10/15/2021 16:41:39, Utc
TimeZoneInfo.Local: (UTC-08:00) PST
Linux 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021
.NET 5.0.10
.... Invariant:              True ....
America/Los_Angeles
localNow: 10/15/2021 16:42:31, Local, utcNow: 10/15/2021 16:42:31, Utc
TimeZoneInfo.Local: (UTC) Coordinated Universal Time
Linux 5.11.0-34-generic #36~20.04.1-Ubuntu SMP Fri Aug 27 08:06:32 UTC 2021
.NET 6.0.0-rc.2.21480.5
.... Invariant:              True ....
UTC

@tarekgh
Copy link
Member

tarekgh commented Oct 15, 2021

You have Invariant mode is set to true. is it intentional? please check if any of the settings are on in your environment. Also, for .NET 6.0 we have fixed the issue which had the same symptoms when running with Invariant mode. The fix of this issue will be in the release of .NET 6.0.

@ivan7wl
Copy link
Author

ivan7wl commented Oct 16, 2021

You have Invariant mode is set to true. is it intentional? please check if any of the settings are on in your environment. Also, for .NET 6.0 we have fixed the issue which had the same symptoms when running with Invariant mode. The fix of this issue will be in the release of .NET 6.0.

Yes the InvariantGlobalization is set to true in my test project. And the results in UPDATE maybe not related to RTC settings, because I haven't notified the dotnet runtime version when running the tests.

@ghost ghost added needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration and removed needs more info labels Oct 16, 2021
@tarekgh
Copy link
Member

tarekgh commented Oct 16, 2021

ok, thanks. This issue should be fixed in the release build already. If you are interested to try it, you may try installing the 6.0 release build from https://github.com/dotnet/installer. I am closing the issue but feel free to send any question if you have any. Thanks for your report.

@tarekgh tarekgh closed this as completed Oct 16, 2021
@ivan7wl
Copy link
Author

ivan7wl commented Oct 16, 2021

@tarekgh Thanks for your reply, I tried .NET 6.0.0-rtm.21515.9 and it works. However SelfContained publish currently not working on this version. Waiting for the official release.

@danmoseley
Copy link
Member

SelfContained publish currently not working on this version

Is that a known issue? What is the output? @agocke

@ivan7wl
Copy link
Author

ivan7wl commented Oct 17, 2021

SelfContained publish currently not working on this version

Is that a known issue? What is the output? @agocke

Hi @danmoseley , maybe this is an unreleased version, the output is:

$ dotnet publish
Microsoft (R) Build Engine version 17.0.0-preview-21515-03+d66a44095 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
/home/ivan_wl/devel/dotnet/test_project_60/test_project.csproj : error NU1102: Unable to find package Microsoft.NETCore.App.Runtime.linux-x64 with version (= 6.0.0-rtm.21515.9)
/home/ivan_wl/devel/dotnet/test_project_60/test_project.csproj : error NU1102:   - Found 63 version(s) in nuget.org [ Nearest version: 6.0.0-rc.2.21480.5 ]
/home/ivan_wl/devel/dotnet/test_project_60/test_project.csproj : error NU1102: Unable to find package Microsoft.AspNetCore.App.Runtime.linux-x64 with version (= 6.0.0-rtm.21515.15)
/home/ivan_wl/devel/dotnet/test_project_60/test_project.csproj : error NU1102:   - Found 65 version(s) in nuget.org [ Nearest version: 6.0.0-rc.2.21480.10 ]
  Failed to restore /home/ivan_wl/devel/dotnet/test_project_60/test_project.csproj (in 1.2 sec).

@ghost ghost locked as resolved and limited conversation to collaborators Nov 16, 2021
@richlander richlander reopened this Feb 12, 2023
@tarekgh tarekgh modified the milestones: 7.0.0, Future Feb 12, 2023
@richlander
Copy link
Member

I re-opened this since I was seeing unexpected behavior. After a code review, I saw that my code was wrong. Oops!

Working on some samples at dotnet/dotnet-docker#4414

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.DateTime needs-further-triage Issue has been initially triaged, but needs deeper consideration or reconsideration
Projects
None yet
Development

No branches or pull requests

6 participants