Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does DateTimeStyles.RoundtripKind enumeration mean?

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:

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 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.
    

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 DateTimeStyles.RoundtripKind enumeration and how it works?

like image 731
RBT Avatar asked Sep 19 '16 11:09

RBT


People also ask

What is DateTimeStyles?

DateTimeStyles. The formatting options that customize string parsing for some date and time parsing methods.

What is ParseExact?

ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.

How do I convert a string to a date C#?

In C#, a string can be converted to DateTime object using parsing methods provided by DateTime struct. Apart from these methods, we can convert a string to date using Convert. To DateTime() method which takes a string representation of a date as input and returns its equivalent DateTime object.

What formats does DateTime parse?

Only the ISO 8601 format ( YYYY-MM-DDTHH:mm:ss.sssZ ) is explicitly specified to be supported. Other formats are implementation-defined and may not work across all browsers.


1 Answers

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.

like image 120
RBT Avatar answered Oct 07 '22 05:10

RBT