Bug 24958

Summary: TimeZoneInfo.Local.Id and TimeZoneInfo.Local.DisplayName return "Local"
Product: [Mono] Installers Reporter: Peter Collins <peter.collins>
Component: GeneralAssignee: marcos.henrich
Severity: normal CC: jlennox, kumpera, mono-bugs+mono
Priority: Normal    
Version: unspecified   
Target Milestone: 3.12.0   
Hardware: Macintosh   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

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.