How do I get .NET WinForms components to use a SET timezone instead of clients time zone?

600 Views Asked by At

I was not looking forward to describing this problem until I found another forum web site with the EXACT SAME question I need answered (but with no answer.) So with credit to this guy here is my question...

We have a large Windows .Net application (winform executable) that gets installed on the client's desktop. This application calls web services on a server in a different timezone. Virtually all date oriented components detect the timezone difference and automatically adjust the datetime values (returned in dataSets generated by SQL queries), which is generally desirable in most applications but causing problems with accounting related applications that are "date" not "datetime" oriented. We are only interested in the "date" portion. However, a date of 1/1/2003 GMT-5 is automatically converted to 12/31/2002 11:00 GMT-6 on the client. Rather than going through all of the code and extracting the UniversalTime to get back to 1/1/2003 for visual purposes we would like to simply "fake" the timezone for the client-side executable by making it think it's in the same timezone as the server.

Question: Can we set the TimeZone programmatically for the currently running instance only, rather than the global setting?

I really don't have much to add because it is our EXACT issue. In our case we have ActiveReports that are fetching remote SQL data into a datasets and then binding the report to the dataset. So, for example, birthdays are wrong because we are storing the Date for them and X hours are subtracting for the date in the western time zones? So the birthdates are off by minus 1.

Any thoughts?

Thanks!

Seth

1

There are 1 best solutions below

1
On

The way that we address this issue is to serialize string representations of the dates.

The property implementations for the string dates perform the appropriate conversion to and from the actual date properties.

For example:

    [NonSerialized]
    public DateTime MyDate { get; set; }

    [System.Xml.Serialization.XmlAttribute("MyDate")]
    public string MyDateString
    {
        get
        {
            if (MyDate.Equals(DateTime.MinValue))
            {
                return string.Empty;
            }
            else
            {
                return MyDate.ToShortDateString();
            }
        }
        set
        {
            DateTime dtTemp = DateTime.MinValue;
            if (DateTime.TryParse(value, out dtTemp))
            {
                MyDate = dtTemp;
            }
            else
            {
                MyDate = DateTime.MinValue;
            }
        }
    }