Archive for August, 2005

Times, Timezones and Daylight Savings

Wednesday, August 31st, 2005

Oh man… I just made it through a bit of a timezone thing so I thought I’d record it here for propersity (and so I never need to deal with it again).

Background info:

Here is some information regarding Date and Time Format standards from W3C: http://www.w3.org/TR/NOTE-datetime

Here is another link regarding Best Practices with Dates and Times in .NET

And finally, here’s a link to the MSDN reference on the Timezone class

My problem: Basically, I’m building an web application to import a bunch of events into an Exchange calendar via WEBDAV. This method requires dates and times for events being in the following format: yyyy-MM-ddTHH:mm:ss.fffZ

As specified by the W3C: the ‘T’ is a literal character that represents the start of the time section. The ‘Z’ however is a short form for the UTC timezone. You can replace this with a timezone offset like “-04:00″. So that makes “2005-07-20T10:30:00.000-04:00″ a valid time format.

Enter my horror: The problem is that in areas that observe Daylight Savings time the offset will change depending on whether or not you’re within the daylight saving time frame. This is compounded when adding future events because you to need to ensure it is the correct offset for the time when the event is going to occur.

.NET to the rescue: Luckily .NET has some classes for dealing with this. The following code snippet will automatically format your date object in a recognized W3C format and will add the correct offset for your timezone. It will also automatically adjust for Daylight Savings Time:


'You must import the System namespace
'startDateAdj, endDateAdj is declared and set to desired local time earlier
Dim tz As TimeZone = TimeZone.CurrentTimeZone
strStartDate = Format(startDateAdj, "yyyy-MM-ddTHH:mm:ss.fff") & tz.GetUtcOffset(startDateAdj).ToString
strEndDate = Format(endDateAdj, "yyyy-MM-ddTHH:mm:ss.fff") & tz.GetUtcOffset(endDateAdj).ToString

Thank you .NET!

Time Processes in .NET

Friday, August 26th, 2005

Here’s some simple code to time a process in VB .NET. This particular example is for an ASP .NET app (hence the “Response.Write”:

     Dim ProcessStartTime As Date = Now

     'CODE TO BE TIMED GOES HERE

     Dim RunLength As System.TimeSpan = Now.Subtract(ProcessStartTime)
     Dim Millisecs As Integer = RunLength.TotalMilliseconds
     Response.Write("Process Time: " & Millisecs & " ms")