DateTime.Ticks,DateTime.Equals和时区 [英] DateTime.Ticks, DateTime.Equals and timezones

查看:162
本文介绍了DateTime.Ticks,DateTime.Equals和时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码(在C#中)如何返回false:

  DateTime d = DateTime.Now; 
d.Ticks == d.ToUniversalTime()。 // false

我希望DateTime的刻度是基于UTC时间。
DateTime.Tick 中的MSDN页面提到


此属性的值表示0001年1月1日午夜12:00:00之后的100纳秒间隔数,表示DateTime.MinValue。它不包括归因于闰秒的蜱数。


1月1日午夜,0001 ..哪个时区?



为什么DateTime.Ticks应该是时区依赖?



我猜,蜱的不同之处在于为什么以下代码也返回false

  DateTime d = DateTime.Now; 
d == d.ToUniversalTime(); // false

DateTime.Equals 提到


t1和t2如果他们的Ticks是相等的属性值相等。我们的期望是DateTime.Ticks将是相等的,不管是否是平等的,而不考虑他们的亲属属性值。




<时区。



我希望有时间的时间相等,无论他们发生了什么时区。我的期望是错误的吗?

解决方案

源:http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/fde7e5b0-e2b9-4d3b-8a63-c2ae75e316d8


DateTime.Ticks被记录为午夜12:00:00以来已经过去的100纳秒间隔
,1月1日,0001。那是当地时间1月1日的
。如果您将DateTime转换为UTC,那么Ticks
将是0001年1月1日凌晨12:00:00之后经过
的100纳秒间隔。可能不同的
当地时间1月1日,ergo两个Ticks值将是
不同。



How come the following code (in C#) returns false :

DateTime d = DateTime.Now;
d.Ticks == d.ToUniversalTime().Ticks; // false

I'd expect the ticks of a DateTime to be based on UTC time. The MSDN page on DateTime.Ticks mentions says

The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.

Midnight on January first, 0001 .. in which timezone ?

Why would DateTime.Ticks be timezone dependant ?

I guess that the fact that the Ticks are different is why the following code also returns false

DateTime d = DateTime.Now;
d == d.ToUniversalTime(); // false

The MSDN doc on DateTime.Equals mentions

t1 and t2 are equal if their Ticks property values are equal. Their Kind property values are not considered in the test for equality.

My expectation was that DateTime.Ticks would be equal, no matter the timezone.

I'd expect two moments in time to be equal no matter on what timezone they happened. Are my expectations wrong ?

解决方案

source: http://social.msdn.microsoft.com/Forums/en/netfxbcl/thread/fde7e5b0-e2b9-4d3b-8a63-c2ae75e316d8

DateTime.Ticks is documented as "number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001". That is 1-Jan-0001 local time. If you convert your DateTime to UTC, Ticks will then be number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 UTC. Potentially different that 1-Jan-0001 local time, ergo the two Ticks values will be different.

这篇关于DateTime.Ticks,DateTime.Equals和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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