Reputation: 53223
I understand that to refer a single point in time DateTimeOffset is better and more reliable way compared to DateTime as it replaces .Kind property by a more convenient thing that is the Offset to UTC.
Does this solve all the issues regarding to storing a single point in Date-Time or are there still some cases that I should be concerned about? (If there are can you give me examples where DateTimeOffset can't be reliable?)
Thanks
Upvotes: 1
Views: 395
Reputation: 241420
Given a DateTimeOffset
, there is never any confusion about what point in time that represents. So they are always reliable.
But there are some cases where a DateTimeOffset
alone is still not sufficient. Here's an example of a common situation:
DateTimeOffset
with the value 2013-03-10T01:00:00-05:00
.2013-03-10T03:00:00-05:00
.2013-03-10T03:00:00-04:00
.To overcome this situation, you must also know that the time was recorded in New York. You knew that in the first step, but then it was thrown out when you recorded it. Somewhere else in your application, you must hold on to this fact. Preferably, you would keep a time zone id, so that you could re-calculate the correct offset.
If using the TimeZoneInfo
class in your application, then you would want to track the value of the .Id
property, along with your DateTimeOffset
. For New York, the time zone id would be "Eastern Standard Time"
. This is a bit confusing, because this same value is used regardless of whether DST is in effect or not. (There is no Windows time zone with an Id
of "Eastern Daylight Time"
). Also, there is no built-in class or struct that will pair a TimeZoneInfo
with a DateTimeOffset
. You have to do it yourself.
If you are using Noda Time (which I highly recommend). Then you can take advantage of the IANA time zone id of "America/New_York"
, and the ZonedDateTime
object - which is designed for this exact situation.
You should also refer to DateTime vs DateTimeOffset. You should find the analogy there quite useful.
There are also some cases where DateTimeOffset
is not appropriate. Maybe this one is obvious, but it's still worth mentioning.
This happens more often than you would think. For example:
There are other real-world examples where the same point on the calendar is not the same point in time, yet people tend to think of them as if they were.
In Noda Time, you would use a LocalDateTime
for these scenarios. Without Noda Time, you would use a DateTime
with .Kind == Unspecified
.
Upvotes: 1