Bug 43805

Summary: Output of DateTime.Now() differs on Mono for ambiguous time period
Product: [Mono] Class Libraries Reporter: Neale Ferguson <neale>
Component: SystemAssignee: Bugzilla <bugzilla>
Status: NEW ---    
Severity: normal CC: mono-bugs+mono
Priority: ---    
Version: unspecified   
Target Milestone: Untriaged   
Hardware: All   
OS: Linux   
Tags: Is this bug a regression?: ---
Last known good build:
Attachments: Patch to duplicate the behaviour of DateTime.Now() on .NET

Description Neale Ferguson 2016-08-27 00:22:27 UTC
If I run the following program:

using System;

public class Example
{
        public static void Main()
        {
                Console.WriteLine(DateTime.Now);
        }
}

Using the Great Britain timezone UTC and if I set the system date and time to a couple of hours before the transition (midnight on the 30th UTC)from daylight saving time back to standard time (2016-10-30 02:00:00):

[neale@lneale3 - mono] sudo date --utc 103000002016.00
Sun Oct 30 00:00:00 UTC 2016

And run the program. I would expect that my time  would be UTC (1am) - which it is when I run it on Windows .NET. However, it comes out as midnight as it determines that we are in the ambiguous hour and simply applies the base offset.

In https://msdn.microsoft.com/en-us/library/bb384269(v=vs.110).aspx it says 'Make an assumption about how the time maps to UTC. For example, you can assume that an ambiguous time is always expressed in the time zone's standard time.’ However, it appears .NET does not take this advice and will return the daylight saving time for that ambiguous hour.

I suggest that Mono mimic .NET.
Comment 1 Marek Safar 2016-08-29 10:49:18 UTC
*** Bug 43804 has been marked as a duplicate of this bug. ***
Comment 2 Neale Ferguson 2016-09-14 16:54:11 UTC
Created attachment 17468 [details]
Patch to duplicate the behaviour of DateTime.Now() on .NET
Comment 3 Neale Ferguson 2016-12-16 20:54:01 UTC
Also, related. If I run the program below which defines a transition time of 3am on the Sunday (in this example 2016-10-30) and I pass it times from midnight to 4am then I get:

Transition Start: Sunday-3-5 at 1/1/0001 2:00:00 AM
Transition End:   Sunday-10-5 at 1/1/0001 3:00:00 AM
10/30/2016 12:00:00 AM MDT -> 10/29/2016 11:00:00 PM UTC (False - Local)
10/30/2016 1:00:00 AM MDT -> 10/30/2016 12:00:00 AM UTC (False - Local)
10/30/2016 2:00:00 AM MST -> 10/30/2016 2:00:00 AM UTC (False - Local)
10/30/2016 3:00:00 AM MST -> 10/30/2016 3:00:00 AM UTC (True - Local)
10/30/2016 4:00:00 AM MST -> 10/30/2016 4:00:00 AM UTC (False - Local)

Above if the local clock is 2am then it is still within DST and should report as 1am UTC.

What I see is in TimeZoneInfo we create a DateTime value stdUtcDateTime that has a Kind property of UTC and pass it to IsInDST->IsInDSTForYear which adjusts the end rule based on the kind being UTC. Why do we not just pass it the local time which has a Kind property of local. In this case we make no adjustment to the DST rule and we correctly assess that the UTC is 1 hour earlier.

Making this change results in:

Rule Start: 1/1/1970 12:00:00 AM End: 12/31/9999 12:00:00 AM
Transition Start: Sunday-3-5 at 1/1/0001 2:00:00 AM
Transition End:   Sunday-10-5 at 1/1/0001 3:00:00 AM
10/30/2016 12:00:00 AM MDT -> 10/29/2016 11:00:00 PM UTC (False - Local)
10/30/2016 1:00:00 AM MDT -> 10/30/2016 12:00:00 AM UTC (False - Local)
10/30/2016 2:00:00 AM MDT -> 10/30/2016 1:00:00 AM UTC (False - Local)
10/30/2016 3:00:00 AM MST -> 10/30/2016 3:00:00 AM UTC (True - Local)
10/30/2016 4:00:00 AM MST -> 10/30/2016 4:00:00 AM UTC (False - Local)

using System;
using System.Reflection;

public class Example
{
	public static void Main()
	{
		TimeZoneInfo.TransitionTime startTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 2, 0, 0), 3, 5, DayOfWeek.Sunday);

		TimeZoneInfo.TransitionTime endTransition = TimeZoneInfo.TransitionTime.CreateFloatingDateRule(new DateTime(1, 1, 1, 3, 0, 0), 10, 5, DayOfWeek.Sunday);

		TimeSpan delta = TimeSpan.FromMinutes(60.0);
		TimeZoneInfo.AdjustmentRule adjustment = TimeZoneInfo.AdjustmentRule.CreateAdjustmentRule(new DateTime(1970, 1, 1), DateTime.MaxValue.Date, delta, startTransition, endTransition);
		Console.WriteLine("Rule Start: {0} End: {1}",adjustment.DateStart,adjustment.DateEnd);
		TimeZoneInfo.TransitionTime startTrans = adjustment.DaylightTransitionStart;
		TimeZoneInfo.TransitionTime endTrans = adjustment.DaylightTransitionEnd;
		Console.WriteLine("Transition Start: {0}-{1}-{2} at {3}",startTrans.DayOfWeek,startTrans.Month,startTrans.Week, startTrans.TimeOfDay);
		Console.WriteLine("Transition End:   {0}-{1}-{2} at {3}",endTrans.DayOfWeek,endTrans.Month,endTrans.Week, endTrans.TimeOfDay);
		TimeZoneInfo.AdjustmentRule[] adjustments = { adjustment };

		TimeZoneInfo tzInfo = TimeZoneInfo.CreateCustomTimeZone("MY Standard Time", TimeSpan.Zero, "MST", "MST", "MDT", adjustments);

		// There is no .NET API to set timezone. Use reflection to assign time zone to the TimeZoneInfo.local field.
		FieldInfo localTimeZone = typeof(TimeZoneInfo).GetField("local", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.Instance);
		localTimeZone.SetValue(null, tzInfo);

		for (int i = 0; i < 5; i++) {
			DateTime st = new DateTime(2016, 10, 30, i, 0, 0, DateTimeKind.Local);
			Console.WriteLine("{0} {1} -> {2} UTC ({3} - {4})", st.ToString(), (tzInfo.IsDaylightSavingTime(st) ? tzInfo.DaylightName : tzInfo.StandardName), TimeZoneInfo.ConvertTimeToUtc(st), tzInfo.IsAmbiguousTime(st), st.Kind);
		}

	}
}