DateTime 'Z' 格式说明符在哪里? [英] Where's the DateTime 'Z' format specifier?

查看:39
本文介绍了DateTime 'Z' 格式说明符在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

[更新:格式说明符与格式字符串不同;格式说明符是一段自定义格式字符串,其中格式字符串是 'stock' 并且不提供自定义.我的问题是说明符不是格式]

我一直在尝试使用使用zzz"格式说明符的格式字符串执行往返日期时间转换,我知道该格式说明符绑定到本地时间.因此,如果我尝试使用 UTC 日期时间往返,它会抛出一个 DateTimeInvalidLocalFormat 异常,它应该使用以下文本:

I've been trying to perform roundtrip DateTime conversions with a format string that uses 'zzz' format specifier, which I know is bound to local time. So, if I attempt to round trip with a UTC date time it throws a DateTimeInvalidLocalFormat exception, which it should, with this text:

UTC 日期时间正在转换为仅适用于本地时间的格式的文本.使用z"格式说明符调用 DateTime.ToString 时可能会发生这种情况,该说明符将在输出中包含本地时区偏移量.在这种情况下,请使用指定 UTC 时间的Z"格式说明符,或使用o"格式字符串,这是在文本中保留 DateTime 的推荐方法.当传递要由 XmlConvert 或 DataSet 序列化的 DateTime 时,也会发生这种情况.如果使用 XmlConvert.ToString,请传入 XmlDateTimeSerializationMode.RoundtripKind 以正确序列化.如果使用 DataSet,请将 DataColumn 对象上的 DateTimeMode 设置为 DataSetDateTime.Utc.

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. This can also occur when passing a DateTime to be serialized by XmlConvert or DataSet. If using XmlConvert.ToString, pass in XmlDateTimeSerializationMode.RoundtripKind to serialize correctly. If using DataSet, set the DateTimeMode on the DataColumn object to DataSetDateTime.Utc.

根据这个建议,我需要做的就是让我的代码正常工作,就是将 'zzz' 替换为 'ZZZ',这样我就可以使用 UTC 格式了.问题是,在文档中的任何地方都找不到Z",我尝试过的任何Z"格式组合,即Z"、ZZ"、ZZZ",总是只是将 DateTime 实例转换为将那些 Z 视为文字.

Based on this suggestion, all I need to do to get my code to work is to replace 'zzz' with 'ZZZ' so I can stand in a UTC format. The problem is, 'Z' isn't found anywhere in the documentation and any 'Z' format combination I try, i.e. 'Z', 'ZZ', 'ZZZ', always just converts the DateTime instance with those Z's treated like literals.

是否有人在没有告诉异常消息作者的情况下忘记实现Z",或者我是否错过了如何在不进行黑客攻击的情况下用+0000"换出有效的本地时间偏移量?

Did someone forget to implement 'Z' without telling the exception message author, or am I missing how to swap out a valid local time offset with "+0000" without hacking?

代码示例:

// This is the format with 'zzzzz' representing local time offset
const string format = "ddd MMM dd HH:mm:ss zzzzz yyyy";

// create a UTC time
const string expected = "Fri Dec 19 17:24:18 +0000 2008";
var time = new DateTime(2008, 12, 19, 17, 24, 18, 0, DateTimeKind.Utc);

// If you're using a debugger this will rightfully throw an exception
// with .NET 3.5 SP1 because 'z' is for local time only; however, the exception
// asks me to use the 'Z' specifier for UTC times, but it doesn't exist, so it
// just spits out 'Z' as a literal.
var actual = time.ToString(format, CultureInfo.InvariantCulture);

Assert.AreEqual(expected, actual);

推荐答案

也许K"格式说明符会有用.这是唯一一个似乎提到使用大写Z"的地方.

Maybe the "K" format specifier would be of some use. This is the only one that seems to mention the use of capital "Z".

Z"是 DateTimes 的一种独特情况.文字Z"实际上是 UTC 时间的 ISO 8601 日期时间标准的一部分.当时间末尾加上Z"(祖鲁语)时,表示该时间是 UTC,因此文字 Z 实际上是时间的一部分.这可能会给 .NET 中的日期格式库带来一些问题,因为它实际上是一个文字,而不是格式说明符.

"Z" is kind of a unique case for DateTimes. The literal "Z" is actually part of the ISO 8601 datetime standard for UTC times. When "Z" (Zulu) is tacked on the end of a time, it indicates that that time is UTC, so really the literal Z is part of the time. This probably creates a few problems for the date format library in .NET, since it's actually a literal, rather than a format specifier.

这篇关于DateTime 'Z' 格式说明符在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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