RBT RBT - 1 year ago 74
C# Question

What does DateTimeStyles.RoundtripKind enumeration mean?

I was reading into answerer's post here where I ran into this enumerator

which I'm trying to understand. I looked into MSDN here which says:

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. The above time stamp contains some identifier

  2. The MSDN documentation talks about
    format specifiers which it doesn't tell what it is?

  3. If you go into dig more details on
    enumeration on the MSDN link I've quoted above it says nothing about
    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.

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

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

Answer Source

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

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:

  • 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)

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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download