private DateTimeOffset ParseDateTimeOffset(JsonReader reader) { switch (reader.TokenType) { // Return the default value of DateTimeOffset of the JSON value is NULL case JsonToken.Null: return(default); // If the token type is an integer, we assume UNIX time regardles of the format of the converter case JsonToken.Integer: return(TimeUtils.GetDateTimeOffsetFromUnixTime((long)reader.Value)); // If the token type is an integer, we assume UNIX time regardles of the format of the converter case JsonToken.Float: return(TimeUtils.GetDateTimeOffsetFromUnixTime((double)reader.Value)); // Is the value already a date? JSON.net may automatically detect and parse some date formats case JsonToken.Date: switch (reader.Value) { case DateTime dt: return(dt); case DateTimeOffset dto: return(dto); default: throw new JsonSerializationException("Value doesn't match an instance of DateTime or DateTimeOffset: " + reader.Value.GetType()); } case JsonToken.String: // Get the value as a string string value = (string)reader.Value; // Parse the string using the format of the converter switch (Format) { case TimeFormat.Iso8601: return(Iso8601Utils.Parse(value)); case TimeFormat.Rfc822: return(Rfc822Utils.Parse(value)); case TimeFormat.Rfc2822: return(Rfc2822Utils.Parse(value)); case TimeFormat.UnixTime: return(TimeUtils.GetDateTimeOffsetFromUnixTime(value)); default: throw new JsonSerializationException("Unsupported format " + Format); } default: throw new JsonSerializationException("Unexpected token type: " + reader.TokenType); } }
public static DateTimeOffset Iso8601ToDateTimeOffset(string iso8601) { if (string.IsNullOrWhiteSpace(iso8601)) { throw new ArgumentNullException(nameof(iso8601)); } return(Iso8601Utils.Parse(iso8601)); }
public static DateTime Iso8601ToDateTime(string iso8601) { return(Iso8601Utils.Parse(iso8601).DateTime); }
public void ParseWithMilliseconds() { // Initialize some reference timestamps DateTimeOffset reference1 = new DateTime(2013, 08, 07, 21, 07, 24, 123, DateTimeKind.Utc); DateTimeOffset reference2 = new DateTime(2016, 10, 07, 17, 33, 38, 456, DateTimeKind.Utc); //var dto = Iso8601Utils.Parse("2016-10-07T16:33:38.456-01:00"); //throw new Exception(reference2.ToString("yyyy-MM-ddTHH:mm:ss.fffK") + " vs " + dto.ToString("yyyy-MM-ddTHH:mm:ss.fffK")); // Universal time Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T21:07:24.123Z"), "Reference 1 - UTC +00"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T21:07:24.123+00:00"), "Reference 1 - UTC +01"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T17:33:38.456Z"), "Reference 1 - UTC +02"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T17:33:38.456+00:00"), "Reference 1 - UTC +02"); // Offset (reference 1) Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T20:07:24.123-01:00"), "Reference 1 - UTC -01"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T19:07:24.123-02:00"), "Reference 1 - UTC -02"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T18:07:24.123-03:00"), "Reference 1 - UTC -03"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T17:07:24.123-04:00"), "Reference 1 - UTC -04"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T16:07:24.123-05:00"), "Reference 1 - UTC -05"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T15:07:24.123-06:00"), "Reference 1 - UTC -05"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T14:07:24.123-07:00"), "Reference 1 - UTC -07"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T13:07:24.123-08:00"), "Reference 1 - UTC -08"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T12:07:24.123-09:00"), "Reference 1 - UTC -09"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T11:07:24.123-10:00"), "Reference 1 - UTC -10"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T10:07:24.123-11:00"), "Reference 1 - UTC -11"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T09:07:24.123-12:00"), "Reference 1 - UTC -12"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T22:07:24.123+01:00"), "Reference 1 - UTC +01"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T23:07:24.123+02:00"), "Reference 1 - UTC +02"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T00:07:24.123+03:00"), "Reference 1 - UTC +03"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T01:07:24.123+04:00"), "Reference 1 - UTC +04"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T02:07:24.123+05:00"), "Reference 1 - UTC +05"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T03:07:24.123+06:00"), "Reference 1 - UTC +06"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T04:07:24.123+07:00"), "Reference 1 - UTC +07"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T05:07:24.123+08:00"), "Reference 1 - UTC +08"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T06:07:24.123+09:00"), "Reference 1 - UTC +09"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T07:07:24.123+10:00"), "Reference 1 - UTC +10"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T08:07:24.123+11:00"), "Reference 1 - UTC +11"); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T09:07:24.123+12:00"), "Reference 1 - UTC +12"); // Offset (reference 2) Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T16:33:38.456-01:00"), "Reference 2 - UTC -01"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T15:33:38.456-02:00"), "Reference 2 - UTC -02"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T14:33:38.456-03:00"), "Reference 2 - UTC -03"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T13:33:38.456-04:00"), "Reference 2 - UTC -04"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T12:33:38.456-05:00"), "Reference 2 - UTC -05"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T11:33:38.456-06:00"), "Reference 2 - UTC -06"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T10:33:38.456-07:00"), "Reference 2 - UTC -07"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T09:33:38.456-08:00"), "Reference 2 - UTC -08"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T08:33:38.456-09:00"), "Reference 2 - UTC -09"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T07:33:38.456-10:00"), "Reference 2 - UTC -10"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T06:33:38.456-11:00"), "Reference 2 - UTC -11"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T05:33:38.456-12:00"), "Reference 2 - UTC -12"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T18:33:38.456+01:00"), "Reference 2 - UTC +01"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T19:33:38.456+02:00"), "Reference 2 - UTC +02"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T20:33:38.456+03:00"), "Reference 2 - UTC +03"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T21:33:38.456+04:00"), "Reference 2 - UTC +04"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T22:33:38.456+05:00"), "Reference 2 - UTC +05"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T23:33:38.456+06:00"), "Reference 2 - UTC +06"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T00:33:38.456+07:00"), "Reference 2 - UTC +07"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T01:33:38.456+08:00"), "Reference 2 - UTC +08"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T02:33:38.456+09:00"), "Reference 2 - UTC +09"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T03:33:38.456+10:00"), "Reference 2 - UTC +10"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T04:33:38.456+11:00"), "Reference 2 - UTC +11"); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T05:33:38.456+12:00"), "Reference 2 - UTC +12"); }
public void Parse() { // Initialize some reference timestamps DateTimeOffset reference1 = new DateTime(2013, 08, 07, 21, 07, 24, DateTimeKind.Utc); DateTimeOffset reference2 = new DateTime(2016, 10, 07, 17, 33, 38, DateTimeKind.Utc); // Universal time Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T21:07:24Z")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T21:07:24+00:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T17:33:38Z")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T17:33:38+00:00")); // Offset (reference 1) Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T20:07:24-01:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T19:07:24-02:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T18:07:24-03:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T17:07:24-04:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T16:07:24-05:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T15:07:24-06:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T14:07:24-07:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T13:07:24-08:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T12:07:24-09:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T11:07:24-10:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T10:07:24-11:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T09:07:24-12:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T22:07:24+01:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-07T23:07:24+02:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T00:07:24+03:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T01:07:24+04:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T02:07:24+05:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T03:07:24+06:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T04:07:24+07:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T05:07:24+08:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T06:07:24+09:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T07:07:24+10:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T08:07:24+11:00")); Assert.AreEqual(reference1, Iso8601Utils.Parse("2013-08-08T09:07:24+12:00")); // Offset (reference 2) Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T16:33:38-01:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T15:33:38-02:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T14:33:38-03:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T13:33:38-04:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T12:33:38-05:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T11:33:38-06:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T10:33:38-07:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T09:33:38-08:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T08:33:38-09:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T07:33:38-10:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T06:33:38-11:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T05:33:38-12:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T18:33:38+01:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T19:33:38+02:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T20:33:38+03:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T21:33:38+04:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T22:33:38+05:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-07T23:33:38+06:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T00:33:38+07:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T01:33:38+08:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T02:33:38+09:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T03:33:38+10:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T04:33:38+11:00")); Assert.AreEqual(reference2, Iso8601Utils.Parse("2016-10-08T05:33:38+12:00")); }