DateTimeStyles.RoundtripKind枚举是什么意思? [英] What does DateTimeStyles.RoundtripKind enumeration mean?

查看:130
本文介绍了DateTimeStyles.RoundtripKind枚举是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此处中阅读了答复者的帖子遇到了我试图理解的此枚举值 DateTimeStyles.RoundtripKind 。我调查了MSDN 此处表示:

I was reading into answerer's post here where I ran into this enumeration value DateTimeStyles.RoundtripKind which I'm trying to understand. I looked into MSDN here which says:


当DateTime对象
为时,保留日期的DateTimeKind字段使用 o或 r标准格式的
说明符将其转换为字符串,然后将该字符串转换回DateTime对象。

The DateTimeKind field of a date is preserved when a DateTime object is converted to a string using the "o" or "r" standard format specifier, and the string is then converted back to a DateTime object.

我提到的帖子中输入的时间戳是这样的:

The timestamp in the input in the post I referred is like this:

<timestamp time='2016-09-16T13:45:30'>

我运行了她的代码,它仍然有效。现在,连接我拥有的所有信息全是一团糟:

I ran her code and it still works. Now it is all a mess to connect all the information I have:


  1. 上面的时间戳包含一些标识符 T

  2. MSDN文档讨论了 o r 格式说明符,它不告诉它是什么?

  3. 如果要深入了解 DateTimeKind 的更多详细信息我在上面引用的MSDN链接上的枚举对 o r 格式说明符一无所知。 此处是这样的链接:

  1. The above time stamp contains some identifier T
  2. The MSDN documentation talks about o and r format specifiers which it doesn't tell what it is?
  3. If you go into dig more details on DateTimeKind enumeration on the MSDN link I've quoted above it says nothing about o and r format specifiers. Here is the link which says:

Member Name   |      Description
--------------------------------------------------------------------------------

Local         |      The time represented is local time.

Unspecified   |      The time represented is not specified as either local time or Coordinated Universal Time (UTC).

Utc           |      The time represented is UTC.


PS I尝试在上面创建一个表,但似乎SO不支持创建表格结构。

P.S. I tried creating a table above but it seems SO has no native support for creating tabular structures.

所以有人可以帮助我了解 DateTimeStyles.RoundtripKind 枚举及其工作原理?

So can someone help me understand DateTimeStyles.RoundtripKind enumeration and how it works?

推荐答案

所以我终于能够理解这一点并在此处共享相同的信息如果它对其他人也有帮助:

So I was finally able to understand this and sharing the same information here if it can be helpful for others too:

第一部分是将C#DateTime对象转换为字符串。有许多格式说明符可以执行此操作,但是对于我们来说,关于 DateTimeStyles.RoundtripKind 来说, r和 o格式说明符是我们关注的问题。您可以看到所有日期时间格式说明符此处。看看使用以下格式说明符进行代码转换时会发生什么:

First part is conversion of C# DateTime object into string. There are many format specifiers to do that but for us "r" and "o" format specifiers are of concern to us with regards to DateTimeStyles.RoundtripKind. You can see all date time format specifiers here. See what happens when we do the conversion in code using these format specifiers:

//r corresponds to RFC 1123 format (GMT date time format)
var gmtDateTimeString = DateTime.Now.ToString("r"); //gives Fri, 23 Sep 2016 15:39:21 GMT 

//o corresponds to ISO 8601 (Local date time format)
var localDateTimeString = DateTime.Now.ToString("o"); //gives 2016-09-23T15:39:21.8899216+05:30

您可以清楚地看到字符串日期输出的时间中包含嵌入的信息,这表明:

You can clearly see that string date time being output has the information embedded inside it which suggests:


  • 星期五,2016年9月23日15:39 :21 GMT DateTimeKind.Utc (存在 GMT文本)

  • 2016-09-23T15:39:21.8899216 + 05:30 表示日期时间 DateTimeKind.Local ( T字符为根据 ISO 8601 标准)

  • Fri, 23 Sep 2016 15:39:21 GMT is of DateTimeKind.Utc ("GMT" text is present)
  • 2016-09-23T15:39:21.8899216+05:30 represents a date time of DateTimeKind.Local ("T" character is present as per ISO 8601 standard)

现在是第二部分。如果我必须将这些日期时间字符串 gmtDateTimeString localDateTimeString 转换回日期时间对象,则需要解析他们。因此,借助于 DateTimeStyles.RoundtripKind 枚举值传递给 DateTime.Parse API,您实际上表示时区信息是

Now comes the second part. If I've to convert these date time strings gmtDateTimeString and localDateTimeString back to a date time object then we need to parse them. So with the help of DateTimeStyles.RoundtripKind enumeration value passed to DateTime.Parse API you actually signify that time zone information is already baked in the string and API parses the date time appropriately using that information.

通常,当日期时间数据以XML格式通过网络传输时,则使用ISO 8601格式。在此线程中发布问题之前,我在所引用的帖子中看到了该内容。因此,在解析从XML文档获得的这种日期时间字符串时,使用 DateTimeStyles.RoundtripKind 根据当前时区信息获取正确的日期时间值是适当的在字符串中。

Normally when date time data is transferred over the wire in XML format then ISO 8601 format is used which I saw in the post which I referred before posting the question in this thread. So while parsing such a date time string obtained from an XML document it was appropriate to use the DateTimeStyles.RoundtripKind to get the right date time value as per the time-zone information present in the string.

这篇关于DateTimeStyles.RoundtripKind枚举是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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