Esempio n. 1
0
        public void OffsetProperty()
        {
            Offset offset = Offset.FromHours(5);

            OffsetDateTime odt = new OffsetDateTime(new LocalDateTime(2012, 1, 2, 3, 4), offset);
            Assert.AreEqual(offset, odt.Offset);
        }
        private OffsetDateTime CreateUpdatedOffsetDateTimeFromTokens(OffsetDateTime existingOffsetDateTime, DateToken dateToken, TimeToken timeToken)
        {
            var existingLocalDateTime = existingOffsetDateTime.LocalDateTime;
            var day = existingLocalDateTime.Day;
            var month = existingLocalDateTime.Month;
            var year = existingLocalDateTime.Year;
            if (dateToken != null)
            {
                if (dateToken.Day.HasValue) day = dateToken.Day.Value;
                if (dateToken.Month.HasValue) month = dateToken.Month.Value;
                if (dateToken.Year.HasValue) year = dateToken.Year.Value;
            }
            var hour = 12;
            var minute = 0;
            if (timeToken != null)
            {
                hour = timeToken.Hour;
                if (timeToken.Meridiem == Meridiem.AM && hour == 12) hour = 0;
                if (timeToken.Meridiem == Meridiem.PM && hour < 12) hour += 12;
                minute = timeToken.Minute ?? 0;
            }

            try
            {
                return new LocalDateTime(year, month, day, hour, minute).WithOffset(existingOffsetDateTime.Offset);
            }
            catch (ArgumentOutOfRangeException)
            {
                throw new InvalidTokenValueException(ErrorMessages.InvalidDateTime);
            }
        }
Esempio n. 3
0
        public void LocalDateTimeProperty()
        {
            LocalDateTime local = new LocalDateTime(2012, 6, 19, 1, 2, 3, 4, 5, CalendarSystem.Julian);
            Offset offset = Offset.FromHours(5);

            OffsetDateTime odt = new OffsetDateTime(local, offset);
            Assert.AreEqual(local, odt.LocalDateTime);
        }
Esempio n. 4
0
        public void ToInstant()
        {
            Instant instant = Instant.FromUtc(2012, 6, 25, 16, 5, 20);
            LocalDateTime local = new LocalDateTime(2012, 6, 25, 21, 35, 20);
            Offset offset = Offset.FromHoursAndMinutes(5, 30);

            OffsetDateTime odt = new OffsetDateTime(local, offset);
            Assert.AreEqual(instant, odt.ToInstant());
        }
Esempio n. 5
0
 public void WithOffset()
 {
     // Jon talks about Noda Time at Leetspeak in Sweden on October 12th 2013, at 13:15 UTC+2
     Instant instant = Instant.FromUtc(2013, 10, 12, 11, 15);
     Offset offset = Offset.FromHours(2);
     OffsetDateTime actual = instant.WithOffset(offset);
     OffsetDateTime expected = new OffsetDateTime(new LocalDateTime(2013, 10, 12, 13, 15), offset);
     Assert.AreEqual(expected, actual);
 }
        public void localdatetime_to_offset() {
            var localDateTime = new LocalDateTime(2016, 1, 21, 20, 6).WithOffset(Offset.FromHours(-5));
            //var tz = DateTimeZoneProviders.Tzdb["America/New_York"];
            var sourceOffsetDateTime = new OffsetDateTime(new LocalDateTime(2016, 1, 21, 20, 6), Offset.FromHours(-5));
            var targetOffset = Offset.FromHours(-10);
            var targetDateTime = sourceOffsetDateTime.WithOffset(targetOffset);

            Assert.Equal("2016-01-21T20:06:00-05", sourceOffsetDateTime.ToString());
            Assert.Equal("2016-01-21T15:06:00-10", targetDateTime.ToString());
        }
