Example #1
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);
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZonedDateTime"/> struct.
 /// </summary>
 /// <param name="instant">The instant.</param>
 /// <param name="zone">The time zone.</param>
 /// <param name="calendar">The calendar system.</param>
 /// <exception cref="ArgumentNullException"><paramref name="zone"/> or <paramref name="calendar"/> is null.</exception>
 public ZonedDateTime(Instant instant, DateTimeZone zone, CalendarSystem calendar)
 {
     Preconditions.CheckNotNull(zone, "zone");
     Preconditions.CheckNotNull(calendar, "calendar");
     offset        = zone.GetUtcOffset(instant);
     localDateTime = new LocalDateTime(instant.Plus(offset), calendar);
     this.zone     = zone;
 }
Example #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZonedDateTime"/> struct in the specified time zone
        /// from a given local time and offset. The offset is validated to be correct as part of initialization.
        /// In most cases a local time can only map to a single instant anyway, but the offset is included here for cases
        /// where the local time is ambiguous, usually due to daylight saving transitions.
        /// </summary>
        /// <param name="localDateTime">The local date and time.</param>
        /// <param name="zone">The time zone.</param>
        /// <param name="offset">The offset between UTC and local time at the desired instant.</param>
        /// <exception cref="ArgumentException"><paramref name="offset"/> is not a valid offset at the given
        /// local date and time.</exception>
        public ZonedDateTime(LocalDateTime localDateTime, DateTimeZone zone, Offset offset)
        {
            this.zone = Preconditions.CheckNotNull(zone, nameof(zone));
            Instant candidateInstant = localDateTime.ToLocalInstant().Minus(offset);
            Offset  correctOffset    = zone.GetUtcOffset(candidateInstant);

            // Not using Preconditions, to avoid building the string unnecessarily.
            if (correctOffset != offset)
            {
                throw new ArgumentException($"Offset {offset} is invalid for local date and time {localDateTime} in time zone {zone.Id}", nameof(offset));
            }
            offsetDateTime = new OffsetDateTime(localDateTime, offset);
        }
Example #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZonedDateTime"/> struct in the specified time zone
        /// from a given local time and offset. The offset is validated to be correct as part of initialization.
        /// In most cases a local time can only map to a single instant anyway, but the offset is included here for cases
        /// where the local time is ambiguous, usually due to daylight saving transitions.
        /// </summary>
        /// <param name="localDateTime">The local date and time.</param>
        /// <param name="zone">The time zone.</param>
        /// <param name="offset">The offset between UTC and local time at the desired instant.</param>
        /// <exception cref="ArgumentNullException"><paramref name="zone"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="offset"/> is not a valid offset at the given
        /// local date and time.</exception>
        public ZonedDateTime(LocalDateTime localDateTime, DateTimeZone zone, Offset offset)
        {
            Preconditions.CheckNotNull(zone, "zone");
            Instant candidateInstant = localDateTime.LocalInstant.Minus(offset);
            Offset  correctOffset    = zone.GetUtcOffset(candidateInstant);

            // Not using Preconditions, to avoid building the string unnecessarily.
            if (correctOffset != offset)
            {
                throw new ArgumentException("Offset " + offset + " is invalid for local date and time " + localDateTime
                                            + " in time zone " + zone.Id, "offset");
            }
            this.localDateTime = localDateTime;
            this.offset        = offset;
            this.zone          = zone;
        }
Example #5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZonedDateTime" /> struct in the specified time zone
 /// and the ISO calendar.
 /// </summary>
 /// <param name="instant">The instant.</param>
 /// <param name="zone">The time zone.</param>
 public ZonedDateTime(Instant instant, DateTimeZone zone)
 {
     this.zone      = Preconditions.CheckNotNull(zone, nameof(zone));
     offsetDateTime = new OffsetDateTime(instant, zone.GetUtcOffset(instant));
 }
Example #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ZonedDateTime"/> struct.
 /// </summary>
 /// <param name="instant">The instant.</param>
 /// <param name="zone">The time zone.</param>
 /// <param name="calendar">The calendar system.</param>
 public ZonedDateTime(Instant instant, DateTimeZone zone, CalendarSystem calendar)
 {
     this.zone      = Preconditions.CheckNotNull(zone, nameof(zone));
     offsetDateTime = new OffsetDateTime(instant, zone.GetUtcOffset(instant), Preconditions.CheckNotNull(calendar, nameof(calendar)));
 }
Example #7
0
 public void ChangeTimeZone(DateTimeZone dateTimeZone)
 {
     ZonedDateTime = new ZonedDateTime(ZonedDateTime.LocalDateTime, dateTimeZone, dateTimeZone.GetUtcOffset(Instant.FromDateTimeOffset(ZonedDateTime.ToDateTimeOffset())));
 }
