Example #1
0
        public void InstantComparer()
        {
            var london     = DateTimeZoneProviders.Tzdb["Europe/London"];
            var losAngeles = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];

            // LA is 8 hours behind London. So the London evening occurs before the LA afternoon.
            var londonEvening       = new LocalDateTime(2014, 7, 9, 20, 32).InZoneStrictly(london);
            var losAngelesAfternoon = new LocalDateTime(2014, 7, 9, 14, 0).InZoneStrictly(losAngeles);

            // Same instant as londonEvening
            var losAngelesLunchtime = new LocalDateTime(2014, 7, 9, 12, 32).InZoneStrictly(losAngeles);

            var londonPersian = londonEvening.LocalDateTime
                                .WithCalendar(CalendarSystem.PersianSimple)
                                .InZoneStrictly(london);

            var comparer = ZonedDateTime.Comparer.Instant;

            TestHelper.TestComparerStruct(comparer, londonEvening, losAngelesLunchtime, losAngelesAfternoon);
            Assert.AreEqual(0, comparer.Compare(londonPersian, londonEvening));
            Assert.IsTrue(comparer.Equals(londonPersian, londonEvening));
            Assert.AreEqual(comparer.GetHashCode(londonPersian), comparer.GetHashCode(londonEvening));
            Assert.IsTrue(comparer.Equals(losAngelesLunchtime, londonEvening));
            Assert.AreEqual(comparer.GetHashCode(losAngelesLunchtime), comparer.GetHashCode(londonEvening));
            Assert.IsFalse(comparer.Equals(losAngelesAfternoon, londonEvening));
            Assert.AreNotEqual(comparer.GetHashCode(losAngelesAfternoon), comparer.GetHashCode(londonEvening));
        }
Example #2
0
        public void LocalComparer()
        {
            var london     = DateTimeZoneProviders.Tzdb["Europe/London"];
            var losAngeles = DateTimeZoneProviders.Tzdb["America/Los_Angeles"];

            // LA is 8 hours behind London. So the London evening occurs before the LA afternoon.
            var londonEvening       = new LocalDateTime(2014, 7, 9, 20, 32).InZoneStrictly(london);
            var losAngelesAfternoon = new LocalDateTime(2014, 7, 9, 14, 0).InZoneStrictly(losAngeles);

            // Same local time as losAngelesAfternoon
            var londonAfternoon = losAngelesAfternoon.LocalDateTime.InZoneStrictly(london);

            var londonPersian = londonEvening.LocalDateTime
                                .WithCalendar(CalendarSystem.PersianSimple)
                                .InZoneStrictly(london);

            var comparer = ZonedDateTime.Comparer.Local;

            TestHelper.TestComparerStruct(comparer, losAngelesAfternoon, londonAfternoon, londonEvening);
            Assert.Throws <ArgumentException>(() => comparer.Compare(londonPersian, londonEvening));
            Assert.IsFalse(comparer.Equals(londonPersian, londonEvening));
            Assert.AreNotEqual(comparer.GetHashCode(londonPersian), comparer.GetHashCode(londonEvening));
            Assert.IsFalse(comparer.Equals(londonAfternoon, londonEvening));
            Assert.AreNotEqual(comparer.GetHashCode(londonAfternoon), comparer.GetHashCode(londonEvening));
            Assert.IsTrue(comparer.Equals(londonAfternoon, losAngelesAfternoon));
            Assert.AreEqual(comparer.GetHashCode(londonAfternoon), comparer.GetHashCode(losAngelesAfternoon));
        }