Esempio n. 7
0
 public void WithOffset_NonIsoCalendar()
 {
     // October 12th 2013 ISO is 1434-12-07 Islamic
     CalendarSystem calendar = CalendarSystem.GetIslamicCalendar(IslamicLeapYearPattern.Base15, IslamicEpoch.Civil);
     Instant instant = Instant.FromUtc(2013, 10, 12, 11, 15);
     Offset offset = Offset.FromHours(2);
     OffsetDateTime actual = instant.WithOffset(offset, calendar);
     OffsetDateTime expected = new OffsetDateTime(new LocalDateTime(1434, 12, 7, 13, 15, calendar), offset);
     Assert.AreEqual(expected, actual);
 }
        public void FindCandidateZones()
        {
            // Unfortunately we can't (yet - November 1st 2012) parse an OffsetDateTime. One day...
            OffsetDateTime odt = new OffsetDateTime(new LocalDateTime(2012, 10, 27, 23, 47, 57), Offset.FromHours(-7));
            var targetInstant = odt.ToInstant();
            var targetOffset = odt.Offset;

            var zoneProvider = DateTimeZoneProviders.Tzdb;
            foreach (var id in zoneProvider.Ids)
            {
                var zone = zoneProvider[id];
                if (zone.GetUtcOffset(targetInstant) == targetOffset)
                {
                    Console.WriteLine(id);
                }
            }
        }
Esempio n. 9
0
        public void LocalDateTimeProperties()
        {
            LocalDateTime local = new LocalDateTime(2012, 6, 19, 1, 2, 3, 4, 5, CalendarSystem.Julian);
            Offset offset = Offset.FromHours(5);

            OffsetDateTime odt = new OffsetDateTime(local, offset);

            var localDateTimePropertyNames = typeof(LocalDateTime).GetProperties()
                                                                  .Select(p => p.Name)
                                                                  .ToList();
            var commonProperties = typeof(OffsetDateTime).GetProperties()
                                                         .Where(p => localDateTimePropertyNames.Contains(p.Name));
            foreach (var property in commonProperties)
            {
                Assert.AreEqual(typeof(LocalDateTime).GetProperty(property.Name).GetValue(local, null),
                                property.GetValue(odt, null));
            }
        }
Esempio n. 10
0
        public void Equality()
        {
            LocalDateTime local1 = new LocalDateTime(2012, 10, 6, 1, 2, 3);
            LocalDateTime local2 = new LocalDateTime(2012, 9, 5, 1, 2, 3);
            Offset offset1 = Offset.FromHours(1);
            Offset offset2 = Offset.FromHours(2);

            OffsetDateTime equal1 = new OffsetDateTime(local1, offset1);
            OffsetDateTime equal2 = new OffsetDateTime(local1, offset1);
            OffsetDateTime unequalByOffset = new OffsetDateTime(local1, offset2);
            OffsetDateTime unequalByLocal = new OffsetDateTime(local2, offset1);

            TestHelper.TestEqualsStruct(equal1, equal2, unequalByOffset);
            TestHelper.TestEqualsStruct(equal1, equal2, unequalByLocal);

            TestHelper.TestOperatorEquality(equal1, equal2, unequalByOffset);
            TestHelper.TestOperatorEquality(equal1, equal2, unequalByLocal);
        }
Esempio n. 11
0
 public static OffsetDateTime AsOffsetDateTime(DateTimeOffset value)
 {
     return(OffsetDateTime.FromDateTimeOffset(value));
 }
Esempio n. 12
0
        public void XmlSerialization_ZeroOffset()
        {
            var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.Zero);

            TestHelper.AssertXmlRoundtrip(value, "<value>2013-04-12T17:53:23Z</value>");
        }
Esempio n. 13
0
 /// <inheritdoc />
 public override int GetHashCode(OffsetDateTime obj) => obj.ToElapsedTimeSinceEpoch().GetHashCode();
Esempio n. 14
0
 /// <inheritdoc />
 public override int Compare(OffsetDateTime x, OffsetDateTime y) =>
 // TODO(optimization): Optimize cases which are more than 2 days apart, by avoiding the arithmetic?
 x.ToElapsedTimeSinceEpoch().CompareTo(y.ToElapsedTimeSinceEpoch());
Esempio n. 15
0
        private static string CurrentTimeZoneOffsetString()
        {
            ZoneOffset offset = OffsetDateTime.now().Offset;

            return(offset.Equals(UTC) ? "+0000" : offset.ToString().Replace(":", ""));
        }
