This is Xamarin's bug tracking system. For product support, please use the support links listed in your Xamarin Account.
Bug 24958 - TimeZoneInfo.Local.Id and TimeZoneInfo.Local.DisplayName return "Local"
Summary: TimeZoneInfo.Local.Id and TimeZoneInfo.Local.DisplayName return "Local"
Status: RESOLVED FIXED
Alias: None
Product: Mono Installers
Classification: Mono
Component: General (show other bugs)
Version: unspecified
Hardware: Macintosh Mac OS
: Normal normal
Target Milestone: 3.12.0
Assignee: marcos.henrich
URL:
Depends on:
Blocks:
 
Reported: 2014-12-01 11:48 UTC by Peter Collins
Modified: 2015-09-06 14:07 UTC (History)
3 users (show)

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


Attachments

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

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
/usr/share/zoneinfo/America/Los_Angeles
Comment 4 Joseph Lennox 2015-08-17 20:05:45 UTC
    [DllImport("c")]
    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
Marcos,

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:
https://github.com/mono/mono/pull/1989
Comment 7 marcos.henrich 2015-09-06 14:07:03 UTC
Fixed in master 83985b4ab6d3ffdd17fa08862a8ab1b838054ce8.
https://github.com/mono/mono/commit/83985b4ab6d3ffdd17fa08862a8ab1b838054ce8

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