Reputation: 900
I have a .NET Core 2.0 application where I need to convert UTC DateTime
s to local server times. This software runs both on Linux (CentOS 7) and Windows.
On Windows, the code works as expected: DateTime.UtcNow
returns UTC time and DateTime.UtcNow.ToLocalTime()
returns the time adjusted to current server time.
However, on CentOS, DateTime.UtcNow.ToLocalTime()
returns the same UTC time instead of local time:
$"Utc: {DateTime.UtcNow}, Kind: {DateTime.UtcNow.Kind}";
$"Local: {DateTime.UtcNow.ToLocalTime()}, Kind: {DateTime.UtcNow.ToLocalTime().Kind}";
Outputs
"Utc: 07/19/2018 10:53:03, Kind: Utc"
"Local: 07/19/2018 10:53:03, Kind: Local"
While the CentOS Server time should be UTC+3:
$ date
Thu Jul 19 13:55:14 EEST 2018
Edit: DateTime.Now
also returns current time as UTC instead of the local time.
Upvotes: 4
Views: 1489
Reputation: 900
Obviously I should have mentioned that I'm using Docker. This is apparently a Docker feature - containers don't share the host's timezones. I solved this by defining the timezone in my docker-compose.yml
file:
services:
...:
image: ...
environment:
TZ: "Europe/Helsinki"
Upvotes: 6