Esempio n. 16
0
//JAVA TO C# CONVERTER WARNING: 'final' parameters are ignored unless the option to convert to C# 7.2 'in' parameters is selected:
//ORIGINAL LINE: static String formatTime(final long startTime, java.time.ZoneId zoneId)
        internal static string FormatTime(long startTime, ZoneId zoneId)
        {
            return(OffsetDateTime.ofInstant(Instant.ofEpochMilli(startTime), zoneId).format(ISO_OFFSET_DATE_TIME));
        }
 public IEnumerable <Event> Get(OffsetDateTime after)
 {
     return(events.Where(@event => after == null || @event.EventStartTime.ToInstant() > after.ToInstant()));
 }
Esempio n. 18
0
 public void ToString_WithFormat()
 {
     LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
     Offset offset = Offset.FromHours(1);
     OffsetDateTime odt = new OffsetDateTime(local, offset);
     Assert.AreEqual("2012/10/06 01:02:03 01", odt.ToString("yyyy/MM/dd HH:mm:ss o<-HH>", CultureInfo.InvariantCulture));
 }
Esempio n. 19
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));
            // Earlier instant, later local
            var laterLocal = control.LocalDateTime.PlusHours(2).WithOffset(Offset.FromHours(10));

            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(earlierLocal, control)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, laterLocal)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(laterLocal, control)));

            Assert.IsFalse(comparer.Equals(control, differentCalendar));
            Assert.IsFalse(comparer.Equals(control, earlierLocal));
            Assert.IsTrue(comparer.Equals(control, control));
        }
Esempio n. 20
0
 public void WithOffset()
 {
     LocalDateTime morning = new LocalDateTime(2014, 1, 31, 9, 30);
     OffsetDateTime original = new OffsetDateTime(morning, Offset.FromHours(-8));
     LocalDateTime evening = new LocalDateTime(2014, 1, 31, 19, 30);
     Offset newOffset = Offset.FromHours(2);
     OffsetDateTime expected = new OffsetDateTime(evening, newOffset);
     Assert.AreEqual(expected, original.WithOffset(newOffset));
 }
Esempio n. 21
0
 public void XmlSerialization_NonIso()
 {
     var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23, CalendarSystem.Julian),
         Offset.FromHours(1));
     TestHelper.AssertXmlRoundtrip(value, "<value calendar=\"Julian\">2013-04-12T17:53:23+01:00</value>");
 }
Esempio n. 22
0
 public void XmlSerialization_ZeroOffset()
 {
     var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.Zero);
     TestHelper.AssertXmlRoundtrip(value, "<value>2013-04-12T17:53:23Z</value>");
 }
Esempio n. 23
0
 public void XmlSerialization_Iso()
 {
     var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.FromHours(1));
     TestHelper.AssertXmlRoundtrip(value, "<value>2013-04-12T17:53:23+01:00</value>");
 }
 public static OffsetDateTime PlusDays(this OffsetDateTime offsetDateTime, double days)
 {
     return(offsetDateTime.Plus(Duration.FromDays(days)));
 }
 public static int Microsecond(this OffsetDateTime offsetDateTime)
 {
     throw new NotImplementedException($"This method is available only for consuming via LINQ for EntityFramework translation to SQL.");
 }
Esempio n. 26
0
 public void ToString_Utc()
 {
     LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
     OffsetDateTime odt = new OffsetDateTime(local, Offset.Zero);
     Assert.AreEqual("2012-10-06T01:02:03Z", odt.ToString());
 }
Esempio n. 27
0
        public void FromDateTimeOffset()
        {
            LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
            Offset offset = Offset.FromHours(1);
            OffsetDateTime expected = new OffsetDateTime(local, offset);

            // We can build an OffsetDateTime regardless of kind... although if the kind is Local, the offset
            // has to be valid for the local time zone when building a DateTimeOffset, and if the kind is Utc, the offset has to be zero.
            DateTimeOffset bcl = new DateTimeOffset(DateTime.SpecifyKind(new DateTime(2012, 10, 6, 1, 2, 3), DateTimeKind.Unspecified),
                TimeSpan.FromHours(1));
            OffsetDateTime actual = OffsetDateTime.FromDateTimeOffset(bcl);
            Assert.AreEqual(expected, actual);
        }
