Beispiel #1
0
        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);
            }
        }
Beispiel #2
0
        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 (reference 1)
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 21:07:24 GMT"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 21:07:24 UT"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 21:07:24 UTC"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 7 Aug 2013 21:07:24 GMT"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 7 Aug 2013 21:07:24 UT"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 7 Aug 2013 21:07:24 UTC"));

            // Universal time (reference 2)
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 17:33:38 GMT"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 17:33:38 UT"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 17:33:38 UTC"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 7 Oct 2016 17:33:38 GMT"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 7 Oct 2016 17:33:38 UT"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 7 Oct 2016 17:33:38 UTC"));

            // North American time zones (reference 1)
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 16:07:24 EST")); //  -0500
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 17:07:24 EDT")); //  -0400
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 15:07:24 CST")); //  -0600
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 16:07:24 CDT")); //  -0500
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 14:07:24 MST")); //  -0700
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 15:07:24 MDT")); //  -0600
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 13:07:24 PST")); //  -0800
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 14:07:24 PDT")); //  -0700

            // North American time zones (reference 2)
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 12:33:38 EST")); //  -0500
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 13:33:38 EDT")); //  -0400
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 11:33:38 CST")); //  -0600
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 12:33:38 CDT")); //  -0500
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 10:33:38 MST")); //  -0700
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 11:33:38 MDT")); //  -0600
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 09:33:38 PST")); //  -0800
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 10:33:38 PDT")); //  -0700

            // Letter time zones (reference 1)
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 20:07:24 A")); // -0100
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 19:07:24 B")); // -0200
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 18:07:24 C")); // -0300
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 17:07:24 D")); // -0400
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 16:07:24 E")); // -0500
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 15:07:24 F")); // -0600
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 14:07:24 G")); // -0700
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 13:07:24 H")); // -0800
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 12:07:24 I")); // -0900
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 11:07:24 K")); // -1000
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 10:07:24 L")); // -1100
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 09:07:24 M")); // -1200
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 22:07:24 N")); // +0100
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 23:07:24 O")); // +0200
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 00:07:24 P")); // +0300
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 01:07:24 Q")); // +0400
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 02:07:24 R")); // +0500
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 03:07:24 S")); // +0600
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 04:07:24 T")); // +0700
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 05:07:24 U")); // +0800
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 06:07:24 V")); // +0900
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 07:07:24 W")); // +1000
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 08:07:24 X")); // +1100
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 09:07:24 Y")); // +1200
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 21:07:24 Z")); // +0000

            // Letter time zones (reference 2)
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 16:33:38 A")); // -0100
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 15:33:38 B")); // -0200
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 14:33:38 C")); // -0300
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 13:33:38 D")); // -0400
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 12:33:38 E")); // -0500
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 11:33:38 F")); // -0600
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 10:33:38 G")); // -0700
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 09:33:38 H")); // -0800
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 08:33:38 I")); // -0900
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 07:33:38 K")); // -1000
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 06:33:38 L")); // -1100
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 05:33:38 M")); // -1200
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 18:33:38 N")); // +0100
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 19:33:38 O")); // +0200
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 20:33:38 P")); // +0300
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 21:33:38 Q")); // +0400
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 22:33:38 R")); // +0500
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 23:33:38 S")); // +0600
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 00:33:38 T")); // +0700
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 01:33:38 U")); // +0800
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 02:33:38 V")); // +0900
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 03:33:38 W")); // +1000
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 04:33:38 X")); // +1100
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 05:33:38 Y")); // +1200
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 17:33:38 Z")); // +0000

            // Offset (reference 1)
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 20:07:24 -0100"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 19:07:24 -0200"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 18:07:24 -0300"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 17:07:24 -0400"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 16:07:24 -0500"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 15:07:24 -0600"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 14:07:24 -0700"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 13:07:24 -0800"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 12:07:24 -0900"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 11:07:24 -1000"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 10:07:24 -1100"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 09:07:24 -1200"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 22:07:24 +0100"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Wed, 07 Aug 2013 23:07:24 +0200"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 00:07:24 +0300"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 01:07:24 +0400"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 02:07:24 +0500"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 03:07:24 +0600"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 04:07:24 +0700"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 05:07:24 +0800"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 06:07:24 +0900"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 07:07:24 +1000"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 08:07:24 +1100"));
            Assert.AreEqual(reference1, Rfc822Utils.Parse("Thu, 08 Aug 2013 09:07:24 +1200"));

            // Offset (reference 2)
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 16:33:38 -0100"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 15:33:38 -0200"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 14:33:38 -0300"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 13:33:38 -0400"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 12:33:38 -0500"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 11:33:38 -0600"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 10:33:38 -0700"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 09:33:38 -0800"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 08:33:38 -0900"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 07:33:38 -1000"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 06:33:38 -1100"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 05:33:38 -1200"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 18:33:38 +0100"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 19:33:38 +0200"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 20:33:38 +0300"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 21:33:38 +0400"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 22:33:38 +0500"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Fri, 07 Oct 2016 23:33:38 +0600"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 00:33:38 +0700"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 01:33:38 +0800"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 02:33:38 +0900"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 03:33:38 +1000"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 04:33:38 +1100"));
            Assert.AreEqual(reference2, Rfc822Utils.Parse("Sat, 08 Oct 2016 05:33:38 +1200"));
        }
Beispiel #3
0
 /// <summary>
 /// Converts the specified <paramref name="rfc2822"/> formatted date to a corresponding instance of <see cref="DateTimeOffset"/>.
 /// </summary>
 /// <param name="rfc2822">The string with the RFC 2822 formatted date.</param>
 /// <returns>An instance of <see cref="DateTimeOffset"/>.</returns>
 public static DateTimeOffset Parse(string rfc2822)
 {
     return(Rfc822Utils.Parse(rfc2822));
 }
 public static DateTime Rfc822ToDateTime(string rfc822)
 {
     return(Rfc822Utils.Parse(rfc822).DateTime);
 }