Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DateTime to Utc only if not already Utc

Tags:

c#

.net

datetime

I'm using the DateTimeWithZone struct that Jon Skeet posted at Creating a DateTime in a specific Time Zone in c# fx 3.5

This didn't work exactly for my situation since it assumes that the DateTime passed in the constructor is the local time, and therefore converts it to Utc using the specified TimeZone.

In my case we will mostly be passing in DateTime objects already in Utc (since this is what we are storing) so we need to only perform the conversion if the source DateTime.Kind is not Utc.

Therefore I changed the constructor to:

    public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone, DateTimeKind kind = DateTimeKind.Utc) {
        dateTime = DateTime.SpecifyKind(dateTime, kind);
        utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone);
        this.timeZone = timeZone;
    }

Here we have an optional Kind parameter that defaults to Utc.

However, running this code and passing a Utc DateTime generates the following exception:

The conversion could not be completed because the supplied DateTime did not have the Kind property set correctly. For example, when the Kind property is DateTimeKind.Local, the source time zone must be TimeZoneInfo.Local.

According to the docs (http://msdn.microsoft.com/en-us/library/bb495915.aspx):

If the Kind property of the dateTime parameter equals DateTimeKind.Utc and the sourceTimeZone parameter equals TimeZoneInfo.Utc, this method returns dateTime without performing any conversion.

Since both the input time and the timezone both have a Kind property of Utc then I would not expect to get this exception.

Have I misunderstood?

like image 971
Ben Foster Avatar asked May 27 '11 10:05

Ben Foster


1 Answers

Like the MSDN docs say if you pass in a DateTime with the kind set to anything besides DateTimeKind.Utc and specify a TimeZone other than Utc the conversion function will throw an exception. That Must be what is happening here. In your code you should check if the DateTime is already in Utc and skip the conversion if it is.

Also since the dateTime you are passing in will have a DateTime attached to it already you probably don't need to pass in a separate Kind parameter.

from the docs

Converts the time in a specified time zone to Coordinated Universal Time (UTC).

meaning that it converts from the time zone supplied to Utc

the function throws an argument exception if:

dateTime .Kind is DateTimeKind.Utc and sourceTimeZone does not equal TimeZoneInfo.Utc.

-or-

dateTime .Kind is DateTimeKind.Local and sourceTimeZone does not equal TimeZoneInfo.Local.

-or-

sourceTimeZone .IsInvalidDateTime( dateTime ) returns true.

like image 98
Yaur Avatar answered Sep 28 '22 11:09

Yaur