Esempio n. 28
0
        public void ToDateTimeOffset()
        {
            LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
            Offset offset = Offset.FromHours(1);
            OffsetDateTime odt = new OffsetDateTime(local, offset);

            DateTimeOffset expected = new DateTimeOffset(DateTime.SpecifyKind(new DateTime(2012, 10, 6, 1, 2, 3), DateTimeKind.Unspecified),
                TimeSpan.FromHours(1));
            DateTimeOffset actual = odt.ToDateTimeOffset();
            Assert.AreEqual(expected, actual);
        }
 public virtual void SupportsMinusOffsetDateTime(OffsetDateTime offsetDateTime)
 {
     SupportsPropertyOrMethod(x => x.Minus(offsetDateTime));
 }
Esempio n. 30
0
 protected override OffsetTime Unwrap(DateTimeOffset value) => OffsetDateTime.FromDateTimeOffset(value).ToOffsetTime();
Esempio n. 31
0
 public void DefaultConstructor()
 {
     var actual = new OffsetDateTime();
     Assert.AreEqual(new LocalDateTime(1, 1, 1, 0, 0), actual.LocalDateTime);
     Assert.AreEqual(Offset.Zero, actual.Offset);
 }
Esempio n. 32
0
 public void ToString_PartHourOffset()
 {
     LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
     Offset offset = Offset.FromHoursAndMinutes(1, 30);
     OffsetDateTime odt = new OffsetDateTime(local, offset);
     Assert.AreEqual("2012-10-06T01:02:03+01:30", odt.ToString());
 }
Esempio n. 33
0
 /// <inheritdoc />
 public override bool Equals(OffsetDateTime x, OffsetDateTime y) =>
 x.ToElapsedTimeSinceEpoch() == y.ToElapsedTimeSinceEpoch();
        public object Retrieve(KeyValuePair <string, string> keyValuePair, Type targetType, Type propertyType)
        {
            var t = Nullable.GetUnderlyingType(propertyType);

            if (t != null)
            {
                if (string.IsNullOrWhiteSpace(keyValuePair.Value))
                {
                    return(null);
                }
            }
            else
            {
                t = propertyType;
            }

            if (t == typeof(LocalDate))
            {
                var res = LocalDatePattern.Iso.Parse(keyValuePair.Value);
                if (res.Success)
                {
                    return(res.Value);
                }

                var res4 = LocalDateTimePattern.ExtendedIso.Parse(keyValuePair.Value);
                if (res4.Success && res4.Value.TimeOfDay == LocalTime.Midnight)
                {
                    return(res4.Value.Date);
                }


                if (DateTime.TryParse(keyValuePair.Value, out var d) && d == d.Date)
                {
                    return(LocalDate.FromDateTime(d));
                }

                throw _getInvalidOperationException(keyValuePair.Value);
            }

            if (t == typeof(LocalDateTime))
            {
                var res = LocalDateTimePattern.ExtendedIso.Parse(keyValuePair.Value);
                if (res.Success)
                {
                    return(res.Value);
                }

                if (DateTime.TryParse(keyValuePair.Value, out var d))
                {
                    return(LocalDateTime.FromDateTime(d));
                }

                throw _getInvalidOperationException(keyValuePair.Value);
            }

            if (t == typeof(Instant))
            {
                var res = InstantPattern.ExtendedIso.Parse(keyValuePair.Value);
                if (res.Success)
                {
                    return(res.Value);
                }

                if (DateTime.TryParse(keyValuePair.Value, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var d))
                {
                    return(Instant.FromDateTimeUtc(d));
                }

                throw _getInvalidOperationException(keyValuePair.Value);
            }

            if (t == typeof(LocalTime))
            {
                var res = LocalTimePattern.ExtendedIso.Parse(keyValuePair.Value);
                if (!res.Success)
                {
                    throw _getInvalidOperationException(keyValuePair.Value);
                }
                return(res.Value);
            }

            if (t == typeof(OffsetDateTime))
            {
                var res = OffsetDateTimePattern.ExtendedIso.Parse(keyValuePair.Value);
                if (res.Success)
                {
                    return(res.Value);
                }

                if (DateTimeOffset.TryParse(keyValuePair.Value, out var o))
                {
                    return(OffsetDateTime.FromDateTimeOffset(o));
                }

                throw _getInvalidOperationException(keyValuePair.Value);
            }

            throw new NotImplementedException();
        }
