Time zone functionality not working

Topics: Developer Forum
Aug 6, 2007 at 4:28 PM
First let me thank you for publishing a .net solution for the Olson database. This will be very helpful.

I do have questions on functionality that I can't get to work. I am not sure whether I am doing something wrong. I read the writeup at http://www.codeproject.com/useritems/UsingtimezonesinNET.asp and that's where I got the sample code. I converted it to vb.net.

I can't get DST offsets to work correctly. This is what i tried for the Central/Chicago time zone:

1. TZTimeZone.GetDaylightChanges returns the correct on and off DST dates only for 2007. For 2006, it returns April 30 as the on date, and according to the rules from http://home.tiscali.nl/~t876506/TZworld.html#nam (and the northamerica file from tzdata2007f.tar at ftp://elsie.nci.nih.gov/pub/), for the period from 1987 to 2006 it should return first Sunday in April (4/2/2006, 4/3/2005, 4/4/2004, 4/6/2003). 4/30/2006 would be correct according to the rules from 1976-1986 (Apr lastSun). For all years prior to 2006, it returns the same on and off dates as 2006, and for all years after 2007, the on and off dates are the same as in 2007. Here is my code:

Dim s2 as string
Dim zone As PublicDomain.TzTimeZone
'zone = PublicDomain.TzTimeZone.CurrentTimeZone()
zone = PublicDomain.TzTimeZone.GetTimeZone(PublicDomain.TzConstants.TimezoneAmericaChicago)
s2 &= "zone.StandardName: '" & zone.StandardName & "'" & vbCrLf
s2 &= "zone.GetUtcOffset(Now).TotalHours: '" & zone.GetUtcOffset(Now).TotalHours.ToString & "'" & vbCrLf
s2 &= "zone.ToUniversalTime(now): '" & zone.ToUniversalTime(Now) & "'" & vbCrLf
s2 &= "zone.DaylightName(): '" & zone.DaylightName() & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2004).Start: '" & zone.GetDaylightChanges(2004).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2004).End: '" & zone.GetDaylightChanges(2004).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2005).Start: '" & zone.GetDaylightChanges(2005).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2005).End: '" & zone.GetDaylightChanges(2005).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2006).Start: '" & zone.GetDaylightChanges(2006).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2006).End: '" & zone.GetDaylightChanges(2006).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2007).Start: '" & zone.GetDaylightChanges(2007).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2007).End: '" & zone.GetDaylightChanges(2007).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2008).Start: '" & zone.GetDaylightChanges(2008).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2008).End: '" & zone.GetDaylightChanges(2008).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2009).Start: '" & zone.GetDaylightChanges(2009).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2009).End: '" & zone.GetDaylightChanges(2009).End & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2010).Start: '" & zone.GetDaylightChanges(2010).Start & "'" & vbCrLf
s2 &= "zone.GetDaylightChanges(2010).End: '" & zone.GetDaylightChanges(2010).End & "'" & vbCrLf

Contents of s2:
zone.StandardName: 'America/Chicago'
zone.GetUtcOffset(Now).TotalHours: '-5'
zone.ToUniversalTime(now): '8/6/2007 3:46:50 PM'
zone.DaylightName(): 'America/Chicago'
zone.GetDaylightChanges(2004).Start: '4/30/2004 2:00:00 AM'
zone.GetDaylightChanges(2004).End: '10/29/2004 2:00:00 AM'
zone.GetDaylightChanges(2005).Start: '4/30/2005 2:00:00 AM'
zone.GetDaylightChanges(2005).End: '10/29/2005 2:00:00 AM'
zone.GetDaylightChanges(2006).Start: '4/30/2006 2:00:00 AM'
zone.GetDaylightChanges(2006).End: '10/29/2006 2:00:00 AM'
zone.GetDaylightChanges(2007).Start: '3/11/2007 2:00:00 AM'
zone.GetDaylightChanges(2007).End: '11/4/2007 2:00:00 AM'
zone.GetDaylightChanges(2008).Start: '3/11/2008 2:00:00 AM'
zone.GetDaylightChanges(2008).End: '11/4/2008 2:00:00 AM'
zone.GetDaylightChanges(2009).Start: '3/11/2009 2:00:00 AM'
zone.GetDaylightChanges(2009).End: '11/4/2009 2:00:00 AM'
zone.GetDaylightChanges(2010).Start: '3/11/2010 2:00:00 AM'
zone.GetDaylightChanges(2010).End: '11/4/2010 2:00:00 AM'


2. TzTimeZone.IsDaylightSavingTime returns True for a date in December, when DST will be out. The rule for the off date is 'first Sunday in November'. Append this code to the code above:

