public void GetDaysBetween()
        {
            var jan1 = new EssentialsDate(2019, 1, 1);
            var feb1 = new EssentialsDate(2019, 1, 1);

            Assert.AreEqual(31, EssentialsDate.GetDaysBetween(jan1, feb1));
            Assert.AreEqual(-31, EssentialsDate.GetDaysBetween(feb1, jan1));
        }
        public void YearAndDay()
        {
            EssentialsDate a = new EssentialsDate(2019, 1, 1);
            EssentialsDate b = new EssentialsDate(2019, 10, 20);
            EssentialsDate c = new EssentialsDate(2019, 12, 31);

            Assert.AreEqual(2019001, a.YearAndDay);
            Assert.AreEqual(2019293, b.YearAndDay);
            Assert.AreEqual(2019365, c.YearAndDay);
        }
        internal static object ToFormat(EssentialsDate date, TimeFormat format)
        {
            switch (format)
            {
            case TimeFormat.Iso8601:
                return(date.Iso8601);

            default:
                throw new ArgumentException("Unsupported format " + format, nameof(format));
            }
        }
Ejemplo n.º 4
0
        public void EssentialsDate()
        {
            EssentialsTime t = new EssentialsTime(2019, 9, 14, TimeSpan.Zero);

            EssentialsDate a = new EssentialsDate(2020, 9, 13);
            EssentialsDate b = new EssentialsDate(2020, 9, 14);
            EssentialsDate c = new EssentialsDate(2020, 9, 15);

            Assert.AreEqual(0, TimeUtils.GetAge(t, a));
            Assert.AreEqual(1, TimeUtils.GetAge(t, b));
            Assert.AreEqual(1, TimeUtils.GetAge(t, c));
        }
Ejemplo n.º 5
0
        public void DateTime()
        {
            EssentialsDate t = new EssentialsDate(2019, 9, 14);

            DateTime a = new DateTime(2020, 9, 13);
            DateTime b = new DateTime(2020, 9, 14);
            DateTime c = new DateTime(2020, 9, 15);

            Assert.AreEqual(0, TimeUtils.GetAge(t, t));
            Assert.AreEqual(0, TimeUtils.GetAge(t, a));
            Assert.AreEqual(1, TimeUtils.GetAge(t, b));
            Assert.AreEqual(1, TimeUtils.GetAge(t, c));
        }
