Bug 11817 - System.TimeZoneInfo.Local fail on Windows
Summary: System.TimeZoneInfo.Local fail on Windows
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: System.Core (show other bugs)
Version: 2.10.x
Hardware: PC Windows
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2013-04-17 01:17 UTC by Andrey
Modified: 2015-02-16 04:52 UTC (History)
7 users (show)

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


Attachments
tracing output (1.77 KB, application/octet-stream)
2013-04-17 01:17 UTC, Andrey
Details
test source code (170 bytes, text/plain)
2013-04-17 01:19 UTC, Andrey
Details
decompiled method (705 bytes, text/plain)
2013-04-17 01:19 UTC, Andrey
Details
Remove LIBC from build and use IsWindows (5.43 KB, patch)
2015-02-16 04:52 UTC, Mikkel Kruse Johnsen
Details | Diff

Description Andrey 2013-04-17 01:17:55 UTC
Created attachment 3819 [details]
tracing output

Overview
   System.TimeZoneInfo.Local fail on Windows.

Steps to Reproduce
Compile & Run the following code
-------------------------------------------------------
using System;

class MainClass {
        static void Main()
        {
                var __tz = System.TimeZoneInfo.Local;
		Console.WriteLine(__tz);
        }
}
--------------------------------------------------------

Actial Results

Unhandled Exception:
System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.get_Local () [0x00048] in C:\cygwin\sources\mono\mcs\class\System.Core\System\TimeZoneInfo.cs:101
  at MainClass.Main () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TimeZoneNotFoundException: Exception of type System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.get_Local () [0x00048] in C:\cygwin\sources\mono\mcs\class\System.Core\System\TimeZoneInfo.cs:101
  at MainClass.Main () [0x00000] in <filename unknown>:0

Expected Results
Something like:
(UTC+06:00) Ekaterinburg

Additional Info

Verified on mono 2.10.9 & mono 3.0.9

tracing
mono --debug --trace=T:System.TimeZoneInfo TimeZone.exe 
give the following output
------------------------------------------------------------------------
[000008E0: 0.00000 0] ENTER: System.TimeZoneInfo:get_Local ()()
[000008E0: 0.00000 1] ENTER: System.TimeZoneInfo:FindSystemTimeZoneByFileName (string,string)([STRING:0072EFC0:Local], [STRING:003AFEA0:/etc
/localtime], )
[000008e0:] EXCEPTION handling: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
EXCEPTION: catch found at clause 1 of System.TimeZoneInfo:get_Local ()
[000008E0: 0.03281 2] ENTER: System.TimeZoneInfo:get_TimeZoneDirectory ()()
[000008E0: 0.03543 2] LEAVE: System.TimeZoneInfo:get_TimeZoneDirectory ()[STRING:00721F50:/usr/share/zoneinfo]
[000008E0: 0.04561 2] ENTER: System.TimeZoneInfo:FindSystemTimeZoneByFileName (string,string)([STRING:0072EFC0:Local], [STRING:003ADEB0:/usr
/share/zoneinfo\localtime], )
[000008e0:] EXCEPTION handling: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
EXCEPTION: catch found at clause 0 of System.TimeZoneInfo:get_Local ()
[000008e0:] EXCEPTION handling: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.

Unhandled Exception:
System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.get_Local () [0x00048] in C:\cygwin\sources\mono\mcs\class\System.Core\System\TimeZoneInfo.cs:101
  at MainClass.Main () [0x00000] in <filename unknown>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.TimeZoneNotFoundException: Exception of type 'System.TimeZoneNotFoundException' was thrown.
  at System.TimeZoneInfo.get_Local () [0x00048] in C:\cygwin\sources\mono\mcs\class\System.Core\System\TimeZoneInfo.cs:101
  at MainClass.Main () [0x00000] in <filename unknown>:0
----------------------------------------------------------------------------