Example #8
0
        public static int DaysOff(string date, string time, string rpfTimeZone, string ingestTimeZone)
        {
            int           tensPlace     = 0;
            DateTime      localDt       = DateTime.Parse(string.Format("{0} {1}", date, time));
            LocalTime     localTime     = new LocalTime(localDt.Hour, localDt.Minute, localDt.Second);
            int           hour          = localDt.Hour;
            LocalDateTime localDateTime = LocalDateTime.FromDateTime(localDt);

            DateTimeZoneProviders dtzp = new DateTimeZoneProviders();

            DateTimeZone rpfZone    = dtzp.Tzdb[rpfTimeZone];
            DateTimeZone ingestZone = dtzp.Tzdb[ingestTimeZone];

            ZonedDateTime zonedDateTime = localDateTime.InZoneLeniently(rpfZone);
            Offset        rpfOffset     = rpfZone.GetUtcOffset(zonedDateTime.ToInstant());
            Offset        ingestOffset  = ingestZone.GetUtcOffset(zonedDateTime.ToInstant());


            ZonedDateTime zeroTime   = new LocalDateTime(LocalDate.FromDateTime(localDt), new LocalTime(0, 15)).InZoneLeniently(ingestZone);
            ZonedDateTime elevenTime = new LocalDateTime(LocalDate.FromDateTime(localDt), new LocalTime(11, 15)).InZoneLeniently(ingestZone);

            ZonedDateTime zeroTimeRpf   = new LocalDateTime(LocalDate.FromDateTime(localDt), new LocalTime(0, 15)).InZoneLeniently(rpfZone);
            ZonedDateTime elevenTimeRpf = new LocalDateTime(LocalDate.FromDateTime(localDt), new LocalTime(11, 15)).InZoneLeniently(rpfZone);

            Offset zeroOffset   = zeroTimeRpf.Offset - zeroTime.Offset;
            Offset elevenOffset = elevenTimeRpf.Offset - elevenTime.Offset;

            if (Math.Abs(zeroOffset.Milliseconds) < Math.Abs(elevenOffset.Milliseconds))
            {
                var zeroClock = new LocalTime(0, 0);
                var lowTime   = zeroClock.PlusMilliseconds(-zeroOffset.Milliseconds);
                var highTime  = zeroClock.PlusMilliseconds(-elevenOffset.Milliseconds);

                if (localTime >= lowTime && localTime < highTime)
                {
                    tensPlace = 10;
                }
            }

            /*
             *
             * at0 get offsets and subtract rpfoffat0 - ingestOffsetat0  (-4)
             * at11 get offsets and subtract rpfoffat11 - ingestoffat11 (-5)
             *
             * if these are different
             *  range is (0 in time) - at0 or (0 - -4) = 4
             *   to (0 in time) -at11 or (0 - -5) = 5
             *
             *
             */

            var offsetDelta = rpfOffset - ingestOffset;
            var hrsToAdd    = offsetDelta.Milliseconds / NodaConstants.MillisecondsPerHour;

            var newHour = (hour + hrsToAdd);

            if (newHour < 0)
            {
                return(-1 - tensPlace);
            }
            else if (newHour > 24)
            {
                return(1 + tensPlace);
            }
            else
            {
                return(0 + tensPlace);
            }
        }
Example #9
-1
        /// <summary>
        /// Initializes a new instance of the <see cref="TimeZoneOffsetProvider"/> class
        /// </summary>
        /// <param name="timeZone">The time zone to provide offsets for</param>
        /// <param name="utcStartTime">The start of the range of offsets</param>
        /// <param name="utcEndTime">The en of the range of offsets</param>
        public TimeZoneOffsetProvider(DateTimeZone timeZone, DateTime utcStartTime, DateTime utcEndTime)
        {
            _timeZone = timeZone;

            // pad the end so we get the correct zone interval
            utcEndTime += TimeSpan.FromDays(2*365);

            var start = DateTimeZone.Utc.AtLeniently(LocalDateTime.FromDateTime(utcStartTime));
            var end = DateTimeZone.Utc.AtLeniently(LocalDateTime.FromDateTime(utcEndTime));
            var zoneIntervals = _timeZone.GetZoneIntervals(start.ToInstant(), end.ToInstant());
            _discontinuities = new Queue<long>(zoneIntervals.Select(x => x.Start.ToDateTimeUtc().Ticks));

            if (_discontinuities.Count == 0)
            {
                // end of discontinuities
                _nextDiscontinuity = DateTime.MaxValue.Ticks;
                _currentOffsetTicks = _timeZone.GetUtcOffset(Instant.FromDateTimeUtc(DateTime.UtcNow)).Ticks;
            }
            else
            {
                // get the offset just before the next discontinuity to initialize
                _nextDiscontinuity = _discontinuities.Dequeue();
                _currentOffsetTicks = _timeZone.GetUtcOffset(Instant.FromDateTimeUtc(new DateTime(_nextDiscontinuity - 1, DateTimeKind.Utc))).Ticks;
            }
        }