Exemple #1
0
 /// <summary>
 /// Initializes a new instance from the specified <paramref name="timestamp"/> and <paramref name="timeZone"/>.
 /// </summary>
 /// <param name="timestamp">The timestamp.</param>
 /// <param name="timeZone">The time zone.</param>
 public EssentialsWeek(DateTimeOffset timestamp, TimeZoneInfo timeZone)
 {
     WeekNumber = Iso8601Utils.GetWeekNumber(timestamp);
     Start      = TimeUtils.GetStartOfWeek(timestamp, timeZone);
     End        = TimeUtils.GetEndOfWeek(timestamp, timeZone);
     Year       = GetYear();
 }
        public void TryParseFormat()
        {
            using (new CultureDisposable(DanishCulture)) {
                bool   success1 = Iso8601Utils.TryParse("2022-03-07T16:50:23Z", out DateTimeOffset result1);
                string actual1  = result1.ToString(Iso8601Constants.DateTimeMilliseconds);

                bool   success2 = Iso8601Utils.TryParse("2022-03-07T16:50:23.123Z", out DateTimeOffset result2);
                string actual2  = result2.ToString(Iso8601Constants.DateTimeMilliseconds);

                bool   success3 = Iso8601Utils.TryParse("2022-03-07T17:50:23+01:00", out DateTimeOffset result3);
                string actual3  = result3.ToString(Iso8601Constants.DateTimeMilliseconds);

                bool   success4 = Iso8601Utils.TryParse("2022-03-07T17:50:23.123+01:00", out DateTimeOffset result4);
                string actual4  = result4.ToString(Iso8601Constants.DateTimeMilliseconds);

                bool success5 = Iso8601Utils.TryParse("Monday, 07 March 2022 17:50:23", out DateTimeOffset _);

                Assert.IsTrue(success1, "#1 Successful");
                Assert.AreEqual("2022-03-07T16:50:23.000+00:00", actual1, "#1 AreEqual");

                Assert.IsTrue(success2, "#2 Successful");
                Assert.AreEqual("2022-03-07T16:50:23.123+00:00", actual2, "#2 AreEqual");

                Assert.IsTrue(success3, "#3 Successful");
                Assert.AreEqual("2022-03-07T17:50:23.000+01:00", actual3, "#3 AreEqual");

                Assert.IsTrue(success4, "#4 Successful");
                Assert.AreEqual("2022-03-07T17:50:23.123+01:00", actual4, "#4 AreEqual");

                Assert.IsFalse(success5, "#5 Successful");
            }
        }
Exemple #3
0
 /// <summary>
 /// Initializes a new instance based on the specified <paramref name="year"/>, <paramref name="week"/> and <paramref name="offset"/>.
 /// </summary>
 /// <param name="year">The year.</param>
 /// <param name="week">The week number.</param>
 /// <param name="offset">The offset.</param>
 public EssentialsWeek(int year, int week, TimeSpan offset)
 {
     WeekNumber = week;
     Year       = year;
     Start      = Iso8601Utils.FromWeekNumber(year, week, offset);
     End        = Start.GetEndOfWeek();
 }
Exemple #4
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);
            }
        }
Exemple #5
0
 public static DateTimeOffset Iso8601ToDateTimeOffset(string iso8601)
 {
     if (string.IsNullOrWhiteSpace(iso8601))
     {
         throw new ArgumentNullException(nameof(iso8601));
     }
     return(Iso8601Utils.Parse(iso8601));
 }
Exemple #6
0
 public static string ToIso8601(EssentialsDateTime timestamp)
 {
     if (timestamp == null)
     {
         throw new ArgumentNullException(nameof(timestamp));
     }
     return(Iso8601Utils.ToString(timestamp.DateTime));
 }
        /// <summary>
        /// Initialize a new instance based on the specified <paramref name="timestamp"/>.
        /// </summary>
        /// <param name="timestamp">A timestamp.</param>
        public EssentialsDateWeek(EssentialsDateTime timestamp)
        {
            Week  = Iso8601Utils.GetWeekNumber(timestamp.DateTime);
            Start = TimeUtils.GetFirstDayOfWeek(timestamp.DateTime);
            End   = TimeUtils.GetLastDayOfWeek(timestamp.DateTime);

            if (End.Month == 1 && Week == 1)
            {
                Year = End.Year;
            }
            else if (Start.Month == 12 && Week >= 50)
            {
                Year = Start.Year;
            }
            else
            {
                Year = timestamp.Year;
            }
        }
        internal static object ToFormat(DateTimeOffset value, TimeFormat format)
        {
            switch (format)
            {
            case TimeFormat.Iso8601:
                return(Iso8601Utils.ToString(value));

            case TimeFormat.Rfc822:
                return(Rfc822Utils.ToString(value));

            case TimeFormat.Rfc2822:
                return(Rfc2822Utils.ToString(value));

            case TimeFormat.UnixTime:
                return((long)UnixTimeUtils.ToSeconds(value));

            default:
                throw new ArgumentException("Unsupported format " + format, nameof(format));
            }
        }
