c# - DateTime.UtcNow 应该有时区偏移量吗?

标签 c# .net datetime utc linqpad

这个(LINQPad 片段):

DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz").Dump();
DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz").Dump();

返回:

2016-01-08T09:05:04-07:00 // Expected
2016-01-08T16:05:04-07:00 // Not what I expected

鉴于第二个结果是世界时,我预计它会返回 2016-01-08T16:05:04-00:00(时区偏移量为零)。

我是不是遗漏了什么或者这是一个错误?

最佳答案

我想结合Gusman'sEvan's如果他们允许我发表评论并提出一些反对他们的论点。让我们一步一步来..

Should DateTime.UtcNow have a timezone offset?

不管怎样Kind它具有(本地、Utc 或未指定)DateTime 实例不保留任何 UTC 偏移值。期间。

首先,让我们看一下The "zzz" format specifier文档;

With DateTime values, the "zzz" custom format specifier represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's DateTime.Kind property. For this reason, the "zzz" format specifier is not recommended for use with DateTime values.

根据前两个粗体句子,LINQPad 生成的两个结果完全符合预期。您也可以在 Visual Studio 中获得相同的结果(当然需要日期和时间部分)。

只需以“不调试开始”方式运行此代码 (Ctrl + F5)

Console.WriteLine(DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz"));

但是当您使用 (F5) 作为“开始调试”运行它时呢?

在这种情况下有两个选项取决于您的 DateTimeInvalidLocalFormatManaged Debugging Assistants是否勾选。此部分位于 Debug 菜单上,然后单击 Exceptions

enter image description here

如果这个选项被勾选,你会得到一个异常,它说;

A UTC DateTime is being converted to text in a format that is only correct for local times. This can happen when calling DateTime.ToString using the 'z' format specifier, which will include a local time zone offset in the output. In that case, either use the 'Z' format specifier, which designates a UTC time, or use the 'o' format string, which is the recommended way to persist a DateTime in text.

如果不勾选这个选项,你将不会收到任何消息,结果将与 LINQPad 相同。

Given that the 2nd result is universal time, I expected it to return 2016-01-08T16:05:04-00:00 (timezone offset of zero).

正如我所解释的,zzz 格式说明符不会表现得像任何 DateTime 实例。 p>

但它对于 DateTimeOffset 的表现不同记录为的实例;

With DateTimeOffset values, this format specifier represents the DateTimeOffset value's Offset from UTC in hours.

DateTimeOffset.NowDateTimeOffset.UtcNow无论您在 LINQPad 还是 Visual Studio 中运行,都完全符合您的期望,因为它们被记录为;

现在

A DateTimeOffset object whose date and time is the current local time and whose offset is the local time zone's offset from Coordinated Universal Time (UTC).

UtcNow

An object whose date and time is the current Coordinated Universal Time (UTC) and whose offset is TimeSpan.Zero.

Console.WriteLine(DateTimeOffset.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"));
Console.WriteLine(DateTimeOffset.UtcNow.ToString("yyyy-MM-ddTHH:mm:sszzz"));

会产生

2016-01-08T09:05:04-07:00
2016-01-08T16:05:04-00:00

Tl;dr 我认为这不是一个错误。 LINQPad 生成正确的结果完全它应该是什么。在 Visual Studio 中,如果选项在 Debug模式下被选中或未被选中,您可能会得到一个异常。

关于c# - DateTime.UtcNow 应该有时区偏移量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34681509/

相关文章:

C#: While 循环结束时有条件

c# - 在数组中使用 Person 类

.net - DataContractSerializer : Deserialized object with reference id 'xyz' not found in stream. 如何恢复数据?

c# - 为什么我应该在使用位图时保持流打开

c# - 处理和存储耗时

c# - 使用 SSIS - 查询事件目录的来源

c# - C# 中的部分接口(interface)

javascript - 如何设置 Bootstrap 按钮以使其一键禁用

python - 在 panda DataFrame 列中获取上个月的第一个工作日

sql - 如何从 SQL 中的 DATETIME 列获取 DATE?