runtime: DateTime.Now is wrong when i change Ubuntu server Time

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>

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 17 (13 by maintainers)

Most upvoted comments

This is ported to 2.1.

Thanks @shakeri for reporting this issue.

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

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.