Exemple #9
0
        public void FromWeekNumberUtc()
        {
            TimeZoneInfo utc = TimeZoneInfo.FindSystemTimeZoneById("UTC");

            DateTimeOffset week1 = Iso8601Utils.FromWeekNumber(2021, 1, utc);

            DateTimeOffset week12 = Iso8601Utils.FromWeekNumber(2021, 12, utc);
            DateTimeOffset week13 = Iso8601Utils.FromWeekNumber(2021, 13, utc);

            DateTimeOffset week43 = Iso8601Utils.FromWeekNumber(2021, 43, utc);
            DateTimeOffset week44 = Iso8601Utils.FromWeekNumber(2021, 44, utc);

            DateTimeOffset week52 = Iso8601Utils.FromWeekNumber(2021, 52, utc);

            Assert.AreEqual("2021-01-04T00:00:00+00:00", week1.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 1");
            Assert.AreEqual("2021-03-22T00:00:00+00:00", week12.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 12");
            Assert.AreEqual("2021-03-29T00:00:00+00:00", week13.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 13");
            Assert.AreEqual("2021-10-25T00:00:00+00:00", week43.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 43");
            Assert.AreEqual("2021-11-01T00:00:00+00:00", week44.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 44");
            Assert.AreEqual("2021-12-27T00:00:00+00:00", week52.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 52");
        }
Exemple #10
0
        public void FromWeekNumberRomance()
        {
            TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");

            DateTimeOffset week1 = Iso8601Utils.FromWeekNumber(2021, 1, romance);

            DateTimeOffset week12 = Iso8601Utils.FromWeekNumber(2021, 12, romance);
            DateTimeOffset week13 = Iso8601Utils.FromWeekNumber(2021, 13, romance);

            DateTimeOffset week43 = Iso8601Utils.FromWeekNumber(2021, 43, romance);
            DateTimeOffset week44 = Iso8601Utils.FromWeekNumber(2021, 44, romance);

            DateTimeOffset week52 = Iso8601Utils.FromWeekNumber(2021, 52, romance);

            Assert.AreEqual("2021-01-04T00:00:00+01:00", week1.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 1");
            Assert.AreEqual("2021-03-22T00:00:00+01:00", week12.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 12");
            Assert.AreEqual("2021-03-29T00:00:00+02:00", week13.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 13");
            Assert.AreEqual("2021-10-25T00:00:00+02:00", week43.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 43");
            Assert.AreEqual("2021-11-01T00:00:00+01:00", week44.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 44");
            Assert.AreEqual("2021-12-27T00:00:00+01:00", week52.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 52");
        }
Exemple #11
0
        public void FromWeekNumberOffset()
        {
            TimeSpan gmt1 = TimeSpan.FromHours(1);
            TimeSpan gmt2 = TimeSpan.FromHours(2);

            DateTimeOffset week1 = Iso8601Utils.FromWeekNumber(2021, 1, gmt1);

            DateTimeOffset week12 = Iso8601Utils.FromWeekNumber(2021, 12, gmt1);
            DateTimeOffset week13 = Iso8601Utils.FromWeekNumber(2021, 13, gmt2);

            DateTimeOffset week43 = Iso8601Utils.FromWeekNumber(2021, 43, gmt2);
            DateTimeOffset week44 = Iso8601Utils.FromWeekNumber(2021, 44, gmt1);

            DateTimeOffset week52 = Iso8601Utils.FromWeekNumber(2021, 52, gmt1);

            Assert.AreEqual("2021-01-04T00:00:00+01:00", week1.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 1");
            Assert.AreEqual("2021-03-22T00:00:00+01:00", week12.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 12");
            Assert.AreEqual("2021-03-29T00:00:00+02:00", week13.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 13");
            Assert.AreEqual("2021-10-25T00:00:00+02:00", week43.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 43");
            Assert.AreEqual("2021-11-01T00:00:00+01:00", week44.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 44");
            Assert.AreEqual("2021-12-27T00:00:00+01:00", week52.ToString(DateTimeSeconds, CultureInfo.InvariantCulture), "Week 52");
        }
 /// <summary>
 /// Initialize a new instance based on the specified <strong>ISO 8601</strong> <paramref name="year"/> and
 /// <paramref name="week"/>.
 /// </summary>
 /// <param name="year">The <strong>ISO 8601</strong> year of the week.</param>
 /// <param name="week">The <strong>ISO 8601</strong> week number.</param>
 public EssentialsDateWeek(int year, int week) : this(Iso8601Utils.FromWeekNumber(year, week).DateTime)
 {
 }
Exemple #13
0
 public static DateTimeOffset GetDateTimeOffsetFromIso8601Week(int year, int week)
 {
     return(Iso8601Utils.FromWeekNumber(year, week));
 }
Exemple #14
0
 public static DateTime GetDateTimeFromIso8601Week(int year, int week)
 {
     return(Iso8601Utils.FromWeekNumber(year, week).DateTime);
 }
Exemple #15
0
 public static DateTime Iso8601ToDateTime(string iso8601)
 {
     return(Iso8601Utils.Parse(iso8601).DateTime);
 }
Exemple #16
0
        public new void ToString()
        {
            // 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("2013-08-07T21:07:24.000Z", Iso8601Utils.ToString(reference1));
            Assert.AreEqual("2016-10-07T17:33:38.000Z", Iso8601Utils.ToString(reference2));

            // Declare the timezones
            TimeZoneInfo utcPlus01  = TimeZoneInfo.CreateCustomTimeZone("UTC +01", TimeSpan.FromHours(1), "UTC +01", "UTC +01");
            TimeZoneInfo utcPlus02  = TimeZoneInfo.CreateCustomTimeZone("UTC +02", TimeSpan.FromHours(2), "UTC +02", "UTC +02");
            TimeZoneInfo utcPlus03  = TimeZoneInfo.CreateCustomTimeZone("UTC +03", TimeSpan.FromHours(3), "UTC +03", "UTC +03");
            TimeZoneInfo utcPlus04  = TimeZoneInfo.CreateCustomTimeZone("UTC +04", TimeSpan.FromHours(4), "UTC +04", "UTC +04");
            TimeZoneInfo utcPlus05  = TimeZoneInfo.CreateCustomTimeZone("UTC +05", TimeSpan.FromHours(5), "UTC +05", "UTC +05");
            TimeZoneInfo utcPlus06  = TimeZoneInfo.CreateCustomTimeZone("UTC +06", TimeSpan.FromHours(6), "UTC +06", "UTC +06");
            TimeZoneInfo utcPlus07  = TimeZoneInfo.CreateCustomTimeZone("UTC +07", TimeSpan.FromHours(7), "UTC +07", "UTC +07");
            TimeZoneInfo utcPlus08  = TimeZoneInfo.CreateCustomTimeZone("UTC +08", TimeSpan.FromHours(8), "UTC +08", "UTC +08");
            TimeZoneInfo utcPlus09  = TimeZoneInfo.CreateCustomTimeZone("UTC +09", TimeSpan.FromHours(9), "UTC +09", "UTC +09");
            TimeZoneInfo utcPlus10  = TimeZoneInfo.CreateCustomTimeZone("UTC +10", TimeSpan.FromHours(10), "UTC +10", "UTC +10");
            TimeZoneInfo utcPlus11  = TimeZoneInfo.CreateCustomTimeZone("UTC +11", TimeSpan.FromHours(11), "UTC +11", "UTC +11");
            TimeZoneInfo utcPlus12  = TimeZoneInfo.CreateCustomTimeZone("UTC +12", TimeSpan.FromHours(12), "UTC +12", "UTC +12");
            TimeZoneInfo utcMinus01 = TimeZoneInfo.CreateCustomTimeZone("UTC -01", TimeSpan.FromHours(-1), "UTC -01", "UTC -01");
            TimeZoneInfo utcMinus02 = TimeZoneInfo.CreateCustomTimeZone("UTC -02", TimeSpan.FromHours(-2), "UTC -02", "UTC -02");
            TimeZoneInfo utcMinus03 = TimeZoneInfo.CreateCustomTimeZone("UTC -03", TimeSpan.FromHours(-3), "UTC -03", "UTC -03");
            TimeZoneInfo utcMinus04 = TimeZoneInfo.CreateCustomTimeZone("UTC -04", TimeSpan.FromHours(-4), "UTC -04", "UTC -04");
            TimeZoneInfo utcMinus05 = TimeZoneInfo.CreateCustomTimeZone("UTC -05", TimeSpan.FromHours(-5), "UTC -05", "UTC -05");
            TimeZoneInfo utcMinus06 = TimeZoneInfo.CreateCustomTimeZone("UTC -06", TimeSpan.FromHours(-6), "UTC -06", "UTC -06");
            TimeZoneInfo utcMinus07 = TimeZoneInfo.CreateCustomTimeZone("UTC -07", TimeSpan.FromHours(-7), "UTC -07", "UTC -07");
            TimeZoneInfo utcMinus08 = TimeZoneInfo.CreateCustomTimeZone("UTC -08", TimeSpan.FromHours(-8), "UTC -08", "UTC -08");
            TimeZoneInfo utcMinus09 = TimeZoneInfo.CreateCustomTimeZone("UTC -09", TimeSpan.FromHours(-9), "UTC -09", "UTC -09");
            TimeZoneInfo utcMinus10 = TimeZoneInfo.CreateCustomTimeZone("UTC -10", TimeSpan.FromHours(-10), "UTC -10", "UTC -10");
            TimeZoneInfo utcMinus11 = TimeZoneInfo.CreateCustomTimeZone("UTC -11", TimeSpan.FromHours(-11), "UTC -11", "UTC -11");
            TimeZoneInfo utcMinus12 = TimeZoneInfo.CreateCustomTimeZone("UTC -12", TimeSpan.FromHours(-12), "UTC -12", "UTC -12");

            // Offset
            Assert.AreEqual("2013-08-07T22:07:24.000+01:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus01)));
            Assert.AreEqual("2013-08-07T23:07:24.000+02:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus02)));
            Assert.AreEqual("2013-08-08T00:07:24.000+03:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus03)));
            Assert.AreEqual("2013-08-08T01:07:24.000+04:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus04)));
            Assert.AreEqual("2013-08-08T02:07:24.000+05:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus05)));
            Assert.AreEqual("2013-08-08T03:07:24.000+06:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus06)));
            Assert.AreEqual("2013-08-08T04:07:24.000+07:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus07)));
            Assert.AreEqual("2013-08-08T05:07:24.000+08:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus08)));
            Assert.AreEqual("2013-08-08T06:07:24.000+09:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus09)));
            Assert.AreEqual("2013-08-08T07:07:24.000+10:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus10)));
            Assert.AreEqual("2013-08-08T08:07:24.000+11:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus11)));
            Assert.AreEqual("2013-08-08T09:07:24.000+12:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcPlus12)));
            Assert.AreEqual("2013-08-07T20:07:24.000-01:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus01)));
            Assert.AreEqual("2013-08-07T19:07:24.000-02:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus02)));
            Assert.AreEqual("2013-08-07T18:07:24.000-03:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus03)));
            Assert.AreEqual("2013-08-07T17:07:24.000-04:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus04)));
            Assert.AreEqual("2013-08-07T16:07:24.000-05:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus05)));
            Assert.AreEqual("2013-08-07T15:07:24.000-06:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus06)));
            Assert.AreEqual("2013-08-07T14:07:24.000-07:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus07)));
            Assert.AreEqual("2013-08-07T13:07:24.000-08:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus08)));
            Assert.AreEqual("2013-08-07T12:07:24.000-09:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus09)));
            Assert.AreEqual("2013-08-07T11:07:24.000-10:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus10)));
            Assert.AreEqual("2013-08-07T10:07:24.000-11:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus11)));
            Assert.AreEqual("2013-08-07T09:07:24.000-12:00", Iso8601Utils.ToString(TimeZoneInfo.ConvertTime(reference1, utcMinus12)));
        }
Exemple #17
0
        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");
        }
Exemple #18
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
            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"));
        }
 /// <summary>
 /// Gets the week number of the specified <paramref name="date"/> according to the <strong>ISO 8601</strong>
 /// specification.
 /// </summary>
 /// <param name="date">An instance of <see cref="DateTimeOffset"/> representing the date.</param>
 /// <returns>An instance of <see cref="int"/> representing the <strong>ISO 8601</strong> week number.</returns>
 public static int GetIso8601WeekNumber(this DateTimeOffset date)
 {
     return(Iso8601Utils.GetWeekNumber(date));
 }
Exemple #20
0
 public static string ToIso8601(DateTimeOffset timestamp)
 {
     return(Iso8601Utils.ToString(timestamp));
 }