In my application I store all transaction dates in UTC in the database.

At the time of displaying the transactions in the UI I convert the date to
local date time and then display the same to the user.

I do the following:

string LocalDtTimeString =
Convert.ToString(DtClass.ConvertToLocalDateTime(System.DateTime.Parse(strDate)));

where strDate is the string representation of the date fetched from the
database.

and the DtClass method implementation is:

public class DtClass
{
:
:
public static DateTime ConvertToLocalDateTime(DateTime UTCDateTime)
{
return UTCDateTime.ToLocalTime();
}
:
:
}

Scenario is:

My UI is in the context of PST. I perform a transaction at 4:30 PM (PST).
Database stores the date as next day morning 12:30 AM (UTC).

Now I changed the Time Zone of my machine from PST (GMT - 8) to
EST (GMT - 5).

I run my UI and see that it shows the transaction time as 10:30 PM (EST)
instead of showing it as 7:30 PM (EST).

This is happening on a test machine.

When I wrote this code and tested it on the development machine, it was
correctly converting times and giving respective local times.

Is there anything that I need to change in the code that will make it work
consistently on all machines?

I looked around regarding this issue on the internet and read some material,
which talks about behavior differences based on the culture notably with
DateTime.Parse and DateTime.ToString.

I don't understand why it subtracts 2 hours instead of 5...

I would really appreciate your help...

Thank you...
-Avinash

RE: Apparent issues while using System.DateTime by AvinashP

AvinashP
Fri Jan 07 14:47:08 CST 2005

I kept thinking about it. I think DateTime.ToLocalTime() is internally
calling two APIs.

1. To check the TimeZone In my case I think here it gets the correct UTC
offset of -5.
2. To check if there was a time zone shift. In my case here it gets an
answer yes and gets the difference between PST and EST to be 3 which it
unfortunately adds to the time calculated in step 1. :(. Hence we get the
time 10:05 PM instead of 7:05PM.

This is just my speculation as to what could possibly be happening inside
the guts of ToLocalTime()...

DateTime.ToLocalTime() developers, could you please help by commenting on
this...

Thank you all so much for your help.
Regards,
Avinash




"Avinash P." wrote:

> In my application I store all transaction dates in UTC in the database.
>
> At the time of displaying the transactions in the UI I convert the date to
> local date time and then display the same to the user.
>
> I do the following:
>
> string LocalDtTimeString =
> Convert.ToString(DtClass.ConvertToLocalDateTime(System.DateTime.Parse(strDate)));
>
> where strDate is the string representation of the date fetched from the
> database.
>
> and the DtClass method implementation is:
>
> public class DtClass
> {
> :
> :
> public static DateTime ConvertToLocalDateTime(DateTime UTCDateTime)
> {
> return UTCDateTime.ToLocalTime();
> }
> :
> :
> }
>
> Scenario is:
>
> My UI is in the context of PST. I perform a transaction at 4:30 PM (PST).
> Database stores the date as next day morning 12:30 AM (UTC).
>
> Now I changed the Time Zone of my machine from PST (GMT - 8) to
> EST (GMT - 5).
>
> I run my UI and see that it shows the transaction time as 10:30 PM (EST)
> instead of showing it as 7:30 PM (EST).
>
> This is happening on a test machine.
>
> When I wrote this code and tested it on the development machine, it was
> correctly converting times and giving respective local times.
>
> Is there anything that I need to change in the code that will make it work
> consistently on all machines?
>
> I looked around regarding this issue on the internet and read some material,
> which talks about behavior differences based on the culture notably with
> DateTime.Parse and DateTime.ToString.
>
> I don't understand why it subtracts 2 hours instead of 5...
>
> I would really appreciate your help...
>
> Thank you...
> -Avinash