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)

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


Attachments

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();
        }


```

Note You need to log in before you can comment on or make changes to this bug.