Repository navigation
Closed
Labels
area-System.DateTimeneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Milestone
Description
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.
Metadata
Metadata
Assignees
Labels
area-System.DateTimeneeds-further-triageIssue has been initially triaged, but needs deeper consideration or reconsideration
Activity
danmoseley commentedon Oct 15, 2021
@tarekgh
hez2010 commentedon Oct 15, 2021
I cannot reproduce it with .NET 6 rc2, could you try again with rc2?
ivan7wl commentedon Oct 15, 2021
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 commentedon Oct 15, 2021
@ivan7wl
Can you execute following commands and see what are being printed in your environment?
Since .NET reads time zone information from
/etc/localtimeand$TZDIR/$TZ, this might be an issue related to your environment.ivan7wl commentedon Oct 15, 2021
Hi, please checkout the UPDATE in Other Information.
ivan7wl commentedon Oct 15, 2021
Hi @hez2010 , I cannot reproduce this issue anymore, after I run the
timedatectlcommand.But I've check
$TZandetc/localtimewhen I first found this issue,$TZis empty, andetc/localtimeis linked to/usr/share/zoneinfo/Asia/Shanghai.ivan7wl commentedon 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.
tarekgh commentedon Oct 15, 2021
@ivan7wl
Could you please try to create a simple Console application from scratch using
dotnet new Consoleand paste the following code in the main and send the result?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/Shanghaialso try to change the TZ setting on that machine to any other zone just for testing if the issue is withShanghaiTZ only or it is generic issue on the whole machine? I'll try to look offline too.5 remaining items
tarekgh commentedon 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.
ivan7wl commentedon Oct 16, 2021
@tarekgh Thanks for your reply, I tried .NET 6.0.0-rtm.21515.9 and it works. However
SelfContainedpublish currently not working on this version. Waiting for the official release.danmoseley commentedon Oct 16, 2021
Is that a known issue? What is the output? @agocke
ivan7wl commentedon Oct 17, 2021
Hi @danmoseley , maybe this is an unreleased version, the output is:
richlander commentedon Feb 13, 2023
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