[Mono-list] RE: System.TimeZone

Dwivedi , Ajay Kumar AjayKumar.Dwivedi@dresdner-bank.com
Sun, 17 Feb 2002 10:58:40 -0000

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

Content-Type: text/plain

> I've been reading this portion of your patch for the past few days
> trying to understand it. There must something that I'm not 
> understanding
> -- why does which hemisphere matter here? If timezone X in the north
> starts DST from November to March, and April to August in the south,
> then wouldn't the DaylightTime TimeSpan be different? 

	Precisely. Both the DaylightTime will be different. But the problem
lies with the fact that DaylightTime corresponds to a given year (for
we call GetDaylightChanges (int year)). So both Start and End of
are in the same year. The following example makes it clear:

	TimeZone tz = TimeZone.CurrentTimeZone;
	System.Console.WriteLine("DAYLIGHT NAME : "+ tz.DaylightName);
	DaylightTime dt = tz.GetDaylightChanges(2002);
	System.Console.WriteLine("START : "+ dt.Start);
	System.Console.WriteLine("END   : "+ dt.End);

The sample results for 3 different TimeZones are:
1. No Daylight Saving:
	DAYLIGHT NAME : India Standard Time
	START : 01/01/0001 00:00:00
	END   : 01/01/0001 00:00:00
2. Northern Hemisphere
	DAYLIGHT NAME : Vladivostok Daylight Time
	START : 31/03/2002 02:00:00
	END   : 27/10/2002 03:00:00
3. Southern Hemisphere
	DAYLIGHT NAME : New Zealand Daylight Time
	START : 06/10/2002 02:00:00
	END   : 17/03/2002 02:00:00

	Note that for the Northern Hemisphere, START < END
while START > END for the Southern Hemisphere. This is because
the DST period is Oct to Mar in South. So DST ends in March
and Starts in October.

>Why do these following checks here:
>     if (daylightTimes.Start.Ticks < time.Ticks && 
>         daylightTimes.End.Ticks > time.Ticks)
>     if (daylightTimes.Start.Ticks > daylightTimes.End.Ticks)
	As explained above, time falls in DST if it lies between
Start and End when End > Start, and if it falls outside End and Start
when Start > End.

> I would think this is the responsibility of the internal 
> CurrentTimeZone
> class to return "correct" TimeSpans? (actually, this might be quite
> difficult...)

	The DaylightTime is defined as "period during the year when the time
advanced, usually by an hour, to take advantage of the extended daylight
	Thus DaylightTime cannot start with say Oct 2001 and End in March
ie it can't span across years.

> There's also a check for:
>     if (time.Year == daylightTimes.Start.Year && 
>         time.Year == daylightTimes.End.Year)
> At first, I thought this should be a good check, but then it 
> seems like
> the only way to get a DaylightTime object is to call 
> GetDaylightChanges
> (int year). So there shouldn't be a case where daylightTimes and time
> mismatches, right? Hmm, and I just thought of this, couldn't the
> TimeSpan of DST span across a year -- it starts November 2001 and ends
> March 2002, for example.
	No, it can't span across years as explained above.
The logic behind the DST code for southern hemisphere is :
	if(Start > End)	// Southern Hemisphere.
	if(time does not lie between End and start)
		return true;
All this seems good, but there is a problem. Suppose I pass a DateTime of
1st Jan 2000, and a DaylightTime corresponding to 2002, then it will return
Hence the check for the year.

	Hope I it a little clear :)

Happy Hacking,
Ajay kumar Dwivedi,

PS: MS implementation also has the flaw for the southern hemisphere. Try
running the
attached code with NewZealand Timezone (GMT +12) Auckland, Wellington and
find that IsDaylightSavingTime returns all false throughout the year
althought daylight
saving is on. This must be result of using the if(time > Start and time <

#!!!	If anything can go wrong, _FIX_ it. (To hell with MURPHY)
							Dwivedi, Ajay kumar 

Content-Type: application/octet-stream;
Content-Disposition: attachment;

using System;
using System.Globalization;

namespace TestProject
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	public class Class1 

		public Class1()

		static void Main(string[] args)
			TimeZone tz = TimeZone.CurrentTimeZone;
			System.Console.WriteLine("DAYLIGHT NAME : "+ tz.DaylightName);
			System.Console.WriteLine("STANDARD NAME : "+ tz.StandardName);
			DaylightTime dt = tz.GetDaylightChanges(2002);
			System.Console.WriteLine("START : "+ dt.Start);
			System.Console.WriteLine("END   : "+ dt.End);
			System.Console.WriteLine("DELTA : "+ dt.Delta);
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,1,1)));
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,3,1)));
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,5,1)));
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,7,1)));
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,9,1)));
			System.Console.WriteLine(" "+ tz.IsDaylightSavingTime(new DateTime(2002,11,1)));