Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is DateTimeOffset.UtcNow.DateTime equivalent to DateTime.UtcNow?

I need to upgrade some piece of code from statically calling DateTime.UtcNow to calling a time provider service which returns, basically, DateTimeOffset.UtcNow. To further convert this DateTimeOffset instance to DateTime, there is the DateTime property. Alternatively, it seems that there is an implicit conversion from DateTimeOffset to DateTime.

I'm a bit concerned that there might be some edge cases that I can't see right now where these two would not be equivalent. Are there?

like image 698
Andrei Rînea Avatar asked Nov 11 '15 16:11

Andrei Rînea


People also ask

What is DateTimeOffset UtcNow?

The UtcNow property computes the current Universal Coordinated Time (UTC) based on the local system's clock time and an offset defined by the local system's time zone. The precision of the current UTC time's millisecond component depends on the resolution of the system clock.

What's the difference between DateTime and DateTimeOffset?

With its Kind property, DateTime is able to reflect only Coordinated Universal Time (UTC) and the system's local time zone. DateTimeOffset reflects a time's offset from UTC, but it does not reflect the actual time zone to which that offset belongs.

What is difference between DateTime now and DateTime UtcNow?

UtcNow tells you the date and time as it would be in Coordinated Universal Time, which is also called the Greenwich Mean Time time zone - basically like it would be if you were in London England, but not during the summer. DateTime. Now gives the date and time as it would appear to someone in your current locale.

What does DateTimeOffset mean?

The DateTimeOffset structure represents a date and time value, together with an offset that indicates how much that value differs from UTC. Thus, the value always unambiguously identifies a single point in time.


2 Answers

If you look at the value of DateTimeOffset.UtcNow.DateTime.Kind you will see the it is Unspecified. Unspecified kinds are treated as local times by the framework. The kind of DateTime.UtcNow is Utc, so there will be differences when timezone conversions are applied to and from the local timezone.

The work around is to use the DateTimeOffset.UtcNow.UtcDateTime which has the Utc kind specified.

like image 140
Mike Zboray Avatar answered Oct 22 '22 16:10

Mike Zboray


Internally it looks like this:

  public static DateTimeOffset UtcNow {
        get { 
            return new DateTimeOffset(DateTime.UtcNow);
        } 
  } 

  public DateTime DateTime { 
        get {
            return ClockDateTime;
        }
    } 
   private DateTime ClockDateTime { 
        get {
            return new DateTime((m_dateTime + Offset).Ticks, DateTimeKind.Unspecified); 
        }
    }

  public DateTime UtcDateTime { 
        [Pure] 
        get {
            Contract.Ensures(Contract.Result<DateTime>().Kind == DateTimeKind.Utc); 
            return DateTime.SpecifyKind(m_dateTime, DateTimeKind.Utc);
        }
    }

So it looks like you could end up getting a DateTime that was based on UTC but then gets converted to Unspecified.

like image 38
MattC Avatar answered Oct 22 '22 18:10

MattC