/// <summary> /// Converts the given value object to the specified type, using the specified context and culture information. /// </summary> /// <returns> /// An <see cref="T:System.Object"/> that represents the converted value. /// </returns> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that provides a format context. </param> /// <param name="culture">A <see cref="T:System.Globalization.CultureInfo"/>. If null is passed, the current culture is assumed. </param> /// <param name="value">The <see cref="T:System.Object"/> to convert. </param> /// <param name="destinationType">The <see cref="T:System.Type"/> to convert the <paramref name="value"/> parameter to. </param> /// <exception cref="T:System.ArgumentNullException">The <paramref name="destinationType"/> parameter is null. </exception> /// <exception cref="T:System.NotSupportedException">The conversion cannot be performed. </exception> public override object ConvertTo( ITypeDescriptorContext context, CultureInfo culture, [NotNull] object value, Type destinationType) { if (value == null) { throw new ArgumentNullException(nameof(value)); } if (destinationType == null) { throw new ArgumentNullException(nameof(destinationType)); } ZonedDateTime zonedDateTime = (ZonedDateTime)value; if (destinationType == typeof(string)) { // ReSharper disable PossibleNullReferenceException return(ZonedDateTimePattern.ExtendedFormatOnlyIsoPattern .WithZoneProvider(TimeHelpers.DateTimeZoneProvider) .Format(zonedDateTime)); } // ReSharper restore PossibleNullReferenceException if (destinationType == typeof(DateTime)) { return(zonedDateTime.ToDateTimeUtc()); } if (destinationType == typeof(DateTimeOffset)) { return(zonedDateTime.ToDateTimeOffset()); } if (destinationType == typeof(Instant)) { return(zonedDateTime.ToInstant()); } if (destinationType == typeof(LocalDateTime)) { return(zonedDateTime.LocalDateTime); } if (destinationType == typeof(OffsetDateTime)) { return(zonedDateTime.ToOffsetDateTime()); } return(base.ConvertTo(context, culture, value, destinationType)); }
public void Can_Query_By_Equals_Using_A_UtcTime_OffsetDateTime_Stored_As_DateTimeOffset() { var timeZoneUnitedKingdom = DateTimeZoneProviders.Tzdb.GetZoneOrNull("Europe/London"); LocalDateTime localTimeInUnitedKingom = new LocalDateTime(2016, 06, 01, 10, 40); ZonedDateTime zonedStartDateTime = localTimeInUnitedKingom.InZoneStrictly(timeZoneUnitedKingdom); ZonedDateTime zonedFinishDateTime = zonedStartDateTime.Plus(Duration.FromMinutes(60)); ZonedDateTime matchingUtcZonedDateTime = zonedStartDateTime.WithZone(DateTimeZone.Utc); var offsetStartTime = new OffsetDateTime(zonedStartDateTime.LocalDateTime, zonedStartDateTime.Offset); var offsetFinishTime = new OffsetDateTime(zonedFinishDateTime.LocalDateTime, zonedFinishDateTime.Offset); var offsetStartTimeUtc = matchingUtcZonedDateTime.ToOffsetDateTime(); var testEvent = new OffsetDateTimeTestEntity { Description = "Can_Query_By_Equals_Using_A_UtcTime_OffsetDateTime_Stored_As_DateTimeOffset", SystemDateTimeOffset = DateTimeOffset.Now, StartOffsetDateTime = offsetStartTime, FinishOffsetDateTime = offsetFinishTime }; using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { session.Save(testEvent); transaction.Commit(); } // Even though two OffsetDateTimes may refer to the same instant... Assert.That(offsetStartTime.ToInstant(), Is.EqualTo(offsetStartTimeUtc.ToInstant())); // They are not considered equal by NodaTime unless the LocalDateTime and Offset parts are both equal. // (There is nothing wrong with this, it is perfectly valid logic for comparing two OffsetDateTimes.) Assert.That(offsetStartTime, Is.Not.EqualTo(offsetStartTimeUtc)); // However we are storing OffsetDateTimes as DateTimeOffsets in the sql server. // So when using Linq and sending the expression to the sql server, // the sql server will convert all DateTimeOffsets to utc time before comparing them. // Therefore the same two OffsetDateTimes that are not equal as shown above are seen as being equal by sql. using (ISession session = SessionFactory.OpenSession()) using (ITransaction transaction = session.BeginTransaction()) { var query = session.Query <OffsetDateTimeTestEntity>().Where(x => x.StartOffsetDateTime == offsetStartTimeUtc); var retrievedEvent = query.SingleOrDefault(); transaction.Commit(); Assert.That(testEvent, Is.Not.Null); Assert.That(testEvent, Is.EqualTo(retrievedEvent)); } }
static void ParseApple(string text) { Console.WriteLine($"Parsing {text}"); ParseResult <ZonedDateTime> result = ApplePattern.Parse(text); if (!result.Success) { Console.WriteLine($"Parse failed! {result.Exception.Message}"); return; } ZonedDateTime zonedValue = result.Value; Console.WriteLine($"ZonedDateTime: {zonedValue}"); // OffsetDateTime is a Noda Time type... OffsetDateTime offsetValue = zonedValue.ToOffsetDateTime(); Console.WriteLine($"OffsetDateTime: {offsetValue}"); // DateTimeOffset is the BCL type... DateTimeOffset dto = offsetValue.ToDateTimeOffset(); Console.WriteLine($"DateTimeOffset: {dto}"); Console.WriteLine(); }
public string GetRelativeTime(ZonedDateTime start, ZonedDateTime end) { return(GetRelativeTime(start.LocalDateTime, end.ToOffsetDateTime().SwitchOffset(start.Offset).LocalDateTime)); }