Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't .NET convert the time correctly?

In the year 2013, the United Kingdom won't start daylight savings time until March 31st.

Still, TimeZoneInfo.ConvertTimeToUtc subtracts one hour when I try to convert a given time to UTC time.

Why is this? Do I need to tell this method that daylight savings time is not active?

var now = new DateTime(DateTime.Now.Ticks, DateTimeKind.Unspecified);
var convertedTime = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time"));
Console.WriteLine(convertedTime); // subtracts one hour, even though England time and GMT should be equal
like image 456
Jim G. Avatar asked Mar 17 '13 00:03

Jim G.


People also ask

How to Convert time to DateTime in c#?

You can use DateTime. TryParse() : which will convert the specified string representation of a date and time to its DateTime equivalent and returns a value that indicates whether the conversion succeeded.

How do I get UTC time in C#?

ToUniversalTime() Method in C# This method is used to convert the value of the current DateTime object to Coordinated Universal Time (UTC). Syntax: public DateTime ToUniversalTime ();

Why is my date not converting to datetime or date?

The error mentioned above normally arises when the date literal is not proper and cannot be converted from the string into DateTime or date. This error results due to a number of reasons, which we will discuss in detail along with the solution set.

Why conversion failed when converting date and/or time from character string?

When we try to convert date or time from character string following error arises sometimes. “Conversion failed when converting date and/or time from character string.” The error mentioned above normally arises when the date literal is not proper and cannot be converted from the string into DateTime or date.

Does the timetime setting affect internet connectivity?

Time settings typically don't affect internet connectivity, however if it is significantly off, PKI certificates can show as expired or not yet valid, causing browser warnings. Another case is if your endpoint is required to authenticate with the network before being granted access. (RADIUS, 802.1x).

How to convert a datetime from one time zone to another?

For example, the TimeZoneInfo.ConvertTimemethod can be used to convert a DateTimefrom one time zone to another. Passing it a whole date makes no sense, as only a single point in time on that date could possibly be converted.


2 Answers

You are simply using the wrong time zone. England is in the "GMT Standard Time" zone.

The "W. Europe Standard Time" zone is for continental Europe, Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna, UTC+01:00.

like image 108
Hans Passant Avatar answered Sep 22 '22 13:09

Hans Passant


[NOTE: This answer assumes the computer running the code is in the GMT timezone]

It's because "W. Europe Standard time" isn't the same timezone as UTC according to .Net (even though it should be). This is very odd, and seems like a bug in the .Net libraries.

Due to this, you've inadvertently told it to convert a time to UTC and passed it a UTC (i.e. GMT time) but then told it that the time is in "W. Europe Standard time", which is an hour ahead of UTC.

So naturally, the resulting time in an hour different.

Step by step:

var now = new DateTime(DateTime.Now.Ticks, DateTimeKind.Unspecified);

This sets now to the time in UTC.

var convertedTime = TimeZoneInfo.ConvertTimeToUtc(now, TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time")

This converts now to UTC, assuming that now is in "W. Europe Standard time", i.e. UTC+1. But it isn't! Therefore the result is incorrect.

[EDIT in response to comments below]

Note that "W. Europe Standard time" should be UTC, but it isn't for some reason. And when you do this:

TimeZoneInfo.FindSystemTimeZoneById("W. Europe Standard time")

it returns UTC+1 rather than the (apparently) correct UTC. It even changes the name to "W. Europe Daylight Time". Seems like it might be a curious bug?

[Second Edit]

Check out the Microsoft Time Zone Index Values (and also here). (Note Neither of those links are actually for Windows 7, but they contain the same definitions.)

It has an entry for "W. Europe Standard Time" which definitely states (GMT+01:00) Amsterdam, Berlin, Bern, Rome, Stockholm, Vienna

like image 25
Matthew Watson Avatar answered Sep 20 '22 13:09

Matthew Watson