s2 &= "zone.IsDaylightSavingTime(Now): '" & zone.IsDaylightSavingTime(Now) & "'" & vbCrLf
s2 &= "zone.IsDaylightSavingTime(#12/15/2007#): '" & zone.IsDaylightSavingTime(#12/15/2007#) & "'" & vbCrLf
s2 &= "zone.IsDaylightSavingTime('2007-12-15'): '" & zone.IsDaylightSavingTime("2007-12-15") & "'" & vbCrLf

Contents of s2:
zone.IsDaylightSavingTime(Now): 'True'
zone.IsDaylightSavingTime(#12/15/2007#): 'True'
zone.IsDaylightSavingTime('2007-12-15'): 'True'



3. TZTimeZone.GetUtcOffset shows the same value (-5) during the DST period and during the standard time period (December) of this year.

Dim dtDate As PublicDomain.TzDateTime
dtDate = New PublicDomain.TzDateTime(zone.ToLocalTime(DateTime.UtcNow), zone)
dtDate.DateTimeLocal = "2007-09-12 03:00"
s2 &= "dtDate.DateTimeLocal = '2007-09-12 03:00'" & vbCrLf
s2 &= "zone.GetUtcOffset(dtDate).TotalHours: '" & zone.GetUtcOffset(dtDate.DateTimeLocal).TotalHours & "'" & vbCrLf
dtDate.DateTimeLocal = "2007-12-12 03:00"
s2 &= "dtDate.DateTimeLocal = '2007-12-12 03:00'" & vbCrLf
s2 &= "zone.GetUtcOffset(dtDate).TotalHours: '" & zone.GetUtcOffset(dtDate.DateTimeLocal).TotalHours & "'" & vbCrLf

Contents of s2:
dtDate.DateTimeLocal = '2007-09-12 03:00'
zone.GetUtcOffset(dtDate).TotalHours: '-5'
dtDate.DateTimeLocal = '2007-12-12 03:00'
zone.GetUtcOffset(dtDate).TotalHours: '-5'


4. TZTimeZone.ToUniversalTime confirms the above results by always converting the CDT time to Utc using the +5 hours calculation regardless of the DST period:

s2 &= "zone.ToUniversalTime('2007-09-12 03:00'): '" & zone.ToUniversalTime("2007-09-12 03:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2007-11-04 01:00'): '" & zone.ToUniversalTime("2007-11-04 01:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2007-11-04 02:00'): '" & zone.ToUniversalTime("2007-11-04 02:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2007-11-04 03:00'): '" & zone.ToUniversalTime("2007-11-04 03:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2007-12-04 03:00'): '" & zone.ToUniversalTime("2007-12-04 03:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2008-03-09 01:00'): '" & zone.ToUniversalTime("2008-03-09 01:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2008-03-09 02:00'): '" & zone.ToUniversalTime("2008-03-09 02:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2008-03-09 03:00'): '" & zone.ToUniversalTime("2008-03-09 03:00") & "'" & vbCrLf
s2 &= "zone.ToUniversalTime('2008-05-10 03:00'): '" & zone.ToUniversalTime("2008-05-10 03:00") & "'" & vbCrLf

Contents of s2:
zone.ToUniversalTime('2007-09-12 03:00'): '9/12/2007 8:00:00 AM'
zone.ToUniversalTime('2007-11-04 01:00'): '11/4/2007 6:00:00 AM'
zone.ToUniversalTime('2007-11-04 02:00'): '11/4/2007 7:00:00 AM'
zone.ToUniversalTime('2007-11-04 03:00'): '11/4/2007 8:00:00 AM'
zone.ToUniversalTime('2007-12-12 03:00'): '12/12/2007 8:00:00 AM'
zone.ToUniversalTime('2008-03-09 01:00'): '3/9/2008 6:00:00 AM'
zone.ToUniversalTime('2008-03-09 02:00'): '3/9/2008 7:00:00 AM'
zone.ToUniversalTime('2008-03-09 03:00'): '3/9/2008 8:00:00 AM'
zone.ToUniversalTime('2008-05-10 03:00'): '5/10/2008 8:00:00 AM'


5. When I run this code, I get an object not found exception on the GetDaylightChanges(2004).Start line. It appears that GetDaylightChanges() returns Nothing and .Start chokes on this.

zone = PublicDomain.TzTimeZone.GetTimeZone("Europe/Paris")
s2 &= "zone.GetDaylightChanges(2004).Start: '" & zone.GetDaylightChanges(2004).Start & "'" & vbCrLf


Looking forward to your answer.
Coordinator
Aug 9, 2007 at 11:46 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.