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: 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)

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

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.


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

  • Export the original title and description: Developer Community HTML or GitHub Markdown
  • 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


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.

Related Links: