示例#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))))
 {
 }