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

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

问题描述

[更新格式说明不是一回事格式字符串;格式说明是一块定制格式的字符串,其中格式字符串是存量,并且不提供定制。我的问题是不是说明符格式的]

我一直在试图与使用ZZZ格式说明,我知道,势必为本地时间格式字符串执行往返日期时间转换。所以,如果我试图往返与UTC日期和时间,它抛出一个异常DateTimeInvalidLocalFormat,它应该有这样的文字:

一个UTC日期时间正在转换的格式只对本地时间正确的文本。使用Z格式说明,其中将包括本地时区中的输出偏移调用时,则DateTime.ToString可能发生这种情况。 在这种情况下,既可以使用'Z'格式说明,它指定UTC时间,或使用'O'格式字符串,这是坚持以文本为DateTime的推荐方式。路过一个DateTime时由XmlConvert或DataSet的序列化这也可能发生。如果使用XmlConvert.ToString,传递XmlDateTimeSerializationMode.RoundtripKind正确序列化。如果使用的数据集,设置DataColumn对象来DataSetDateTime.Utc的DateTimeMode。

根据这个建议,我需要做的就是我的code的工作是更换ZZZ与ZZZ这样我就可以在UTC格式站立。问题是,'Z'不被任何文档和任何'Z'格式组合我尝试,即'Z','ZZ','ZZZ中发现的,永远只是转换与Z的日期时间例如像文字处理

是否有人忘记实现'Z'瞒着异常消息的作者,还是我缺少的是如何换出有效的本地时间偏移+0000没有黑客?

code示例:

  //这是ZZZZZ重新presenting当地时间幅面胶印机
常量字符串格式=DDD MMM DD HH:MM:SS ZZZZZ YYYY;//创建一个UTC时间
预计=周五12月19日17时24分18秒+0000 2008常量字符串;
VAR时间=新日期时间(2008,12,19,17,24,18,0,DateTimeKind.Utc);//如果你使用调试器,这将理所当然地抛出一个异常
//使用.NET 3.5 SP1,因为'Z'是唯一的本地时间;然而,异常
//要求我使用UTC时间的Z说明,但它不存在,所以它
//刚刚吐出'Z'的文字。
VAR实际= time.ToString(格式,CultureInfo.InvariantCulture);Assert.AreEqual(预期,实际值);


解决方案

也许是K格式说明会有些用处的。这是一个似乎提资金使用Z唯一的一个。

Z是一种用于创建DateTime对象一个唯一的案例。字面上的Z实际上是UTC倍的ISO 8601日期时间标准的一部分。当Z(祖鲁语)上的时间结束上涨,它表明,时间是UTC,所以真的字面Z是部分时间。这可能创造了.NET中的日期格式库中的一些问题,因为它实际上是一个文字,而不是一个格式说明。

[Update: Format specifiers are not the same thing as format strings; a format specifier is a piece of a custom format string, where a format string is 'stock' and doesn't provide customization. My problem is with specifiers not formats]

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:

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.

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.

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?

Code Example:

// 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);

解决方案

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" 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天全站免登陆