Ejemplo n.º 6
0
        public void DateTimeOffset()
        {
            EssentialsDate t = new EssentialsDate(2019, 9, 14);

            DateTimeOffset a = new DateTimeOffset(2020, 9, 13, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset b = new DateTimeOffset(2020, 9, 14, 0, 0, 0, TimeSpan.Zero);
            DateTimeOffset c = new DateTimeOffset(2020, 9, 15, 0, 0, 0, TimeSpan.Zero);

            Assert.AreEqual(0, TimeUtils.GetAge(t, t));
            Assert.AreEqual(0, TimeUtils.GetAge(t, a));
            Assert.AreEqual(1, TimeUtils.GetAge(t, b));
            Assert.AreEqual(1, TimeUtils.GetAge(t, c));
        }
Ejemplo n.º 7
0
        public void TryParse()
        {
            bool success1 = EssentialsDate.TryParse("2019-08-17", out EssentialsDate result1);
            bool success2 = EssentialsDate.TryParse("08/17/2019", out EssentialsDate result2);

            Assert.AreEqual(true, success1);
            Assert.AreEqual(false, success2);

            Assert.AreEqual(2019, result1.Year);
            Assert.AreEqual(8, result1.Month);
            Assert.AreEqual(17, result1.Day);

            Assert.IsNull(result2);
        }
Ejemplo n.º 8
0
        private EssentialsDate ParseEssentialsDate(JsonReader reader)
        {
            switch (reader.TokenType)
            {
            // Return the default value if 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(new EssentialsDate(TimeUtils.GetDateTimeFromUnixTime((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(new EssentialsDate(TimeUtils.GetDateTimeFromUnixTime((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(new EssentialsDate(dt));

                case DateTimeOffset dto:
                    return(new EssentialsDate(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(EssentialsDate.Parse(value));

                default:
                    throw new JsonSerializationException("Unsupported format " + Format);
                }

            default:
                throw new JsonSerializationException("Unexpected token type: " + reader.TokenType);
            }
        }
Ejemplo n.º 9
0
        public void EssentialsDate()
        {
#pragma warning disable 618

            EssentialsDateTime t = new EssentialsDateTime(2019, 9, 14);

            EssentialsDate a = new EssentialsDate(2020, 9, 13);
            EssentialsDate b = new EssentialsDate(2020, 9, 14);
            EssentialsDate c = new EssentialsDate(2020, 9, 15);

            Assert.AreEqual(0, TimeUtils.GetAge(t, a));
            Assert.AreEqual(1, TimeUtils.GetAge(t, b));
            Assert.AreEqual(1, TimeUtils.GetAge(t, c));

#pragma warning restore 618
        }
Ejemplo n.º 10
0
        public void TryParseExactArray()
        {
            bool success1 = EssentialsDate.TryParseExact("2019-08-17", new [] { "yyyy-MM-dd" }, null, DateTimeStyles.None, out EssentialsDate result1);
            bool success2 = EssentialsDate.TryParseExact("08/17/2019", new [] { "MM/dd/yyyy" }, null, DateTimeStyles.None, out EssentialsDate result2);

            Assert.AreEqual(true, success1);
            Assert.AreEqual(true, success2);

            Assert.AreEqual(2019, result1.Year);
            Assert.AreEqual(8, result1.Month);
            Assert.AreEqual(17, result1.Day);

            Assert.AreEqual(2019, result2.Year);
            Assert.AreEqual(8, result2.Month);
            Assert.AreEqual(17, result2.Day);
        }
Ejemplo n.º 11
0
        public void TryParseProvider()
        {
            CultureInfo culture = new CultureInfo("en-US");

            bool success1 = EssentialsDate.TryParse("2019-08-17", culture, DateTimeStyles.None, out EssentialsDate result1);
            bool success2 = EssentialsDate.TryParse("08/17/2019", culture, DateTimeStyles.None, out EssentialsDate result2);

            Assert.AreEqual(true, success1);
            Assert.AreEqual(true, success2);

            Assert.AreEqual(2019, result1.Year);
            Assert.AreEqual(8, result1.Month);
            Assert.AreEqual(17, result1.Day);

            Assert.AreEqual(2019, result2.Year);
            Assert.AreEqual(8, result2.Month);
            Assert.AreEqual(17, result2.Day);
        }
        public void Reverse()
        {
            EssentialsDate start = new EssentialsDate(2019, 10, 20);
            EssentialsDate end   = new EssentialsDate(2019, 10, 14);

            var range = new EssentialsDateRange(start, end);

            Assert.AreEqual(true, range.IsReverse, "# reverse");

            Assert.AreEqual(7, range.Days.Length, "# length");

            Assert.AreEqual("2019-10-20", range.Days[0].Iso8601, "# index 0");
            Assert.AreEqual("2019-10-19", range.Days[1].Iso8601, "# index 1");
            Assert.AreEqual("2019-10-18", range.Days[2].Iso8601, "# index 2");
            Assert.AreEqual("2019-10-17", range.Days[3].Iso8601, "# index 3");
            Assert.AreEqual("2019-10-16", range.Days[4].Iso8601, "# index 4");
            Assert.AreEqual("2019-10-15", range.Days[5].Iso8601, "# index 5");
            Assert.AreEqual("2019-10-14", range.Days[6].Iso8601, "# index 6");
        }
Ejemplo n.º 13
0
        public void CompareTo()
        {
            EssentialsDate a = new EssentialsDate(2012, 7, 30);
            EssentialsDate b = new EssentialsDate(2019, 8, 17);
            EssentialsDate c = null;

            var samples = new[] {
                new { Left = a, Right = b, Expected = 1.CompareTo(2) },
                new { Left = a, Right = a, Expected = 2.CompareTo(2) },
                new { Left = b, Right = a, Expected = 2.CompareTo(1) },
                new { Left = a, Right = c, Expected = 1 }
            };

            foreach (var sample in samples)
            {
                int result = sample.Left.CompareTo(sample.Right);

                Assert.AreEqual(sample.Expected, result);
            }
        }
        public void TryParse()
        {
            bool success1 = EssentialsDate.TryParse("2019-08-17", CultureInfo.InvariantCulture, DateTimeStyles.None, out EssentialsDate result1);
            bool success2 = EssentialsDate.TryParse("08/17/2019", CultureInfo.InvariantCulture, DateTimeStyles.None, out EssentialsDate result2);
            bool success3 = EssentialsDate.TryParse("17/08/2019", CultureInfo.InvariantCulture, DateTimeStyles.None, out EssentialsDate result3);

            Assert.AreEqual(true, success1, "#1");
            Assert.AreEqual(true, success2, "#2");
            Assert.AreEqual(false, success3, "#3");

            Assert.AreEqual(2019, result1.Year);
            Assert.AreEqual(8, result1.Month);
            Assert.AreEqual(17, result1.Day);

            Assert.AreEqual(2019, result2.Year);
            Assert.AreEqual(8, result2.Month);
            Assert.AreEqual(17, result2.Day);

            Assert.IsNull(result3);
        }
Ejemplo n.º 15
0
        public void Equals()
        {
            EssentialsDate a = new EssentialsDate(2012, 7, 30);
            EssentialsDate b = new EssentialsDate(2019, 8, 17);
            EssentialsDate c = null;

            var samples = new[] {
                new { Left = a, Right = a, Expected = true },
                new { Left = a, Right = b, Expected = false },
                new { Left = a, Right = c, Expected = false },
                new { Left = a, Right = c, Expected = false }
            };

            foreach (var sample in samples)
            {
                bool result = sample.Left.Equals(sample.Right);

                Assert.AreEqual(sample.Expected, result);
            }
        }
Ejemplo n.º 16
0
        public void OperatorGreaterThanOrEqualTo()
        {
            EssentialsDate a = new EssentialsDate(2012, 7, 30);
            EssentialsDate b = new EssentialsDate(2019, 8, 17);
            EssentialsDate c = null;

            var samples = new[] {
                new { Left = a, Right = a, Expected = true },
                new { Left = a, Right = b, Expected = false },
                new { Left = b, Right = a, Expected = true },
                new { Left = a, Right = c, Expected = true },
                new { Left = c, Right = a, Expected = false },
                new { Left = c, Right = c, Expected = true }
            };

            int i = 1;

            foreach (var sample in samples)
            {
                bool result = sample.Left >= sample.Right;
                Assert.AreEqual(sample.Expected, result, "#" + i++);
            }
        }
        public void GetEnumerator()
        {
            TimeZoneInfo romance = TimeZoneInfo.FindSystemTimeZoneById("Romance Standard Time");

            var date = new EssentialsDate(2019, 10, 24);

            EssentialsWeek week = new EssentialsWeek(date, romance);

            Assert.AreEqual("2019-10-21T00:00:00.000+02:00", week.Start.ToString(), "Start");
            Assert.AreEqual("2019-10-27T23:59:59.999+01:00", week.End.ToString(), "End");

            EssentialsDate[] days = week.ToArray();

            Assert.AreEqual(7, days.Length);

            Assert.AreEqual(DayOfWeek.Monday, days[0].DayOfWeek);
            Assert.AreEqual("2019-10-21", days[0].ToString());

            Assert.AreEqual(DayOfWeek.Tuesday, days[1].DayOfWeek);
            Assert.AreEqual("2019-10-22", days[1].ToString());

            Assert.AreEqual(DayOfWeek.Wednesday, days[2].DayOfWeek);
            Assert.AreEqual("2019-10-23", days[2].ToString());

            Assert.AreEqual(DayOfWeek.Thursday, days[3].DayOfWeek);
            Assert.AreEqual("2019-10-24", days[3].ToString());

            Assert.AreEqual(DayOfWeek.Friday, days[4].DayOfWeek);
            Assert.AreEqual("2019-10-25", days[4].ToString());

            Assert.AreEqual(DayOfWeek.Saturday, days[5].DayOfWeek);
            Assert.AreEqual("2019-10-26", days[5].ToString());

            Assert.AreEqual(DayOfWeek.Sunday, days[6].DayOfWeek);
            Assert.AreEqual("2019-10-27", days[6].ToString());
        }