DateTime.ToLocalTime() gives inconsistent results

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all,

DateTime.ToLocalTime() gives inconsistent results upon shifting the time
zone of the machine on which the program is running.

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();
}
:
:
}

Your comments are higly appreciated.

Thank you all.
 
Avinash P. said:
DateTime.ToLocalTime() gives inconsistent results upon shifting the time
zone of the machine on which the program is running.

What do you mean by inconsistent results? The results certainly change
depending on the time zone - that's half the point! Could you give an
example of what you think is wrong?
 
Thank you Jon for your reply.

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 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()...

Could you please help by commenting on this...

I would really appreciate your help...

Thank you so much.
 
Avinash P. said:
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.

<snip>

I think part of your problem is that you're parsing a date - why are
you doing that at all, if it's stored in the database, presumably
(hopefully!) as a DateTime? I would suggest that with your code as it
is, it's not easy to tell whether the problem is in DateTime.Parse or
DateTime.ToLocalTime. Separate them out.

I found your example somewhat hard to follow, unfortunately.
Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
Hi Jon,

I used the SCP (short but complete program) guidelines and came up with the
following lines of code, trying to get it as much close to the piece of code
in the actual program.

Please note that like I said in my previous post which was not answered that
this code works perfectly fine on the Dev. machine. We are having this issue
only on the QA machine. I tried running the newly created sample code that I
am attaching here works fine on QA machine, too. So, I have to look further
in the main program as to what is causing it.

// ********************Code BEGINS
using System;

namespace DtTestConsoleApp
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string strDate = string.Empty;
Console.WriteLine("*************************");
Console.WriteLine("1. Fetch Scenario: Using UTC Time to get Local Time:");
Console.WriteLine("Universal Time:");
strDate = Convert.ToString(System.DateTime.Now.ToUniversalTime());
Console.WriteLine(strDate);
Console.WriteLine("Local Time:");
Console.WriteLine(ConvertToLocalDateTime(System.DateTime.Parse(strDate)));
Console.WriteLine("*************************");
strDate = Convert.ToString(System.DateTime.Now);
Console.WriteLine("2. Save Scenario: Using Local Time to get Universal
Time:");
Console.WriteLine("Local Time:");
Console.WriteLine(strDate);
Console.WriteLine("Universal Time:");
Console.WriteLine(ConvertToUTCDateTime(System.DateTime.Parse(strDate)));
Console.WriteLine("*************************");
Console.Read();
}

public static DateTime ConvertToLocalDateTime(DateTime UTCDateTime)
{
Console.WriteLine("Inside ConvertToLocal...Supplied UTC
Time:"+Convert.ToString(UTCDateTime));
return UTCDateTime.ToLocalTime();
}

public static DateTime ConvertToUTCDateTime(DateTime localDateTime)
{
Console.WriteLine("Inside ConvertToUTC...Supplied Local
Time:"+Convert.ToString(localDateTime));
return localDateTime.ToUniversalTime();
}
}
}
// ********************* Code ENDS

Thanks and regards.
 
Avinash P. said:
I used the SCP (short but complete program) guidelines and came up with the
following lines of code, trying to get it as much close to the piece of code
in the actual program.

Please note that like I said in my previous post which was not answered that
this code works perfectly fine on the Dev. machine. We are having this issue
only on the QA machine. I tried running the newly created sample code that I
am attaching here works fine on QA machine, too. So, I have to look further
in the main program as to what is causing it.

Right. You might want to start by trying the same strings that are
causing problems on your QA box in a sample program.
 
Back
Top