Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is an implementation in C# of how to parse and convert a DateTime to and from its RFC-3339 representation. The only restriction it has is that the DateTime is in Coordinated Universal Time (UTC).</p> <pre><code>using System; using System.Globalization; namespace DateTimeConsoleApplication { /// &lt;summary&gt; /// Provides methods for converting &lt;see cref="DateTime"/&gt; structures to and from the equivalent RFC 3339 string representation. /// &lt;/summary&gt; public static class Rfc3339DateTime { //============================================================ // Private members //============================================================ #region Private Members /// &lt;summary&gt; /// Private member to hold array of formats that RFC 3339 date-time representations conform to. /// &lt;/summary&gt; private static string[] formats = new string[0]; /// &lt;summary&gt; /// Private member to hold the DateTime format string for representing a DateTime in the RFC 3339 format. /// &lt;/summary&gt; private const string format = "yyyy-MM-dd'T'HH:mm:ss.fffK"; #endregion //============================================================ // Public Properties //============================================================ #region Rfc3339DateTimeFormat /// &lt;summary&gt; /// Gets the custom format specifier that may be used to represent a &lt;see cref="DateTime"/&gt; in the RFC 3339 format. /// &lt;/summary&gt; /// &lt;value&gt;A &lt;i&gt;DateTime format string&lt;/i&gt; that may be used to represent a &lt;see cref="DateTime"/&gt; in the RFC 3339 format.&lt;/value&gt; /// &lt;remarks&gt; /// &lt;para&gt; /// This method returns a string representation of a &lt;see cref="DateTime"/&gt; that /// is precise to the three most significant digits of the seconds fraction; that is, it represents /// the milliseconds in a date and time value. The &lt;see cref="Rfc3339DateTimeFormat"/&gt; is a valid /// date-time format string for use in the &lt;see cref="DateTime.ToString(String, IFormatProvider)"/&gt; method. /// &lt;/para&gt; /// &lt;/remarks&gt; public static string Rfc3339DateTimeFormat { get { return format; } } #endregion #region Rfc3339DateTimePatterns /// &lt;summary&gt; /// Gets an array of the expected formats for RFC 3339 date-time string representations. /// &lt;/summary&gt; /// &lt;value&gt; /// An array of the expected formats for RFC 3339 date-time string representations /// that may used in the &lt;see cref="DateTime.TryParseExact(String, string[], IFormatProvider, DateTimeStyles, out DateTime)"/&gt; method. /// &lt;/value&gt; public static string[] Rfc3339DateTimePatterns { get { if (formats.Length &gt; 0) { return formats; } else { formats = new string[11]; // Rfc3339DateTimePatterns formats[0] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"; formats[1] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffffK"; formats[2] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffK"; formats[3] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffK"; formats[4] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffK"; formats[5] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffK"; formats[6] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fK"; formats[7] = "yyyy'-'MM'-'dd'T'HH':'mm':'ssK"; // Fall back patterns formats[8] = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK"; // RoundtripDateTimePattern formats[9] = DateTimeFormatInfo.InvariantInfo.UniversalSortableDateTimePattern; formats[10] = DateTimeFormatInfo.InvariantInfo.SortableDateTimePattern; return formats; } } } #endregion //============================================================ // Public Methods //============================================================ #region Parse(string s) /// &lt;summary&gt; /// Converts the specified string representation of a date and time to its &lt;see cref="DateTime"/&gt; equivalent. /// &lt;/summary&gt; /// &lt;param name="s"&gt;A string containing a date and time to convert.&lt;/param&gt; /// &lt;returns&gt;A &lt;see cref="DateTime"/&gt; equivalent to the date and time contained in &lt;paramref name="s"/&gt;.&lt;/returns&gt; /// &lt;remarks&gt; /// The string &lt;paramref name="s"/&gt; is parsed using formatting information in the &lt;see cref="DateTimeFormatInfo.InvariantInfo"/&gt; object. /// &lt;/remarks&gt; /// &lt;exception cref="ArgumentNullException"&gt;&lt;paramref name="s"/&gt; is a &lt;b&gt;null&lt;/b&gt; reference (Nothing in Visual Basic).&lt;/exception&gt; /// &lt;exception cref="FormatException"&gt;&lt;paramref name="s"/&gt; does not contain a valid RFC 3339 string representation of a date and time.&lt;/exception&gt; public static DateTime Parse(string s) { //------------------------------------------------------------ // Validate parameter //------------------------------------------------------------ if(s == null) { throw new ArgumentNullException("s"); } DateTime result; if (Rfc3339DateTime.TryParse(s, out result)) { return result; } else { throw new FormatException(String.Format(null, "{0} is not a valid RFC 3339 string representation of a date and time.", s)); } } #endregion #region ToString(DateTime utcDateTime) /// &lt;summary&gt; /// Converts the value of the specified &lt;see cref="DateTime"/&gt; object to its equivalent string representation. /// &lt;/summary&gt; /// &lt;param name="utcDateTime"&gt;The Coordinated Universal Time (UTC) &lt;see cref="DateTime"/&gt; to convert.&lt;/param&gt; /// &lt;returns&gt;A RFC 3339 string representation of the value of the &lt;paramref name="utcDateTime"/&gt;.&lt;/returns&gt; /// &lt;remarks&gt; /// &lt;para&gt; /// This method returns a string representation of the &lt;paramref name="utcDateTime"/&gt; that /// is precise to the three most significant digits of the seconds fraction; that is, it represents /// the milliseconds in a date and time value. /// &lt;/para&gt; /// &lt;para&gt; /// While it is possible to display higher precision fractions of a second component of a time value, /// that value may not be meaningful. The precision of date and time values depends on the resolution /// of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's /// resolution is approximately 10-15 milliseconds. /// &lt;/para&gt; /// &lt;/remarks&gt; /// &lt;exception cref="ArgumentException"&gt;The specified &lt;paramref name="utcDateTime"/&gt; object does not represent a &lt;see cref="DateTimeKind.Utc"&gt;Coordinated Universal Time (UTC)&lt;/see&gt; value.&lt;/exception&gt; public static string ToString(DateTime utcDateTime) { if (utcDateTime.Kind != DateTimeKind.Utc) { throw new ArgumentException("utcDateTime"); } return utcDateTime.ToString(Rfc3339DateTime.Rfc3339DateTimeFormat, DateTimeFormatInfo.InvariantInfo); } #endregion #region TryParse(string s, out DateTime result) /// &lt;summary&gt; /// Converts the specified string representation of a date and time to its &lt;see cref="DateTime"/&gt; equivalent. /// &lt;/summary&gt; /// &lt;param name="s"&gt;A string containing a date and time to convert.&lt;/param&gt; /// &lt;param name="result"&gt; /// When this method returns, contains the &lt;see cref="DateTime"/&gt; value equivalent to the date and time /// contained in &lt;paramref name="s"/&gt;, if the conversion succeeded, /// or &lt;see cref="DateTime.MinValue"&gt;MinValue&lt;/see&gt; if the conversion failed. /// The conversion fails if the s parameter is a &lt;b&gt;null&lt;/b&gt; reference (Nothing in Visual Basic), /// or does not contain a valid string representation of a date and time. /// This parameter is passed uninitialized. /// &lt;/param&gt; /// &lt;returns&gt;&lt;b&gt;true&lt;/b&gt; if the &lt;paramref name="s"/&gt; parameter was converted successfully; otherwise, &lt;b&gt;false&lt;/b&gt;.&lt;/returns&gt; /// &lt;remarks&gt; /// The string &lt;paramref name="s"/&gt; is parsed using formatting information in the &lt;see cref="DateTimeFormatInfo.InvariantInfo"/&gt; object. /// &lt;/remarks&gt; public static bool TryParse(string s, out DateTime result) { //------------------------------------------------------------ // Attempt to convert string representation //------------------------------------------------------------ bool wasConverted = false; result = DateTime.MinValue; if (!String.IsNullOrEmpty(s)) { DateTime parseResult; if (DateTime.TryParseExact(s, Rfc3339DateTime.Rfc3339DateTimePatterns, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out parseResult)) { result = DateTime.SpecifyKind(parseResult, DateTimeKind.Utc); wasConverted = true; } } return wasConverted; } #endregion } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload