Ejemplo n.º 1
0
 /// <summary>
 /// Subtracts the given time zone offset from this local instant, to give an <see cref="Instant" />.
 /// </summary>
 /// <remarks>
 /// This would normally be implemented as an operator, but as the corresponding "plus" operation
 /// on Instant cannot be written (as Instant is a public class and LocalInstant is an internal class)
 /// it makes sense to keep them both as methods for consistency.
 /// </remarks>
 /// <param name="offset">The offset between UTC and a time zone for this local instant</param>
 /// <returns>A new <see cref="Instant"/> representing the difference of the given values.</returns>
 public Instant Minus(Offset offset)
 {
     return(new Instant(Ticks - offset.Ticks));
 }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructs a new offset date/time with the given local date and time, and the given offset from UTC.
 /// </summary>
 /// <param name="localDateTime">Local date and time to represent</param>
 /// <param name="offset">Offset from UTC</param>
 public OffsetDateTime(LocalDateTime localDateTime, Offset offset)
     : this(localDateTime.Date, new OffsetTime(localDateTime.NanosecondOfDay, offset.Seconds))
 {
 }
Ejemplo n.º 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, [NotNull] 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);
        }
Ejemplo n.º 4
0
 public void Deconstruct(out LocalDateTime localDateTime, [NotNull] out DateTimeZone dateTimeZone, out Offset offset)
 {
     localDateTime = LocalDateTime;
     dateTimeZone  = Zone;
     offset        = Offset;
 }
Ejemplo n.º 5
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);
            }
        }
Ejemplo n.º 6
0
 public void Deconstruct(out LocalDate localDate, out LocalTime localTime, out Offset offset)
 {
     localDate = LocalDateTime.Date;
     localTime = LocalDateTime.TimeOfDay;
     offset    = Offset;
 }
Ejemplo n.º 7
0
 public void Deconstruct(out LocalDateTime localDateTime, out Offset offset)
 {
     localDateTime = LocalDateTime;
     offset        = Offset;
 }