Bug 24958 - TimeZoneInfo.Local.Id and TimeZoneInfo.Local.DisplayName return "Local"
Summary: TimeZoneInfo.Local.Id and TimeZoneInfo.Local.DisplayName return "Local"
Alias: None
Product: Installers
Classification: Mono
Component: General ()
Version: unspecified
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 3.12.0
Assignee: marcos.henrich
Depends on:
Reported: 2014-12-01 11:48 UTC by Peter Collins
Modified: 2015-09-06 14:07 UTC (History)
3 users (show)

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 on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Comment 1 Rodrigo Kumpera 2014-12-01 19:11:53 UTC

Could you take a look at this?
Comment 2 marcos.henrich 2014-12-02 06:50:27 UTC
TimeZoneInfo.Local in OS X is loaded from /etc/localtime.
/etc/localtime is either a symbolic link or a copy of /usr/share/zoneinfo/<OLSON_ID>

From the file data we cannot obtain the Olson Id, only the short standard and DST name of the last rule (e.g CET, EST, GMT, ..).

When we compare /etc/localtime with /usr/share/zoneinfo/* there can be multiple matches.

We cannot change the Id from "Local" to the standard name because the local TZ can have base offset changes thus previous standard names. We would have the following test failing in those timezones:

//This loads /etc/localtime
var tz1 = TimeZoneInfo.Local.Id; 
//This would load /usr/share/zoneinfo/<standard name>
var tz2 = TimeZoneInfo.FindSystemTimeZoneById(tz1.Id);
DateTime date = <time before the time zone base offset change>;
Assert.AreEqual(TimeZoneInfo.ConvertTimeToUtc(date, tz1),
    TimeZoneInfo.ConvertTimeToUtc(date, tz2));
Comment 3 Joseph Lennox 2015-08-17 20:03:07 UTC
Can this case be re-examined? This is breaking behavior. For example, NodaTime does not work properly in Mono on OSX: http://nodatime.org/1.1.x/userguide/mono.html

The Olson ID can be unambiguously had using readlink.

readlink /etc/localtime
Comment 4 Joseph Lennox 2015-08-17 20:05:45 UTC
    private static extern int readlink(string path, byte[] buffer, int buflen);

    public static string ReadLink(string path)
        var buf = new byte[512];
        var ret = readlink(path, buf, buf.Length);
        return ret == -1
            ? null
            : Encoding.UTF8.GetString(buf, 0, ret);

    static DateTimeZone GetLocalTimeZoneId()
        const string pathprefix = "/usr/share/zoneinfo/";
        // eg: /usr/share/zoneinfo/America/Los_Angeles
        var timezonepath = ReadLink("/etc/localtime");
        if (timezonepath == null || !timezonepath.StartsWith(pathprefix))
            throw new Exception("Couldn't determine local timezone.");

        return timezonepath.Substring(pathprefix.Length);
Comment 5 Rodrigo Kumpera 2015-08-18 02:01:58 UTC

Maybe we could check the symlink to figure out what's the actual local TZ, what do you think?
Comment 6 marcos.henrich 2015-08-18 12:55:43 UTC
I did a pull request that uses the symbolic link to figure out the Olson Id:
Comment 7 marcos.henrich 2015-09-06 14:07:03 UTC
Fixed in master 83985b4ab6d3ffdd17fa08862a8ab1b838054ce8.