Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't DateTime.ToLocalTime() take into account daylight savings?

Tags:

c#

.net

datetime

I have a a UTC time string (that I get from a database, so I can't change the format) that is created with DateTime.UtcNow.ToString("s"). I would like to display something user-facing like "10:00 AM". Where I am (in England), the clocks have recently gone forward and the following method is an hour out:

var timenowstring = DateTime.UtcNow.ToString("s");
var dateutc = DateTime.Parse(timenowstring).ToShortTimeString();
var datelocal = DateTime.Parse(timenowstring).ToLocalTime().ToShortTimeString();

Console.WriteLine("Utc time string: " + dateutc);
Console.WriteLine("Local time string: " + datelocal);

Both print "9:02 AM" when actually it's 10:02 AM.

Here's a screenshot of it repro-ing on http://csharppad.com/ :

utc and local show the same time - see system clock in lower right, which is correctbigger image

CSharpPad gist

What am I doing wrong and what's the easiest way to get a DateTime object which will return the right time when I call .ToShortTimeString()?

NB the docs on ToLocalTime() say:

The conversion also takes into account the daylight saving time rule that applies to the time represented by the current DateTime object.

like image 745
user1002973 Avatar asked Apr 10 '15 09:04

user1002973


People also ask

How does ToLocalTime work?

The ToLocalTime method converts a DateTime value from UTC to local time. To convert the time in any designated time zone to local time, use the TimeZoneInfo. ConvertTime method. The value returned by the conversion is a DateTime whose Kind property always returns Local.

What is the function to offset the date for daylight saving in time series?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.


1 Answers

You said:

I have a a UTC time string (that I get from a database, so I can't change the format) that is created with DateTime.UtcNow.ToString("s").

Right off the bat, you have a problem. Dates in a database are (usually) not stored as strings. They're stored in fields with a specific data type. In SQL Server (for example) you may be using a datetime or datetime2 field. These are not strings. When you retrieve them into your .NET code, they are converted directly to a DateTime type. If you are treating it as a string, you are doing it wrong.

For example, your data access code might be doing something like this:

DateTime dt = Convert.ToDateTime(dataReader["myDateTimeField"].ToString());

That is very common, and completely wrong. You should instead be doing this:

DateTime dt = (DateTime) dataReader["myDateTimeField"];

Or if the field is nullable:

DateTime? dt = dataReader["myDateTimeField"] as DateTime;

Once you load the value properly instead of parsing it as a string, the rest will work out fine. The DateTime value will have DateTimeKind.Unspecified for its Kind property, and when you call ToLocalTime on it, it will assume that you wanted to treat the unspecified value as UTC. (See the chart on MSDN.)

Regarding the code you posted, while it's a bit messy (going through strings unnecessarily), it would actually work just fine - assuming you ran it in your time zone. In the ToLocalTime method, "local" means the local time zone setting of the machine wherever the code happens to be running. For csharppad.com, the time zone happens to be UTC. It has no way of knowing you want to use England's time zone rules.

CSharpPad Time Zone

If you intend to run your code on a server, then you shouldn't be using ToLocalTime at all - as the time zone of the server is likely to be irrelevant. Instead, you could use TimeZoneInfo to convert the time:

// this uses the time zone for England
TimeZoneInfo tz = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
DateTime englandDatetime = TimeZoneInfo.ConvertTimeFromUtc(utcDateTime, tz);

Alternatively, you could use the open-source Noda Time library, like this:

DateTimeZone tz = DateTimeZoneProviders.Tzdb["Europe/London"];
DateTime englandDateTime = Instant.FromDateTimeUtc(utcDateTime)
                                  .InZone(tz)
                                  .ToDateTimeUnspecified();
like image 70
Matt Johnson-Pint Avatar answered Oct 06 '22 02:10

Matt Johnson-Pint