Decompiled mscorlib.dll from "C:\Program Files (x86)\Mono-2.10.9\lib\mono\4.0\" give the following code
-----------------------------------------------------------------------------
// System.TimeZoneInfo
/// <summary>Gets a <see cref="T:System.TimeZoneInfo" /> object that represents the local time zone.</summary>
/// <returns>An object that represents the local time zone.</returns>
public static TimeZoneInfo Local
{
	get
	{
		if (TimeZoneInfo.local == null)
		{
			try
			{
				TimeZoneInfo.local = TimeZoneInfo.FindSystemTimeZoneByFileName("Local", "/etc/localtime");
			}
			catch
			{
				try
				{
					TimeZoneInfo.local = TimeZoneInfo.FindSystemTimeZoneByFileName("Local", Path.Combine(TimeZoneInfo.TimeZoneDirectory, "localtime"));
				}
				catch
				{
					throw new TimeZoneNotFoundException();
				}
			}
		}
		return TimeZoneInfo.local;
	}
}
-------------------------------------------------------------------------------
Comment 1 Andrey 2013-04-17 01:19:22 UTC
Created attachment 3820 [details]
test source code
Comment 2 Andrey 2013-04-17 01:19:51 UTC
Created attachment 3821 [details]
decompiled method
Comment 3 Jon Skeet 2013-07-31 14:32:37 UTC
Quick note that this is still failing on Mono 2.11.4 and Mono 3.0.10.
Comment 4 smr 2013-08-27 10:52:05 UTC
I greatly appreciate free software and your efforts on this compiler etc.

I cannot however help wondering why this family of issues regarding TimeZone manipulation  seems to be the ultimate in back-burnered.  It or things like it can be traced back 3 years:

https://bugzilla.novell.com/show_bug.cgi?id=621408

https://bugzilla.novell.com/show_bug.cgi?id=622524

https://bugzilla.xamarin.com/show_bug.cgi?id=8641

and now this one.

I spent some time this morning chasing my tail on this before finding out it's a known bug, part of what appears to be a family of known bugs, and has been for quite a while.  A fix would be appreciated.
Comment 5 mauzik 2013-08-28 10:03:49 UTC
I have a troubles with this bug too. I can not use Json.NET serialization and deserialization under Mono on Windows. I have installed mono version 3.0.10.
Comment 6 Apolo Program 2013-09-26 02:22:42 UTC
Hello:

I had this problem and came with a work around. 

1. Go to a Linux system and grab folder /usr/share/zoneinfo.
2. Put it on your project's root, or anywhere else.
3. Create a class which changes TimeZoneInfo private setting 'TimeZoneDirectory', which is only available on Mono, to point to where you just put zoneinfo folder.

That's it. Next there is a class sample. Folder name, cannot be changed. This will not break mono/Linux Local, but might break something else. I just needed this to work on Windows which is my development and testing environment. For deployment on Linux I recommend removing the work around.

Hope it helps.

The class:
public class MonoBug11817WorkAround
    {
        public static void Apply()
        {
            var property = typeof(TimeZoneInfo).GetProperty("TimeZoneDirectory",
                BindingFlags.Static | BindingFlags.NonPublic);

            if (property == null) return;

            var zoneInfo = FindZoneInfoFolder();
            property.SetValue(null, zoneInfo, new object[0]);
        }

        public static string FindZoneInfoFolder()
        {
            var current = new DirectoryInfo(Directory.GetCurrentDirectory());

            while(current != null)
            {
                var zoneinfoTestPath = Path.Combine(current.FullName, "zoneinfo");
                if (Directory.Exists(zoneinfoTestPath))
                    return zoneinfoTestPath;

                current = current.Parent;
            }

            return null;
        }
    }
Comment 7 Mikkel Kruse Johnsen 2015-02-16 04:52:01 UTC
Created attachment 9846 [details]
Remove LIBC from build and use IsWindows

Remove LIBC as a build option and use IsWindows instead.

Since cross compiling to windows is not able to compile System DLL's all DLL's should be cross platform.

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