I have read through multiple posts here until my head was ready to explode with little lines on the atlas strewn all over.
Here is what I wanted to do:
- I have a calendar full of appointments to display for a business.
- The business is in New York (EDT)
- My dev machine is in PST
- Production Servers are in California (PST)
- Calendar data is stored in the DB in UTC
All I want to do is take a time range specified in EDT (say May 15, 9am – 5pm) and show all the appointments on the calendar.
So, my calendar control tells me “I want the appointments from 5/15/12 9am EDT – 5/15/12 5pm EDT”
I say fine, I will call a db proc and pass the date values in UTC, ie (5/15/12 13:00 UTC – 5/15/12 21:00 UTC). Then when I get them, I will convert them back to EDT before handing them to you.
However, Little did I know that .NET will find this simple task to trip me up.
I got the TimeZoneInfo just fine by using:
TimeZoneInfo zoneInfo = TimeZoneInfo.FindSystemTimeZoneById("Eastern Standard Time")
But that is as far as things worked.
Here’s what I tried next:
DateTime rangeStartUTC = TimeZoneInfo.ConvertTimeToUtc(rangeStart, zoneInfo);
EXCEPTION: Kind is not properly specified.
(WTF is Kind?) I told you the time I want to convert, I told you it is in EDT, I told you I want to convert it to UTC. What more do you want? I could do it by hand. F**ing do it already.
So I tried to set the Kind, but it only has two values! Local or Utc. But my time is neither local, nor Utc. Why in the hell are you asking me for a time zone info then? Cant you just tell what the local time zone is by asking the system clock? Predictably, neither worked. (yeah, according to the docs there is UnSpecified, but again according to the docs, it doesn’t really do anything and yes I tried that one too).
Then, I tried:
TimeZoneInfo.ConvertTime(rangeStart, zoneInfo, TimeZoneInfo.Utc)
SAME LAME EXCUSE!
Time to read some more of St. Jon Skeet’s passages.
What do you know, there is a new class DateTimeOffset. It will solve all your problems.
God bless for all the nice and merciful .NET 4.0 bounties…
DateTimeOffset offStartTime = new DateTimeOffset(rangeStart, zone.GetUtcOffset(rangeStart));
rangeStartUTC = offStartTime.UtcDateTime;
EXCEPTION: "Offset should be 0 for Utc dates"
Gaaaaah!
How the heck did you conclude that the rangeStart is Utc? Did I ever tell you that?
A bunch of people are quoting
TimeZoneInfo.ConvertTime(rangeStart, zone) as the solution, how is it “Kind” enough to work for them? Unless their source time zone conveniently happens to be the same as their local time zone.
So what is a poor .NET C# developer to do?
Try
DateTime.SpecifyKind(rangeStart, DateTimeKind.Unspecified)beforeDateTimeOffsetorTimeZoneInfo. It should not do any conversion of the time itself, just change theKind.As I understand it, the
KindonDateTimeshould be eitherUnspecifiedor correspond to the result of privateTimeZoneInfo.GetCorrespondingKind()method, which returnsLocalforTimeZoneInfo.LocalandUtcforTimeZoneInfo.Utc.UPDATE: Sorry, get the original answer wrong, should be all good now.