Thursday, April 23, 2009

TimeZone Problem

I am working on application which is based on Client/Server model. Earlier server components were deployed as MTS Components but now we have migrated it to WCF Services. It was working all right until the day we deployed our client application in different time zone and server in different timezone. So the date 2nd Dec 2001 12:00:00 (Pacafic) was getting transalated to 1st Dec 2001 09:00:00 on the server (The Ingenious MTS .. huh?). I think its is not property of MTS.. otherwise Microsoft must have give the settings to control it. 

Unfortunately there is not solution for it in MS Visual Studio 2003, but in VS 2005 and later version they have introduced a new property in in datetime datatype called as "SpecifyKind". You can set it to DateTimeKind.Unspecified and it will stop offsetting (adjusting) the date based on different timezone. 

You should set the kind of datetime/date datatype before sending it to server or clients. 

Following is an example:

dim dtDate as datetime
dtDate = DateTime.SpecifyKind(dtDate, DateTimeKind.Unspecified)

Same behavior is seen for datetime column also which can be nutralized using the following code

If ds IsNot Nothing Then
            If ds.Tables.Count > 0 Then
                For Each dt As DataTable In ds.Tables
                    For Each col As System.Data.DataColumn In dt.Columns
                        Try
                            If col.DataType.FullName.ToUpper = "SYSTEM.DATETIME" Then
                                'Unspecified will prevent the offset to happen based on timezones
                                col.DateTimeMode = DataSetDateTime.Unspecified
                            End If
                        Catch ex As Exception
                        End Try
                    Next
                Next
            End If
        End If

hope this will help.