Solved

Setting Time Zone for Windows Form App

Posted on 2008-06-11
6
444 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 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