用整数调用DateTime.AddDays是否始终保持时间不变? [英] Does calling DateTime.AddDays with a whole number always leave the time unchanged?

查看:25
本文介绍了用整数调用DateTime.AddDays是否始终保持时间不变?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码,尝试获取与当地时间昨天午夜"等效的 DateTime :

Consider the following code which attempts to get a DateTime that's equivalent to the local time "midnight yesterday":

DateTime midnightYesterday = DateTime.Today.AddDays(-1.0d);

总是会导致 DateTime 的时间部分为00:00:00的情况-无论corner日,leap秒或什么是当地时区?

Will this always result in a DateTime with a time component of 00:00:00 -- regardless of any corner cases such as leap days, leap seconds, or what the local time zone is?

更一般的说:调用 DateTime.AddDays 并传递一个整数作为参数,总是会导致返回一个 DateTime 与该时间部分完全相同的时间原始的 Datetime ?

More generally: Does calling DateTime.AddDays, passing a whole number as a parameter, always result in a DateTime being returned that has the exact same time component as the original Datetime?

有关MSDN的文档DateTime.AddDays 不能解决这个特定问题.

The MSDN documentation for DateTime.AddDays does not address this specific question.

推荐答案

  1. DateTime 不代表leap秒.您可以阅读文章,从中您将看到它因此并不真正支持UTC.文档指出:
  1. DateTime does not account for leap seconds. You can read this article from which you will see that because of this it doesn't really support UTC. Documentation states that:

时间值以100纳秒为单位,称为刻度,特定日期是自一月12:00午夜以来的滴答数1,公历0001年(C.E.)(公历除外)将以leap秒添加)

Time values are measured in 100-nanosecond units called ticks, and a particular date is the number of ticks since 12:00 midnight, January 1, 0001 A.D. (C.E.) in the GregorianCalendar calendar (excluding ticks that would be added by leap seconds)

  1. 关于夏时制文档有以下内容:

时区之间的转换操作(例如UTC和当地时间或一个时区与另一个时区之间的时间)时间,却没有算术和比较运算.

这意味着将天(这是算术运算)添加到 DateTime 实例中,即使它具有类型 Local (因此表示本地时区中的时间)也不会花费DST考虑在内.这使得在日期时间以 Local 类型执行任何算术运算是一个非常糟糕的主意.如果您需要使用日期时间进行操作-首先将其转换为UTC(没有DST的概念),然后执行操作,然后再转换回本地(上述 does 所述的转换考虑了DST).

That means that adding days (which is arithmetic operation) to DateTime instance, even if it has kind Local (so represents time in local timezone) does not take DST into account. That makes performing any arithmetic operations on datetimes with kind Local a really bad idea. If you need to do that with date times - first convert it to UTC (that has no notion of DST), perform operation then convert back to local (conversion as stated above does take DST into account).

您还可以查看源代码,以了解datetime值在内部存储为数字(滴答数),添加天数只会对该数字添加固定值.计算小时\分钟\秒使用该值并执行固定操作(仅除法)以获得目标值.这些操作都无法解决leap秒,时区或其他任何问题.因此,您的问题的答案是肯定的.

You can also look at source code to see that datetime value is stored as a number internally (number of ticks) and adding days just adds fixed value to that number. Calculating hour\minute\second use that value and perform fixed operations (just a division) to obtain target value. None of those operations account for anything like leap seconds, time zones or anything else. So the answer to your question is yes.

这篇关于用整数调用DateTime.AddDays是否始终保持时间不变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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