runtime icon indicating copy to clipboard operation
runtime copied to clipboard

DateTime.Now is wrong when i change Ubuntu server Time

Open shakeri opened this issue 8 years ago • 17 comments

When i change Date and Time on Ubuntu server with this command: sudo date --set "25 Apr 2018 16:00:00" And run this code: Console.WriteLine(DateTime.Now); here are its output! 4/3/2018 15:00:30 While the current server time is 4/3/2018 16:00:55 My app.csproj file

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
</Project>

shakeri avatar Apr 03 '18 12:04 shakeri

@danmosemsft

RussKeldorph avatar Apr 03 '18 17:04 RussKeldorph

@tarekgh. I assume this is not a daylight savings thing.

danmoseley avatar Apr 03 '18 17:04 danmoseley

@shakeri

after changing the date on the server, and when issuing the date command in the terminal, do you still see the date update there? I just wanted to double check if there is anything else reset the date back. Also, when running DateTime.Now, was this done in a newly launched process or it was a running process which existed when you changed the date on the server?

tarekgh avatar Apr 03 '18 17:04 tarekgh

Just to mention, on Ubuntu, we call clock_gettime https://linux.die.net/man/2/clock_gettime which give us the system time. and the doc is saying the following:

CLOCK_REALTIME
System-wide clock that measures real (i.e., wall-clock) time. Setting this clock requires appropriate privileges. This clock is affected by discontinuous jumps in the system time (e.g., if the system administrator manually changes the clock), and by the incremental adjustments performed by adjtime(3) and NTP.

Which means it should return the right time value even when the admin adjusts the date/time. so, I think the server itself didn't really adjust the date/time as requested by the date --set command.

tarekgh avatar Apr 03 '18 17:04 tarekgh

@danmosemsft

I assume this is not a daylight savings thing.

I don't think this has anything to do with the time zones.

tarekgh avatar Apr 03 '18 18:04 tarekgh

@tarekgh Yes, this changes after update Date on the server.

Also, when running DateTime.Now, was this done in a newly launched process or it was a running process which existed when you changed the date on the server?

Yes, i run the DateTime.Now in a new proccess but it still does not display the date correctly.

shakeri avatar Apr 04 '18 04:04 shakeri

I have tried that on my Ubuntu client VM and is working fine:

tarek@tarek-Ubuntu16-VM:~/test/NetCoresapp$ sudo date --set "25 Apr 2018 11:28"
Wed Apr 25 11:28:00 PDT 2018

tarek@tarek-Ubuntu16-VM:~/test/NetCoresapp$ date
Wed Apr 25 11:28:01 PDT 2018

tarek@tarek-Ubuntu16-VM:~/test/NetCoresapp$ dotnet run
4/25/18 11:28:15 AM

I had to turn the networking off on the machine to ensure the date will not reset by the network.

Could you please try to turn off the network connection on your server and then repeat the commands as I did and let me know what you will get.

tarekgh avatar Apr 04 '18 18:04 tarekgh

@shakeri I am closing this issue as it doesn't repro for us. but feel free to reply back and we'll be happy to help and reopen the issue as needed. Thanks for reporting the issue.

tarekgh avatar Apr 06 '18 15:04 tarekgh

@tarekgh I have tried that on my Ubuntu client VM but it does not work again

mohammadreza@ubuntu:~/Desktop/time$ date
Sat Apr  7 13:09:49 +0430 2018
mohammadreza@ubuntu:~/Desktop/time$ sudo date --set "7 apr 2018 14:09"
Sat Apr  7 14:09:00 +0430 2018
mohammadreza@ubuntu:~/Desktop/time$ date
Sat Apr  7 14:09:03 +0430 2018
mohammadreza@ubuntu:~/Desktop/time$ dotnet run
4/7/18 1:09:11 PM

shakeri avatar Apr 07 '18 08:04 shakeri

@shakeri looks what you did here is different than the original post which was changing the day of the month but here you are changing only the time.

It would be quicker if you can tell me what version of Ubuntu client you have used? and what is your time zone settings in that VM? Also, I assume you have turned the networking off on that VM before trying the commands, right?

from your last report, this seam related o to time zone.

tarekgh avatar Apr 09 '18 17:04 tarekgh

@tarekgh Thanks for reply.

It would be quicker if you can tell me what version of Ubuntu client you have used?

Ubuntu 16.04 LTS

and what is your time zone settings in that VM?

root@SRVCI37-SITE169:~/app# date +'%:z'
+04:30

Also, I assume you have turned the networking off on that VM before trying the commands, right?

No, i dont turned the networking off.

shakeri avatar Apr 10 '18 12:04 shakeri

No, i dont turned the networking off.

Please try to turn the networking off when you try that. the reason is, Ubuntu rests the time again according to the networking connections (the servers you connected ).

you may try the commands to ensure what the system and the framework report.

  • date
  • sudo date --set "whatever date/time you choose"
  • date
  • dotnet run
  • date

tarekgh avatar Apr 10 '18 15:04 tarekgh

@tarekgh I did these steps, but it still shows the time back one hour. In the Python code it work correctly:

mohammadreza@ubuntu:~/Desktop/time$ sudo date --set "2018/4/11 9:02:11"
Wed Apr 11 09:02:11 +0430 2018
mohammadreza@ubuntu:~/Desktop/time$ python
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> datetime.datetime.now()
datetime.datetime(2018, 4, 11, 9, 3, 2, 551453)

But not in .NET Core:

mohammadreza@ubuntu:~/Desktop/time$ dotnet run
4/11/18 8:04:34 AM

I think it's a bug.

shakeri avatar Apr 11 '18 04:04 shakeri

I can repro it now. the trick is, I needed to change the "Time & Date" settings on the machine to set the time manually. I'll take a closer look to know what is going on.

tarekgh avatar Apr 12 '18 00:04 tarekgh

@eerhardt

The problem here is Iran time zone is using the Julian date format <+0330>-3:30<+0430>,J80/0,J264/0 and we are throwing in this case according to the comment on the code

https://github.com/dotnet/coreclr/blob/master/src/mscorlib/shared/System/TimeZoneInfo.Unix.cs#L1171

Why we are not getting the month and day value from the Julian day and creating a fixed date rule with these value? I think this will work. Also, if we don't support this case, why we are throwing and not just ignoring this case?

Another side point, while debugging this issue I came across the code of FindTimeZoneIdUsingReadLink(string tzFilePath) specifically the following line:

                symlinkPath = Path.Combine(tzFilePath, symlinkPath);

https://github.com/dotnet/coreclr/blob/master/src/mscorlib/shared/System/TimeZoneInfo.Unix.cs#L388

I think this is wrong because we are combing tzFilePath which is a file path (and not a directory path) with the symlinkPath which is another file path and the results would be invalid path anyway. so the check

 if (symlinkPath.StartsWith(timeZoneDirectory, StringComparison.Ordinal))

will not be true or at east the subsequent operations would be wrong.

tarekgh avatar Apr 16 '18 18:04 tarekgh

I am reopening it for porting the fix to 2.1

tarekgh avatar Apr 19 '18 16:04 tarekgh

This is ported to 2.1.

Thanks @shakeri for reporting this issue.

tarekgh avatar Apr 19 '18 20:04 tarekgh