Esempio n. 35
0
        public void XmlSerialization_Iso()
        {
            var value = new OffsetDateTime(new LocalDateTime(2013, 4, 12, 17, 53, 23), Offset.FromHours(1));

            TestHelper.AssertXmlRoundtrip(value, "<value>2013-04-12T17:53:23+01:00</value>");
        }
        public bool Compare(string expectedValue, object actualValue)
        {
            switch (actualValue)
            {
            case LocalDate ld:
                {
                    var res1 = LocalDatePattern.Iso.Parse(expectedValue);
                    if (res1.Success)
                    {
                        return(res1.Value == ld);
                    }

                    var res4 = LocalDateTimePattern.ExtendedIso.Parse(expectedValue);
                    if (res4.Success && res4.Value.TimeOfDay == LocalTime.Midnight)
                    {
                        return(res4.Value.Date == ld);
                    }

                    if (DateTime.TryParse(expectedValue, out var d) && d == d.Date)
                    {
                        return(LocalDate.FromDateTime(d) == ld);
                    }
                    return(false);
                }

            case LocalDateTime ldt:
                {
                    var res2 = LocalDateTimePattern.ExtendedIso.Parse(expectedValue);
                    if (res2.Success)
                    {
                        return(res2.Value == ldt);
                    }

                    if (DateTime.TryParse(expectedValue, out var d))
                    {
                        return(LocalDateTime.FromDateTime(d) == ldt);
                    }
                    return(false);
                }

            case Instant i:
                {
                    var res3 = InstantPattern.ExtendedIso.Parse(expectedValue);
                    if (res3.Success)
                    {
                        return(res3.Value == i);
                    }

                    if (DateTime.TryParse(expectedValue, CultureInfo.CurrentCulture, DateTimeStyles.AssumeUniversal | DateTimeStyles.AdjustToUniversal, out var d))
                    {
                        return(Instant.FromDateTimeUtc(d) == i);
                    }

                    return(false);
                }

            case LocalTime t:
                {
                    var res5 = LocalTimePattern.ExtendedIso.Parse(expectedValue);
                    if (!res5.Success)
                    {
                        return(false);
                    }
                    return(res5.Value == t);
                }

            case OffsetDateTime odt:
                {
                    var res6 = OffsetDateTimePattern.ExtendedIso.Parse(expectedValue);
                    if (res6.Success)
                    {
                        return(res6.Value == odt);
                    }

                    if (DateTimeOffset.TryParse(expectedValue, out var o))
                    {
                        return(OffsetDateTime.FromDateTimeOffset(o) == odt);
                    }

                    return(false);
                }
            }

            return(false);
        }
Esempio n. 37
0
 public static DateTimeOffset Resolve(OffsetDateTime value)
 {
     return(value.ToDateTimeOffset());
 }
Esempio n. 38
0
 /// <summary>
 /// Converts a <see cref="DateTimeOffset"/> to <see cref="OffsetDateTime"/>.
 /// </summary>
 /// <remarks>This is a convenience method which calls <see cref="OffsetDateTime.FromDateTimeOffset"/>.</remarks>
 /// <param name="dateTimeOffset">The <c>DateTimeOffset</c> to convert.</param>
 /// <returns>A new <see cref="OffsetDateTime"/> with the same values as <paramref name="dateTimeOffset"/>.</returns>
 public static OffsetDateTime ToOffsetDateTime(this DateTimeOffset dateTimeOffset) =>
 OffsetDateTime.FromDateTimeOffset(dateTimeOffset);
Esempio n. 39
0
 public OffsetDateTime Test(OffsetDateTime arg)
 {
     return(arg + Duration.FromMinutes(10));
 }
 public int ValidateAndGetLength(OffsetDateTime value, NpgsqlParameter?parameter)
 => 8;
        public static OffsetDateTime PlusMicroseconds(this OffsetDateTime offsetDateTime, double microseconds)
        {
            var result = offsetDateTime.Plus(Duration.FromNanoseconds(microseconds * 1000));

            return(result);
        }
Esempio n. 42
0
 public void Write(OffsetDateTime value, NpgsqlWriteBuffer buf, NpgsqlParameter?parameter)
 => Write(value.ToInstant(), buf, parameter);
 public static OffsetDateTime PlusYears(this OffsetDateTime offsetDateTime, int years)
 {
     throw new NotImplementedException($"This method is available only for consuming via LINQ for EntityFramework translation to SQL.");
 }
