Пример #1
0
        public void LocalComparer()
        {
            var localControl      = new LocalDateTime(2013, 4, 2, 19, 54);
            var control           = new OffsetDateTime(localControl, Offset.Zero);
            var negativeOffset    = control.LocalDateTime.WithOffset(Offset.FromHours(-1));
            var positiveOffset    = control.LocalDateTime.WithOffset(Offset.FromHours(1));
            var differentCalendar = control.LocalDateTime.WithCalendar(CalendarSystem.Coptic).WithOffset(Offset.FromHours(5));
            // Later instant, earlier local
            var earlierLocal = control.LocalDateTime.PlusHours(-2).WithOffset(Offset.FromHours(-10));
            // Same offset, previous day
            var muchEarlierLocal = control.PlusHours(-24);
            // Earlier instant, later local
            var laterLocal = control.LocalDateTime.PlusHours(2).WithOffset(Offset.FromHours(10));
            // Same offset, next day
            var muchLaterLocal = control.PlusHours(24);

            var comparer = OffsetDateTime.Comparer.Local;

            Assert.AreEqual(0, comparer.Compare(control, negativeOffset));
            Assert.AreEqual(0, comparer.Compare(control, positiveOffset));
            Assert.Throws <ArgumentException>(() => comparer.Compare(control, differentCalendar));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(control, earlierLocal)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(control, muchEarlierLocal)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(earlierLocal, control)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(muchEarlierLocal, control)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, laterLocal)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, muchLaterLocal)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(laterLocal, control)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(muchLaterLocal, control)));

            Assert.IsFalse(comparer.Equals(control, differentCalendar));
            Assert.IsFalse(comparer.Equals(control, earlierLocal));
            Assert.IsFalse(comparer.Equals(control, muchEarlierLocal));
            Assert.IsFalse(comparer.Equals(control, laterLocal));
            Assert.IsFalse(comparer.Equals(control, muchLaterLocal));
            Assert.IsTrue(comparer.Equals(control, control));

            Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(negativeOffset));
            Assert.AreEqual(comparer.GetHashCode(control), comparer.GetHashCode(positiveOffset));
            Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(earlierLocal));
            Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(muchEarlierLocal));
            Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(laterLocal));
            Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(muchLaterLocal));
            Assert.AreNotEqual(comparer.GetHashCode(control), comparer.GetHashCode(differentCalendar));
        }