예제 #1
0
 /// <summary>
 /// Private constructor only present for serialization.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param>
 /// <param name="context">The source for this deserialization.</param>
 private OffsetDateTime([NotNull] SerializationInfo info, StreamingContext context)
     : this(new LocalDateTime(new LocalDate(Preconditions.CheckNotNull(info, nameof(info)).GetInt32(DaysSerializationName),
                                            CalendarSystem.ForId(info.GetString(CalendarIdSerializationName))),
                              LocalTime.FromTicksSinceMidnight(info.GetInt64(TickOfDaySerializationName))),
            Offset.FromMilliseconds(info.GetInt32(OffsetMillisecondsSerializationName)))
 {
 }
예제 #2
0
        XmlSchema IXmlSerializable.GetSchema() => null !; // TODO(nullable): Return XmlSchema? when docfx works with that

        /// <inheritdoc />
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            Preconditions.CheckNotNull(reader, nameof(reader));
            var pattern = OffsetDateTimePattern.ExtendedIso;

            if (!reader.MoveToAttribute("zone"))
            {
                throw new ArgumentException("No zone specified in XML for ZonedDateTime");
            }
            DateTimeZone newZone = DateTimeZoneProviders.Serialization[reader.Value];

            if (reader.MoveToAttribute("calendar"))
            {
                string         newCalendarId    = reader.Value;
                CalendarSystem newCalendar      = CalendarSystem.ForId(newCalendarId);
                var            newTemplateValue = pattern.TemplateValue.WithCalendar(newCalendar);
                pattern = pattern.WithTemplateValue(newTemplateValue);
            }
            reader.MoveToElement();
            string         text           = reader.ReadElementContentAsString();
            OffsetDateTime offsetDateTime = pattern.Parse(text).Value;

            if (newZone.GetUtcOffset(offsetDateTime.ToInstant()) != offsetDateTime.Offset)
            {
                // Might as well use the exception we've already got...
                ParseResult <ZonedDateTime> .InvalidOffset(text).GetValueOrThrow();
            }
            // Use the constructor which doesn't validate the offset, as we've already done that.
            Unsafe.AsRef(this) = new ZonedDateTime(offsetDateTime, newZone);
        }
예제 #3
0
 /// <summary>
 /// Private constructor only present for serialization.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param>
 /// <param name="context">The source for this deserialization.</param>
 private ZonedDateTime(SerializationInfo info, StreamingContext context)
 // Note: this uses the constructor which explicitly validates that the offset is reasonable.
     : this(new LocalDateTime(new LocalInstant(info.GetInt64(LocalTicksSerializationName)),
                              CalendarSystem.ForId(info.GetString(CalendarIdSerializationName))),
            DateTimeZoneProviders.Serialization[info.GetString(ZoneIdSerializationName)],
            Offset.FromMilliseconds(info.GetInt32(OffsetMillisecondsSerializationName)))
 {
 }
예제 #4
0
        /// <inheritdoc />
        void IXmlSerializable.ReadXml([NotNull] XmlReader reader)
        {
            Preconditions.CheckNotNull(reader, nameof(reader));
            var pattern = OffsetDateTimePattern.Rfc3339;

            if (reader.MoveToAttribute("calendar"))
            {
                string         newCalendarId    = reader.Value;
                CalendarSystem newCalendar      = CalendarSystem.ForId(newCalendarId);
                var            newTemplateValue = pattern.TemplateValue.WithCalendar(newCalendar);
                pattern = pattern.WithTemplateValue(newTemplateValue);
                reader.MoveToElement();
            }
            string text = reader.ReadElementContentAsString();

            Unsafe.AsRef(this) = pattern.Parse(text).Value;
        }
예제 #5
0
        /// <inheritdoc />
        void IXmlSerializable.ReadXml(XmlReader reader)
        {
            Preconditions.CheckNotNull(reader, "reader");
            var pattern = LocalDatePattern.IsoPattern;

            if (reader.MoveToAttribute("calendar"))
            {
                string         newCalendarId    = reader.Value;
                CalendarSystem newCalendar      = CalendarSystem.ForId(newCalendarId);
                var            newTemplateValue = pattern.TemplateValue.WithCalendar(newCalendar);
                pattern = pattern.WithTemplateValue(newTemplateValue);
                reader.MoveToElement();
            }
            string text = reader.ReadElementContentAsString();

            this = pattern.Parse(text).Value;
        }
예제 #6
0
 /// <summary>
 /// Private constructor only present for serialization.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param>
 /// <param name="context">The source for this deserialization.</param>
 private OffsetDateTime(SerializationInfo info, StreamingContext context)
     : this(new LocalDateTime(new LocalInstant(info.GetInt64(LocalTicksSerializationName)),
                              CalendarSystem.ForId(info.GetString(CalendarIdSerializationName))),
            Offset.FromMilliseconds(info.GetInt32(OffsetMillisecondsSerializationName)))
 {
 }
예제 #7
0
 /// <summary>
 /// Private constructor only present for serialization.
 /// </summary>
 /// <param name="info">The <see cref="SerializationInfo"/> to fetch data from.</param>
 /// <param name="context">The source for this deserialization.</param>
 private LocalDate(SerializationInfo info, StreamingContext context)
     : this(new LocalDateTime(new LocalInstant(info.GetInt64(LocalTicksSerializationName)),
                              CalendarSystem.ForId(info.GetString(CalendarIdSerializationName))))
 {
 }