When I convert a datetimeoffset value to a datetime value, is there any possibility for data loss. From MSDN documentation, the conversion from datetimeoffset to datetime is mentioned as follows:
The DateTime property is most commonly used to perform DateTimeOffset to DateTime conversion. However, it returns a DateTime value whose Kind property is Unspecified. This means that any information about the DateTimeOffset value's relationship to UTC is lost by the conversion when the DateTime property is used.
To indicate that a converted DateTime value is the UTC time, you can retrieve the value of the DateTimeOffset.UtcDateTime property. It differs from the DateTime property in two ways:
It returns a DateTime value whose Kind property is Utc. If the Offset property value does not equal TimeSpan.Zero, it converts the time to UTC.
I see the following method to convert datetime offset to datetime:
static DateTime ConvertFromDateTimeOffset(DateTimeOffset dateTime)
{
if (dateTime.Offset.Equals(TimeSpan.Zero))
return dateTime.UtcDateTime;
else if (dateTime.Offset.Equals(TimeZoneInfo.Local.GetUtcOffset(dateTime.DateTime)))
return DateTime.SpecifyKind(dateTime.DateTime, DateTimeKind.Local);
else
return dateTime.DateTime;
}
Now in our system we are converting datetimeoffset to datetime in the above way. Later we want to convert the datetime back to datetimeoffset.
As an example:
DateTime dt = ConvertFromDateTimeOffset(datetimeOffset);
DateTimeOffset dofsetnew = new DateTimeOffset(dt);
My question is whether under any circumstances datetimeOffset and dofsetnew be different ? If so then conversion would be loss data.
From MSDN documentation, the conversion from datetimeoffset to datetime is mentioned as follows: The DateTime property is most commonly used to perform DateTimeOffset to DateTime conversion. However, it returns a DateTime value whose Kind property is Unspecified.
This affects DateTimeOffset values that correspond to UTC time or to the system's local time because the DateTime structure reflects only those two time zones in its Kind property.
The datetimeoffset data type allows you to specify a fractional seconds precision from 0 to 7. This is done by using the datetimeoffset (n) syntax. If you don’t specify this, it will use 7 (the default). It also has a time zone offset.
To indicate that a DateTimeOffset value represents the local time, you can pass the DateTime value returned by the DateTimeOffset.DateTime property to the static ( Shared in Visual Basic) SpecifyKind method.
The way it's written - Yes, anytime the input DateTimeOffset is at a UTC offset that's any value other than 0 and your local timezone (that last 'else' condition). IMHO, you're better off just always using UtcDateTime, assuming that the potential timezone conversion involved there is acceptable.
Also, if your local timezone observes DST, then there's loss there during the ambiguous hour each year, since you won't know which one of them it represents.
If you need to ensure there's no loss, either don't convert to DateTime and stay with DateTimeOffset (sql server type 'datetimeoffset') or if you must, keep the UTC offset as a separate value that you pass along with it, so you can then reconstruct the DateTimeOffset with the 2 values (DateTime and offset).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With