Bug 58444 - System.TimeZoneInfo.Local.Id returns "Local" instead of proper IANA ID on Debian and Ubuntu
Summary: System.TimeZoneInfo.Local.Id returns "Local" instead of proper IANA ID on Deb...
Status: CONFIRMED
Alias: None
Product: Class Libraries
Classification: Mono
Component: mscorlib (show other bugs)
Version: master
Hardware: PC Linux
: --- minor
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-07-27 20:55 UTC by Brandon White
Modified: 2017-09-11 14:48 UTC (History)
2 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 58444 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
CONFIRMED

Description Brandon White 2017-07-27 20:55:57 UTC
System.TimeZoneInfo.Local.Id returns "Local" instead of proper IANA ID on Debian and Ubuntu.  This appears to be due to TimeZoneInfo.cs:TryGetNameFromPath being coded to only work with symbolic links regarding /etc/localtime.  On Linux distros based on Debian, /etc/localtime is not a symbolic link but rather is a full copy of one of the particular files in /usr/share/zoneinfo.  This causes Mono to fail to identify the system's configured timezone.

```
brandon@UbuntuServer:~$ mono -V
Mono JIT compiler version 5.0.1.1 (2017-02/5077205 Thu May 25 09:19:30 UTC 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
        TLS:           __thread
        SIGSEGV:       altstack
        Notifications: epoll
        Architecture:  amd64
        Disabled:      none
        Misc:          softdebug
        LLVM:          supported, not enabled.
        GC:            sgen (concurrent by default)

brandon@UbuntuServer:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.5 LTS
Release:        14.04
Codename:       trusty

brandon@UbuntuServer:~$ csharp
csharp> System.TimeZoneInfo.Local.Id                                                                                                 "Local"                                           

```

What is expected is something like "US/Central" -- whatever the system is actually using.  In particular, it should match the content of the /etc/timezone file.

Perhaps the Mono code in TimeZoneInfo.cs:CreateLocal() could probe /etc/timezone after the "TZ" environment variable probe, and before the /etc/localtime lookup logic.

A common manifestation of this bug is when attempting to use the popular NodaTime library on Mono on Debian-based distros. Specifically, when attempting to call `NodaTime.DateTimeZoneProviders.Tzdb.GetSystemDefault()`
Comment 1 Brandon White 2017-07-27 20:58:24 UTC
If it's any help to others coming across this bug, I've devised the following workaround:

```

        /// <summary>
        /// OS file where the IANA timezone ID is stored.
        /// </summary>
        protected const string TimezoneFilePath = "/etc/timezone";

        /// <summary>
        /// Environment variable that Mono will use for the IANA timezone ID.
        /// </summary>
        protected const string MonoTimezoneEnvironmentVariableKey = "TZ";

        /// <summary>
        /// Workaround/fix for Mono bug where it cannot determine the IANA timezone if /etc/localtime is a copy rather
        /// than a link to a zoneinfo file in /usr/share/zoneinfo. Some distros use a symbolic link, and Mono works with
        /// that. Others such as Debian based distros use a file copy, and Mono does *not* work there.
        /// In the latter case `System.TimeZoneInfo.Local` will contain "Local" for the Id.
        ///
        /// </summary>
        void RefreshMonoTimezone()
        {
            var timezoneLines = File.ReadAllLines(TimezoneFilePath);
            if (timezoneLines.Length < 1) return;

            string timezoneId = timezoneLines[0];
            if (string.IsNullOrWhiteSpace(timezoneId)) return;

            Environment.SetEnvironmentVariable(MonoTimezoneEnvironmentVariableKey, timezoneId);

            // Force Mono to reload it's internal tzinfo
            //
            System.Globalization.CultureInfo.CurrentCulture.ClearCachedData();
        }


```