Esempio n. 44
0
 public void ToString_WholeHourOffset()
 {
     LocalDateTime local = new LocalDateTime(2012, 10, 6, 1, 2, 3);
     Offset offset = Offset.FromHours(1);
     OffsetDateTime odt = new OffsetDateTime(local, offset);
     Assert.AreEqual("2012-10-06T01:02:03+01", odt.ToString());
 }
Esempio n. 45
0
        public void InFixedZone()
        {
            Offset offset = Offset.FromHours(5);
            LocalDateTime local = new LocalDateTime(2012, 1, 2, 3, 4);
            OffsetDateTime odt = new OffsetDateTime(local, offset);

            ZonedDateTime zoned = odt.InFixedZone();
            Assert.AreEqual(DateTimeZone.ForOffset(offset).AtStrictly(local), zoned);
        }
 protected override LocalDateTime GetProperty1Value(OffsetDateTime value) => value.LocalDateTime;
Esempio n. 47
0
        public DateTimeOffset ConvertToTimeZone(DateTimeOffset dateTimeOffSet, ITimeZone timeZone)
        {
            var offsetDateTime = OffsetDateTime.FromDateTimeOffset(dateTimeOffSet);

            return(offsetDateTime.InZone(((TimeZone)timeZone).DateTimeZone).ToDateTimeOffset());
        }
 protected override Offset GetProperty2Value(OffsetDateTime value) => value.Offset;
 public virtual void SupportsEquals(OffsetDateTime offsetDateTime)
 {
     SupportsPropertyOrMethod(x => x.Equals(offsetDateTime));
 }
Esempio n. 50
0
 /// <inheritdoc />
 public override bool Equals(OffsetDateTime x, OffsetDateTime y) =>
 x.localDate == y.localDate && x.NanosecondOfDay == y.NanosecondOfDay;
 public virtual void SupportsMinusOffsetDateTimeOperator(OffsetDateTime offsetDateTime)
 {
     SupportsPropertyOrMethod(x => x - offsetDateTime);
 }
Esempio n. 52
0
 /// <inheritdoc />
 public override int GetHashCode(OffsetDateTime obj) => HashCodeHelper.Hash(obj.localDate, obj.NanosecondOfDay);
 public ActionResult <string> Echo(
     [FromQuery, ModelBinder(typeof(OffsetDateTimeBinder))] OffsetDateTime time)
 {
     return(time.ToString());
 }
Esempio n. 54
0
        public void InstantComparer()
        {
            var localControl = new LocalDateTime(2013, 4, 2, 19, 54);
            var control = new OffsetDateTime(localControl, Offset.Zero);
            var equalAndOppositeChanges = control.LocalDateTime.PlusHours(1).WithOffset(Offset.FromHours(1));
            var differentCalendar = control.LocalDateTime.WithCalendar(CalendarSystem.Coptic).WithOffset(Offset.Zero);

            // Negative offset means later instant
            var negativeOffset = control.LocalDateTime.WithOffset(Offset.FromHours(-1));
            // Positive offset means earlier instant
            var positiveOffset = control.LocalDateTime.WithOffset(Offset.FromHours(1));

            // Later instant, earlier local
            var earlierLocal = control.LocalDateTime.PlusHours(-2).WithOffset(Offset.FromHours(-10));
            // Earlier instant, later local
            var laterLocal = control.LocalDateTime.PlusHours(2).WithOffset(Offset.FromHours(10));

            var comparer = OffsetDateTime.Comparer.Instant;

            Assert.AreEqual(0, comparer.Compare(control, differentCalendar));
            Assert.AreEqual(0, comparer.Compare(control, equalAndOppositeChanges));

            Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, negativeOffset)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(negativeOffset, control)));
            Assert.AreEqual(1, comparer.Compare(control, positiveOffset));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(positiveOffset, control)));

            Assert.AreEqual(-1, Math.Sign(comparer.Compare(control, earlierLocal)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(earlierLocal, control)));
            Assert.AreEqual(1, Math.Sign(comparer.Compare(control, laterLocal)));
            Assert.AreEqual(-1, Math.Sign(comparer.Compare(laterLocal, control)));

            Assert.IsTrue(comparer.Equals(control, differentCalendar));
            Assert.IsFalse(comparer.Equals(control, earlierLocal));
            Assert.IsTrue(comparer.Equals(control, equalAndOppositeChanges));
        }