Solved

Setting Time Zone for Windows Form App

Posted on 2008-06-11
6
446 Views
Last Modified: 2013-11-26
I have a .NET visual Studio 2005 question...  I have a client app that runs off desktops across the world.  It allows the user's to run in their local culture.  All of the database access is handled on a central server (located in the US on the east coast in the EST time zone).  We use remoting to enable the client and the "business" (database) code to communicate and subsequently issue stored proc calls.  We wanted to have all date/times that were saved into the database be in the EST timezone (most users are in the US and it seemed confusing to store dates/times in local time zones and really not have a reference as to what time zone it was in and therefore, not knowing REALLY what time an action was initiated).

Anyway, currently, when the users outside of the EST time zone use the system, their local date/time is entered into the database.

Is there an EASY way to change the timezone used by a thread (similar to the way we can change the culture of the thread) so that all interaction for the client thread could be done in the same timezone as the dates/times we want to store in the database?  I don't really want to change the visual format of the dates/times, just the zone.

0
Comment
Question by:ablesa
  • 3
  • 2
6 Comments
 
LVL 7

Expert Comment

by:sj_hicks
ID: 21766300
I reckon the best way would be to write times in your DB in UTC.  Easy to convert from local to UTC (myLocalTime.ToUniversaTime) and back the other way (myUTCTime.ToLocalTime).
0
 
LVL 11

Expert Comment

by:AkisC
ID: 21767378
What I would do is to have an extra filed in my database (e.g. EST_Timezone) and set its default value to Now()
So any time a user adds a new record the field will have the server time
0
 

Author Comment

by:ablesa
ID: 21770370
thanks to you both for your comments.  Those solutions might have been the way to go 9 months ago, but now, we are testing and getting ready to deploy to users by Aug 1.  It seems that I will have to convert the dates to EST before running the stored procs....  If you have any other "quick fix" ideas, please let me know.

Thanks!
0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 11

Expert Comment

by:AkisC
ID: 21775379
I still suggest... Have an extra filed in your database (e.g. EST_Timezone) and set its default value to Now(), then have the stored procs to return this value to you. It is safest than converting it programmatically
0
 

Accepted Solution

by:
ablesa earned 0 total points
ID: 21802539
As I dug into this issue more, I found that the real problem was in the serialization of the data coming back from the server.  It was taking any date values passed to the client and converting them to their local timezone instead of just passing the date back as it was when it was fetched from the database.  After some digging, I learned why it was converting the dates on the serialization...

I am on Framework 2.0 and within ADO.Net 2.0, there's a property on the dataTable Columns called DataSetDateTime and it can have 4 different values:
Local
 DateTime is always stored in Local. If Utc or Unspecified is assigned to a column in this mode, it is first converted into Local. Serialization in this mode is always performed in Local. There is an offset during serialization.
 
Unspecified
 DateTime is always stored in Unspecified. If Local or Utc is assigned to a column in this mode, it is first converted into Unspecified. Serialization in this mode does not cause an offset.
 
UnspecifiedLocal
 DateTime is stored in Unspecified. If Local or Utc is assigned to a column in this mode, it is first converted into Unspecified. Serialization in this mode causes offset. This is the default behavior and is backward compatible. This option should be thought of as being Unspecified in storage but applying an offset that is similar to Local during serialization.
 
Utc
 DateTime is stored in Universal Coordinated Time (UTC). If Local or Unspecified is assigned to a column in this mode, it is first converted into Utc format. Serialization in this mode is always performed in Utc. There is no offset during serialization.
 

I examined my datatable date columns right after it was pulled from SQL Server and it had DataSetDateTime set to UnspecifiedLocal (which is the default).  I put some code in to loop thru the datatable columns and for those that are date types, I set their DataSetDateTime to Unspecified and it worked...  It stopped converting my dates to the user's local time zone.

Thanks to you both for trying to help solve this.  Just thought I'd put my solution here in case it was useful to someone else.
0
 
LVL 11

Expert Comment

by:AkisC
ID: 21802901
Thank you for taking the time to make us "smarter" with your time investment.

Have fun coding...;)
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question