DateTimeOffset到DateTime的转换-数据丢失 [英] DateTimeOffset to DateTime conversion - Data Loss

查看:81
本文介绍了DateTimeOffset到DateTime的转换-数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将datetimeoffset值转换为datetime值时,是否有可能丢失数据.在MSDN文档中,提到了从datetimeoffset到datetime的转换,如下所示:

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:

DateTime属性最常用于执行DateTimeOffset到DateTime的转换.但是,它返回一个DateTime值,该值的种类属性未指定.这意味着有关DateTimeOffset值与UTC的关系会由于使用DateTime属性时进行转换.

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.

要指示转换后的DateTime值是UTC时间,可以检索DateTimeOffset.UtcDateTime属性的值.它与DateTime属性有两个不同之处:

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:

它将返回其Kind属性为Utc的DateTime值.如果Offset属性值不等于TimeSpan.Zero,则会将时间转换为UTC.

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.

我看到以下将datetime偏移量转换为datetime的方法:

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;   
}

现在,在我们的系统中,我们以上述方式将datetimeoffset转换为datetime.稍后,我们想将datetime转换回datetimeoffset.

Now in our system we are converting datetimeoffset to datetime in the above way. Later we want to convert the datetime back to datetimeoffset.

例如:

DateTime dt = ConvertFromDateTimeOffset(datetimeOffset);
DateTimeOffset dofsetnew = new DateTimeOffset(dt);

我的问题是,在任何情况下datetimeOffset和dofsetnew是否都不同?如果是这样,那么转换将是损失数据.

My question is whether under any circumstances datetimeOffset and dofsetnew be different ? If so then conversion would be loss data.

推荐答案

编写方式-是的,只要输入DateTimeOffset处于UTC偏移量且该值是0以及您当地时区以外的任何值(最后一个'else'条件)).恕我直言,最好还是始终使用UtcDateTime,前提是可以接受其中涉及的潜在时区转换.

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.

如果需要确保没有损失,请不要将其转换为DateTime并保留DateTimeOffset(SQL服务器类型为"datetimeoffset"),或者如果必须,请保留UTC偏移作为与之一起传递的单独值,因此您可以使用2个值(DateTime和offset)来重构DateTimeOffset.

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).

这篇关于DateTimeOffset到DateTime的转换-数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