Skip to content

DateTime.Now on Linux has wrong timezone #60469

Closed

Description

@ivan7wl

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.

Activity

  1. ghost added
    untriaged
    on Oct 15, 2021
  2. danmoseley commented on Oct 15, 2021

    @danmoseley
    Contributor
  3. hez2010 commented on Oct 15, 2021

    @hez2010
    Contributor

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

  4. ivan7wl commented on Oct 15, 2021

    @ivan7wl
    Author

    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.

  5. hez2010 commented on Oct 15, 2021

    @hez2010
    Contributor

    @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.

  6. ivan7wl commented on Oct 15, 2021

    @ivan7wl
    Author

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

    Hi, please checkout the UPDATE in Other Information.

  7. ivan7wl commented on Oct 15, 2021

    @ivan7wl
    Author

    @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.

  8. ivan7wl commented on Oct 15, 2021

    @ivan7wl
    Author

    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
    
  9. removed
    untriaged
    on Oct 15, 2021
  10. added this to the Future milestone on Oct 15, 2021
  11. tarekgh commented on Oct 15, 2021

    @tarekgh
    Member

    @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.

  12. removed this from the Future milestone on Oct 15, 2021
  13. 5 remaining items

  14. ghost added
    needs-further-triage
    on Oct 16, 2021
  15. tarekgh commented on Oct 16, 2021

    @tarekgh
    Member

    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.

  16. ivan7wl commented on Oct 16, 2021

    @ivan7wl
    Author

    @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.

  17. danmoseley commented on Oct 16, 2021

    @danmoseley
    Contributor

    SelfContained publish currently not working on this version

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

  18. ivan7wl commented on Oct 17, 2021

    @ivan7wl
    Author

    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).
    
  19. ghost locked as resolved and limited conversation to collaborators on Nov 16, 2021
  20. modified the milestones: 7.0.0, Future on Feb 12, 2023
  21. richlander commented on Feb 13, 2023

    @richlander
    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 join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.DateTimeneeds-further-triage

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions