Solved

Setting Time Zone for Windows Form App

Posted on 2008-06-11
6
443 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now