Solved

Setting Time Zone for Windows Form App

Posted on 2008-06-11
6
442 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 11

Expert Comment

by:AkisC
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for taking the time to make us "smarter" with your time investment.

Have fun coding...;)
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

9 Experts available now in Live!

Get 1:1 Help Now