public void GetValue_WithLocalInstant()
 {
     LocalInstant when = new LocalInstant(987654321L);
     Assert.AreEqual(0, field.GetValue(new Duration(0L), when));
     Assert.AreEqual(12345, field.GetValue(new Duration(123456789L), when));
     Assert.AreEqual(-1, field.GetValue(new Duration(-12345L), when));
 }
 internal override LocalInstant SetValue(LocalInstant localInstant, long value)
 {
     FieldUtils.VerifyValueBounds(this, value, min, max);
     long wrappedValue = WrappedField.GetInt64Value(localInstant);
     long remainder = wrappedValue >= 0 ? wrappedValue % divisor : (divisor - 1) + ((wrappedValue + 1) % divisor);
     return WrappedField.SetValue(localInstant, value * divisor + remainder);
 }
 internal override int GetDifference(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
 {
     differences++;
     DiffFirstArg = minuendInstant;
     DiffSecondArg = subtrahendInstant;
     return 30;
 }
 internal override LocalInstant Add(LocalInstant localInstant, int value)
 {
     int32Additions++;
     AddInstantArg = localInstant;
     AddValueArg = value;
     return new LocalInstant(localInstant.Ticks + value * unitTicks);
 }
Esempio n. 5
0
 public void MinusOffset_Zero_IsNeutralElement()
 {
     Instant sampleInstant = new Instant(1, 23456L);
     LocalInstant sampleLocalInstant = new LocalInstant(1, 23456L);
     Assert.AreEqual(sampleInstant, sampleLocalInstant.Minus(Offset.Zero));
     Assert.AreEqual(sampleInstant, sampleLocalInstant.MinusZeroOffset());
 }
 internal override LocalInstant SetValue(LocalInstant localInstant, long value)
 {
     FieldUtils.VerifyValueBounds(this, value, 0, divisor - 1);
     int wrappedValue = WrappedField.GetValue(localInstant);
     int divided = wrappedValue >= 0 ? wrappedValue / divisor : ((wrappedValue + 1) / divisor) - 1;
     return WrappedField.SetValue(localInstant, divided * divisor + value);
 }
 public void GetZoneIntervals_WithinFirstSummer()
 {
     var early = new LocalInstant(2000, 6, 1, 0, 0);
     var pair = TestZone.GetZoneIntervals(early);
     Assert.AreEqual("Summer", pair.EarlyInterval.Name);
     Assert.IsNull(pair.LateInterval);
 }
        internal override long GetInt64Difference(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
        {
            DateTimeField field = calendarSystem.Fields.WeekYear;

            if (minuendInstant < subtrahendInstant)
            {
                return -GetInt64Difference(subtrahendInstant, minuendInstant);
            }
            int minuendWeekYear = field.GetValue(minuendInstant);
            int subtrahendWeekYear = field.GetValue(subtrahendInstant);

            Duration minuendRemainder = field.Remainder(minuendInstant);
            Duration subtrahendRemainder = field.Remainder(subtrahendInstant);

            // Balance leap weekyear differences on remainders.
            if (subtrahendRemainder >= Week53Ticks && calendarSystem.GetWeeksInYear(minuendWeekYear) <= 52)
            {
                subtrahendRemainder -= Duration.OneWeek;
            }

            int difference = minuendWeekYear - subtrahendWeekYear;
            if (minuendRemainder < subtrahendRemainder)
            {
                difference--;
            }
            return difference;
        }
        internal override LocalInstant Add(LocalInstant localInstant, long value)
        {
            int64Additions++;
            Add64InstantArg = localInstant;
            Add64ValueArg = value;

            return new LocalInstant(localInstant.Ticks + value * unitTicks);
        }
 public void GetInt64Value_WithLocalInstant()
 {
     LocalInstant when = new LocalInstant(56789L);
     Assert.AreEqual(0L, TicksDurationField.Instance.GetInt64Value(new Duration(0L), when));
     Assert.AreEqual(1234L, TicksDurationField.Instance.GetInt64Value(new Duration(1234L), when));
     Assert.AreEqual(-1234L, TicksDurationField.Instance.GetInt64Value(new Duration(-1234L), when));
     Assert.AreEqual(int.MaxValue + 1L, TicksDurationField.Instance.GetInt64Value(new Duration(int.MaxValue + 1L), when));
 }
 internal override LocalInstant SetValue(LocalInstant localInstant, long value)
 {
     FieldUtils.VerifyValueBounds(this, value, 0, GetMaximumValue());
     if (WrappedField.GetValue(localInstant) < 0)
     {
         value = -value;
     }
     return base.SetValue(localInstant, value);
 }
 internal override LocalInstant SetValue(LocalInstant localInstant, long value)
 {
     FieldUtils.VerifyValueBounds(this, value, 1, GetMaximumValue());
     if (calendarSystem.GetYear(localInstant) <= 0)
     {
         value = 1 - value;
     }
     return base.SetValue(localInstant, value);
 }
Esempio n. 13
0
        public void GetValue_DelegatesToGetInt64Value()
        {
            var field = new StubDateTimeField();
            var arg = new LocalInstant(60);

            field.GetValue(arg);

            Assert.That(field.GetInt64ValueWasCalled, Is.True);
            Assert.That(field.GetInt64ValueArg, Is.EqualTo(arg));
        }
Esempio n. 14
0
 public void FromDateTime()
 {
     LocalInstant expected = new LocalInstant(2011, 08, 18, 20, 53);
     foreach (DateTimeKind kind in Enum.GetValues(typeof(DateTimeKind)))
     {
         DateTime x = new DateTime(2011, 08, 18, 20, 53, 0, kind);
         LocalInstant actual = LocalInstant.FromDateTime(x);
         Assert.AreEqual(expected, actual);
     }
 }
        /// <summary>
        /// Add the specified month to the specified time instant.
        /// The amount added may be negative.
        /// </summary>
        /// <param name="localInstant">The local instant to update</param>
        /// <param name="value">The months to add (can be negative).</param>
        /// <returns>The updated local instant</returns>
        /// <remarks>
        /// If the new month has less total days than the specified
        /// day of the month, this value is coerced to the nearest
        /// sane value. e.g.
        /// 07-31 - (1 month) = 06-30
        /// 03-31 - (1 month) = 02-28 or 02-29 depending
        /// </remarks>
        internal override LocalInstant Add(LocalInstant localInstant, int value)
        {
            // Keep the parameter name the same as the original declaration, but
            // use a more meaningful name in the method
            int months = value;
            if (months == 0)
            {
                return localInstant;
            }
            // Save the time part first
            long timePart = calendarSystem.GetTickOfDay(localInstant);
            // Get the year and month
            int thisYear = calendarSystem.GetYear(localInstant);
            int thisMonth = calendarSystem.GetMonthOfYear(localInstant, thisYear);

            // Do not refactor without careful consideration.
            // Order of calculation is important.

            int yearToUse;
            // Initially, monthToUse is zero-based
            int monthToUse = thisMonth - 1 + months;
            if (monthToUse >= 0)
            {
                yearToUse = thisYear + (monthToUse / monthsPerYear);
                monthToUse = (monthToUse % monthsPerYear) + 1;
            }
            else
            {
                yearToUse = thisYear + (monthToUse / monthsPerYear) - 1;
                monthToUse = Math.Abs(monthToUse);
                int remMonthToUse = monthToUse % monthsPerYear;
                // Take care of the boundary condition
                if (remMonthToUse == 0)
                {
                    remMonthToUse = monthsPerYear;
                }
                monthToUse = monthsPerYear - remMonthToUse + 1;
                // Take care of the boundary condition
                if (monthToUse == 1)
                {
                    yearToUse++;
                }
            }
            // End of do not refactor.

            // Quietly force DOM to nearest sane value.
            int dayToUse = calendarSystem.GetDayOfMonth(localInstant, thisYear, thisMonth);
            int maxDay = calendarSystem.GetDaysInMonth(yearToUse, monthToUse);
            dayToUse = Math.Min(dayToUse, maxDay);
            // Get proper date part, and return result
            long datePart = calendarSystem.GetYearMonthDayTicks(yearToUse, monthToUse, dayToUse);
            return new LocalInstant(datePart + timePart);
        }
Esempio n. 16
0
        public void TestLocalInstantOperators()
        {
            const long diff = TestTime2 - TestTime1;

            var time1 = new LocalInstant(TestTime1);
            var time2 = new LocalInstant(TestTime2);
            Duration duration = time2 - time1;

            Assert.AreEqual(diff, duration.Ticks);
            Assert.AreEqual(TestTime2, (time1 + duration).Ticks);
            Assert.AreEqual(TestTime1, (time2 - duration).Ticks);
        }
        internal override LocalInstant AddWrapField(LocalInstant localInstant, int value)
        {
            if (value == 0)
            {
                return localInstant;
            }

            int thisYear = calendarSystem.GetYear(localInstant);
            int wrappedYear = FieldUtils.GetWrappedValue(thisYear, value, calendarSystem.MinYear, calendarSystem.MaxYear);

            return SetValue(localInstant, wrappedYear);
        }
Esempio n. 18
0
        public void Equality()
        {
            LocalInstant equal = new LocalInstant(1, 100L);
            LocalInstant different1 = new LocalInstant(1, 200L);
            LocalInstant different2 = new LocalInstant(2, 100L);

            TestHelper.TestEqualsStruct(equal, equal, different1);
            TestHelper.TestOperatorEquality(equal, equal, different1);

            TestHelper.TestEqualsStruct(equal, equal, different2);
            TestHelper.TestOperatorEquality(equal, equal, different2);
        }        
        internal override LocalInstant SetValue(LocalInstant localInstant, long value)
        {
            FieldUtils.VerifyValueBounds(this, value, MinimumValue, max);

            int month = (int)value;
            int thisYear = calendarSystem.GetYear(localInstant);
            int thisDom = calendarSystem.GetDayOfMonth(localInstant, thisYear);
            int maxDom = calendarSystem.GetDaysInMonth(thisYear, month);
            if (thisDom > maxDom)
            {
                // Quietly force DOM to nearest sane value.
                thisDom = maxDom;
            }
            return new LocalInstant(calendarSystem.GetYearMonthDayTicks(thisYear, month, thisDom) + calendarSystem.GetTickOfDay(localInstant));
        }
Esempio n. 20
0
        internal override LocalInstant SetValue(LocalInstant localInstant, long value)
        {
            FieldUtils.VerifyValueBounds(this, value, BeforeCommonEraIndex, CommonEraIndex);

            int oldEra = GetValue(localInstant);
            if (oldEra != value)
            {
                int year = calendarSystem.GetYear(localInstant);
                return calendarSystem.SetYear(localInstant, -year);
            }
            else
            {
                return localInstant;
            }
        }
        internal override long GetYearDifference(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
        {
            // Optimized implementation due to fixed months
            int minuendYear = GetYear(minuendInstant);
            int subtrahendYear = GetYear(subtrahendInstant);

            // Inlined remainder method to avoid duplicate calls to get.
            long minuendRem = minuendInstant.Ticks - GetYearTicks(minuendYear);
            long subtrahendRem = subtrahendInstant.Ticks - GetYearTicks(subtrahendYear);

            int difference = minuendYear - subtrahendYear;
            if (minuendRem < subtrahendRem)
            {
                difference--;
            }
            return difference;
        }
Esempio n. 22
0
        public long Subtract(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
        {
            ulong ticks;

            if (minuendInstant < subtrahendInstant)
            {
                return(-Subtract(subtrahendInstant, minuendInstant));
            }
            unchecked
            {
                // This will handle overflow appropriately, so we'll end up with the right
                // positive value.
                ticks = (ulong)(minuendInstant.Ticks - subtrahendInstant.Ticks);
                // This will naturally truncate towards 0, which is what we want.
            }
            return((long)(ticks / unitTicks));
        }
Esempio n. 23
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ZoneRecurrence"/> class.
        /// </summary>
        /// <param name="name">The name of the time zone period e.g. PST.</param>
        /// <param name="savings">The savings for this period.</param>
        /// <param name="yearOffset">The year offset of when this period starts in a year.</param>
        /// <param name="fromYear">The first year in which this recurrence is valid</param>
        /// <param name="toYear">The last year in which this recurrence is valid</param>
        public ZoneRecurrence([NotNull] String name, Offset savings, [NotNull] ZoneYearOffset yearOffset, int fromYear, int toYear)
        {
            Preconditions.CheckNotNull(name, nameof(name));
            Preconditions.CheckNotNull(yearOffset, nameof(yearOffset));

            Preconditions.CheckArgument(fromYear == int.MinValue || (fromYear >= -9998 && fromYear <= 9999), nameof(fromYear),
                                        "fromYear must be in the range [-9998, 9999] or Int32.MinValue");
            Preconditions.CheckArgument(toYear == int.MaxValue || (toYear >= -9998 && toYear <= 9999), nameof(toYear),
                                        "toYear must be in the range [-9998, 9999] or Int32.MaxValue");
            this.Name            = name;
            this.Savings         = savings;
            this.YearOffset      = yearOffset;
            this.FromYear        = fromYear;
            this.ToYear          = toYear;
            this.minLocalInstant = fromYear == int.MinValue ? LocalInstant.BeforeMinValue : yearOffset.GetOccurrenceForYear(fromYear);
            this.maxLocalInstant = toYear == int.MaxValue ? LocalInstant.AfterMaxValue : yearOffset.GetOccurrenceForYear(toYear);
        }
Esempio n. 24
0
        protected override int GetMonthOfYear(LocalInstant localInstant, int year)
        {
            int dayOfYearZeroBased = (int)((localInstant.Ticks - GetStartOfYearInTicks(year)) / NodaConstants.TicksPerStandardDay);

            if (dayOfYearZeroBased == DaysPerLeapYear - 1)
            {
                return(12);
            }
            // First 6 months are all 31 days, which makes it a simple division.
            if (dayOfYearZeroBased < 6 * 31)
            {
                return(dayOfYearZeroBased / 31 + 1);
            }
            // Remaining months are 30 days (until the last one, for a non-leap year), so just take the
            // first six months as read, divide by 30 and then re-add the first 6 months.
            return((dayOfYearZeroBased - 6 * 31) / 30 + 7);
        }
        public void UnsupportedOperations_ThrowNotSupportedException()
        {
            LocalInstant when = new LocalInstant();
            Duration duration = new Duration();

            AssertUnsupported(x => x.Add(when, 0));
            AssertUnsupported(x => x.Add(when, 0L));
            AssertUnsupported(x => x.GetDifference(when, when));
            AssertUnsupported(x => x.GetDuration(10));
            AssertUnsupported(x => x.GetDuration(10, when));
            AssertUnsupported(x => x.GetInt64Difference(when, when));
            AssertUnsupported(x => x.GetInt64Value(duration));
            AssertUnsupported(x => x.GetInt64Value(duration, when));
            AssertUnsupported(x => x.GetValue(duration));
            AssertUnsupported(x => x.GetValue(duration, when));
            AssertUnsupported(x => x.Subtract(when, 0));
            AssertUnsupported(x => x.Subtract(when, 0L));
        }
Esempio n. 26
0
        public LocalInstant Add(LocalInstant localInstant, long value)
        {
            // It's possible that there are better ways to do this, but this at least feels simple.
            if (value > 0)
            {
                // Check that we wouldn't wrap round *more* than once, by performing
                // this multiplication in a checked context.
                ulong ticks = (ulong)value * unitTicks;

                // Now add in an unchecked context...
                long newTicks;
                unchecked
                {
                    newTicks = localInstant.Ticks + (long)ticks;
                }
                // And check that we're not earlier than we should be.
                if (newTicks < localInstant.Ticks)
                {
                    throw new OverflowException("Period addition overflowed.");
                }
                return(new LocalInstant(newTicks));
            }
            else
            {
                ulong positiveValue = value == long.MinValue ? long.MaxValue + 1UL : (ulong)Math.Abs(value);
                // Check that we wouldn't wrap round *more* than once, by performing
                // this multiplication in a checked context.
                ulong ticks = positiveValue * unitTicks;

                // Now add in an unchecked context...
                long newTicks;
                unchecked
                {
                    newTicks = localInstant.Ticks - (long)ticks;
                }
                // And check that we're not later than we should be.
                if (newTicks > localInstant.Ticks)
                {
                    throw new OverflowException("Period addition overflowed.");
                }
                return(new LocalInstant(newTicks));
            }
        }
 public void UnsupportedOperations_ThrowNotSupportedException()
 {
     LocalInstant when = new LocalInstant();
     AssertUnsupported(x => x.GetInt64Value(when));
     AssertUnsupported(x => x.GetLeapAmount(when));
     AssertUnsupported(x => x.GetMaximumValue());
     AssertUnsupported(x => x.GetMaximumValue(when));
     AssertUnsupported(x => x.GetMinimumValue());
     AssertUnsupported(x => x.GetMinimumValue(when));
     AssertUnsupported(x => x.GetValue(when));
     AssertUnsupported(x => x.IsLeap(when));
     AssertUnsupported(x => x.Remainder(when));
     AssertUnsupported(x => x.RoundCeiling(when));
     AssertUnsupported(x => x.RoundFloor(when));
     AssertUnsupported(x => x.RoundHalfCeiling(when));
     AssertUnsupported(x => x.RoundHalfEven(when));
     AssertUnsupported(x => x.RoundHalfFloor(when));
     AssertUnsupported(x => x.SetValue(when, 0L));
 }
        internal override LocalInstant SetYear(LocalInstant localInstant, int year)
        {
            // Optimized implementation of set, due to fixed months
            int thisYear = GetYear(localInstant);
            int dayOfYear = GetDayOfYear(localInstant, thisYear);
            long tickOfDay = GetTickOfDay(localInstant);

            if (dayOfYear > 365)
            {
                // Current year is leap, and day is leap.
                if (!IsLeapYear(year))
                {
                    // Moving to a non-leap year, leap day doesn't exist.
                    dayOfYear--;
                }
            }

            return new LocalInstant(GetYearMonthDayTicks(year, 1, dayOfYear) + tickOfDay);
        }
Esempio n. 29
0
        // Note to self: this is (minuendInstant - subtrahendInstant) in months. So if minuendInstant
        // is later than subtrahendInstant, the result should be positive.
        internal override int MonthsBetween(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
        {
            var minuendDate    = HebrewScripturalCalculator.HebrewFromAbsolute(AbsoluteDayFromLocalInstant(minuendInstant));
            var subtrahendDate = HebrewScripturalCalculator.HebrewFromAbsolute(AbsoluteDayFromLocalInstant(subtrahendInstant));
            // First (quite rough) guess... we could probably be more efficient than this, but it's unlikely to be very far off.
            double minuendMonths    = (minuendDate.Year * MonthsPerLeapCycle) / (double)YearsPerLeapCycle + HebrewMonthConverter.ScripturalToCivil(minuendDate.Year, minuendDate.Month);
            double subtrahendMonths = (subtrahendDate.Year * MonthsPerLeapCycle) / (double)YearsPerLeapCycle + HebrewMonthConverter.ScripturalToCivil(subtrahendDate.Year, subtrahendDate.Month);
            int    diff             = (int)(minuendMonths - subtrahendMonths);

            if (subtrahendInstant <= minuendInstant)
            {
                // Go backwards until we've got a tight upper bound...
                while (AddMonths(subtrahendInstant, diff) > minuendInstant)
                {
                    diff--;
                }
                // Go forwards until we've overshot
                while (AddMonths(subtrahendInstant, diff) <= minuendInstant)
                {
                    diff++;
                }
                // Take account of the overshoot
                return(diff - 1);
            }
            else
            {
                // Moving backwards, so we need to end up with a result greater than or equal to
                // minuendInstant...
                // Go forwards until we've got a tight upper bound...
                while (AddMonths(subtrahendInstant, diff) < minuendInstant)
                {
                    diff++;
                }
                // Go backwards until we've overshot
                while (AddMonths(subtrahendInstant, diff) >= minuendInstant)
                {
                    diff--;
                }
                // Take account of the overshoot
                return(diff + 1);
            }
        }
Esempio n. 30
0
 internal static long GetTickOfDay(LocalInstant localInstant)
 {
     // This is guaranteed not to overflow based on the operations we'll be performing.
     unchecked
     {
         long ticks = localInstant.Ticks;
         if (ticks >= 0)
         {
             // Surprisingly enough, this is faster than (but equivalent to)
             // return ticks % NodaConstants.TicksPerStandardDay;
             int days = TickArithmetic.FastTicksToDays(ticks);
             return(ticks - ((days * 52734375L) << 14));
         }
         else
         {
             // I'm sure this can be optimized using shifting, but it's complicated enough as it is...
             return((NodaConstants.TicksPerStandardDay - 1) + ((ticks + 1) % NodaConstants.TicksPerStandardDay));
         }
     }
 }
Esempio n. 31
0
        internal int GetYear(LocalInstant localInstant)
        {
            long targetTicks = localInstant.Ticks;
            // Get an initial estimate of the year, and the ticks value that
            // represents the start of that year. Then verify estimate and fix if
            // necessary.

            // Initial estimate uses values divided by two to avoid overflow.
            long halfTicksPerYear           = averageTicksPerYear >> 1;
            long halfTicksSinceStartOfYear1 = (targetTicks >> 1) - (ticksAtStartOfYear1 >> 1);

            if (halfTicksSinceStartOfYear1 < 0)
            {
                // When we divide, we want to round down, not towards 0.
                halfTicksSinceStartOfYear1 += 1 - halfTicksPerYear;
            }
            int candidate = (int)(halfTicksSinceStartOfYear1 / halfTicksPerYear) + 1;

            // Most of the time we'll get the right year straight away, and we'll almost
            // always get it after one adjustment - but it's safer (and easier to think about)
            // if we just keep going until we know we're right.
            while (true)
            {
                long candidateStart = GetYearTicksSafe(candidate);
                long ticksFromCandidateStartToTarget = targetTicks - candidateStart;
                if (ticksFromCandidateStartToTarget < 0)
                {
                    // Our candidate year is later than we want.
                    candidate--;
                    continue;
                }
                long candidateLength = GetTicksInYear(candidate);
                if (ticksFromCandidateStartToTarget >= candidateLength)
                {
                    // Out candidate year is earlier than we want.
                    candidate++;
                    continue;
                }
                return(candidate);
            }
        }
        internal override LocalInstant SetYear(LocalInstant localInstant, int year)
        {
            // Optimized implementation of set, due to fixed months
            int  thisYear  = GetYear(localInstant);
            int  dayOfYear = GetDayOfYear(localInstant, thisYear);
            long tickOfDay = TimeOfDayCalculator.GetTickOfDay(localInstant);

            if (dayOfYear > 365)
            {
                // Current year is leap, and day is leap.
                if (!IsLeapYear(year))
                {
                    // Moving to a non-leap year, leap day doesn't exist.
                    dayOfYear--;
                }
            }

            long ticks = GetYearTicks(year) + (dayOfYear - 1) * NodaConstants.TicksPerStandardDay + tickOfDay;

            return(new LocalInstant(ticks));
        }
Esempio n. 33
0
        /// <summary>
        /// Adds the given transition to the transition list if it represents a new transition.
        /// </summary>
        /// <param name="transitions">The list of <see cref="ZoneTransition"/> to add to.</param>
        /// <param name="transition">The transition to add.</param>
        /// <returns><c>true</c> if the transition was added.</returns>
        private static bool AddTransition(IList <ZoneTransition> transitions, ZoneTransition transition)
        {
            int transitionCount = transitions.Count;

            if (transitionCount == 0)
            {
                transitions.Add(transition);
                return(true);
            }

            ZoneTransition lastTransition = transitions[transitionCount - 1];

            if (!transition.IsTransitionFrom(lastTransition))
            {
                return(false);
            }

            // A transition after the "beginning of time" one will always be valid.
            if (lastTransition.Instant == Instant.BeforeMinValue)
            {
                transitions.Add(transition);
                return(true);
            }

            Offset lastOffset = transitions.Count < 2 ? Offset.Zero : transitions[transitions.Count - 2].WallOffset;
            Offset newOffset  = lastTransition.WallOffset;
            // If the local time just before the new transition is the same as the local time just
            // before the previous one, just replace the last transition with new one.
            // TODO(Post-V1): It's not clear what this is doing... work it out and give an example
            LocalInstant lastLocalStart = lastTransition.Instant.Plus(lastOffset);
            LocalInstant newLocalStart  = transition.Instant.Plus(newOffset);

            if (lastLocalStart == newLocalStart)
            {
                transitions.RemoveAt(transitionCount - 1);
                return(AddTransition(transitions, transition));
            }
            transitions.Add(transition);
            return(true);
        }
Esempio n. 34
0
        internal static int GetDayOfWeek(LocalInstant localInstant)
        {
            // 1970-01-01 is day of week 4, Thursday.

            long daysSince19700101;
            long ticks = localInstant.Ticks;

            if (ticks >= 0)
            {
                daysSince19700101 = ticks / NodaConstants.TicksPerStandardDay;
            }
            else
            {
                daysSince19700101 = (ticks - (NodaConstants.TicksPerStandardDay - 1)) / NodaConstants.TicksPerStandardDay;
                if (daysSince19700101 < -3)
                {
                    return(7 + (int)((daysSince19700101 + 4) % 7));
                }
            }

            return(1 + (int)((daysSince19700101 + 3) % 7));
        }
        protected override int GetMonthOfYear(LocalInstant localInstant, int year)
        {
            // Perform a binary search to get the month. To make it go even faster,
            // compare using ints instead of longs. The number of ticks per
            // year exceeds the limit of a 32-bit int's capacity, so divide by
            // 1024 * 10000. No precision is lost (except time of day) since the number of
            // ticks per day contains 1024 * 10000 as a factor. After the division,
            // the instant isn't measured in ticks, but in units of
            // (128/125) seconds.
            int i = (int)((((localInstant.Ticks - GetStartOfYearInTicks(year))) >> 10) / NodaConstants.TicksPerMillisecond);

            return((IsLeapYear(year))
                       ? ((i < 182 * 84375)
                              ? ((i < 91 * 84375) ? ((i < 31 * 84375) ? 1 : (i < 60 * 84375) ? 2 : 3) : ((i < 121 * 84375) ? 4 : (i < 152 * 84375) ? 5 : 6))
                              : ((i < 274 * 84375)
                                     ? ((i < 213 * 84375) ? 7 : (i < 244 * 84375) ? 8 : 9)
                                     : ((i < 305 * 84375) ? 10 : (i < 335 * 84375) ? 11 : 12)))
                       : ((i < 181 * 84375)
                              ? ((i < 90 * 84375) ? ((i < 31 * 84375) ? 1 : (i < 59 * 84375) ? 2 : 3) : ((i < 120 * 84375) ? 4 : (i < 151 * 84375) ? 5 : 6))
                              : ((i < 273 * 84375)
                                     ? ((i < 212 * 84375) ? 7 : (i < 243 * 84375) ? 8 : 9)
                                     : ((i < 304 * 84375) ? 10 : (i < 334 * 84375) ? 11 : 12))));
        }
        protected internal override int GetMonthOfYear(LocalInstant localInstant, int year)
        {
            // Perform a binary search to get the month. To make it go even faster,
            // compare using ints instead of longs. The number of ticks per
            // year exceeds the limit of a 32-bit int's capacity, so divide by
            // 1024 * 10000. No precision is lost (except time of day) since the number of
            // ticks per day contains 1024 * 10000 as a factor. After the division,
            // the instant isn't measured in ticks, but in units of
            // (128/125)seconds.
            int i = (int)((((localInstant.Ticks - GetYearTicks(year))) >> 10) / NodaConstants.TicksPerMillisecond);

            return (IsLeapYear(year))
                       ? ((i < 182 * 84375)
                              ? ((i < 91 * 84375) ? ((i < 31 * 84375) ? 1 : (i < 60 * 84375) ? 2 : 3) : ((i < 121 * 84375) ? 4 : (i < 152 * 84375) ? 5 : 6))
                              : ((i < 274 * 84375)
                                     ? ((i < 213 * 84375) ? 7 : (i < 244 * 84375) ? 8 : 9)
                                     : ((i < 305 * 84375) ? 10 : (i < 335 * 84375) ? 11 : 12)))
                       : ((i < 181 * 84375)
                              ? ((i < 90 * 84375) ? ((i < 31 * 84375) ? 1 : (i < 59 * 84375) ? 2 : 3) : ((i < 120 * 84375) ? 4 : (i < 151 * 84375) ? 5 : 6))
                              : ((i < 273 * 84375)
                                     ? ((i < 212 * 84375) ? 7 : (i < 243 * 84375) ? 8 : 9)
                                     : ((i < 304 * 84375) ? 10 : (i < 334 * 84375) ? 11 : 12)));
        }
Esempio n. 37
0
        /// <summary>
        /// Change the year, maintaining month and day as well as possible. This doesn't
        /// work in the same way as other calendars; see http://judaism.stackexchange.com/questions/39053
        /// for the reasoning behind the rules.
        /// </summary>
        internal override LocalInstant SetYear(LocalInstant localInstant, int year)
        {
            long         tickOfDay         = TimeOfDayCalculator.GetTickOfDay(localInstant);
            int          absoluteSourceDay = AbsoluteDayFromLocalInstant(localInstant);
            YearMonthDay ymd                   = HebrewScripturalCalculator.HebrewFromAbsolute(absoluteSourceDay);
            int          targetDay             = ymd.Day;
            int          targetScripturalMonth = ymd.Month;

            if (targetScripturalMonth == 13 && !IsLeapYear(year))
            {
                // If we were in Adar II and the target year is not a leap year, map to Adar.
                targetScripturalMonth = 12;
            }
            else if (targetScripturalMonth == 12 && IsLeapYear(year) && !IsLeapYear(ymd.Year))
            {
                // If we were in Adar (non-leap year), go to Adar II rather than Adar I in a leap year.
                targetScripturalMonth = 13;
            }
            // If we're aiming for the 30th day of Heshvan, Kislev or an Adar, it's possible that the change in year
            // has meant the day becomes invalid. In that case, roll over to the 1st of the subsequent month.
            if (targetDay == 30 && (targetScripturalMonth == 8 || targetScripturalMonth == 9 || targetScripturalMonth == 12))
            {
                if (HebrewScripturalCalculator.DaysInMonth(year, targetScripturalMonth) != 30)
                {
                    targetDay = 1;
                    targetScripturalMonth++;
                    // From Adar, roll to Nisan.
                    if (targetScripturalMonth == 13)
                    {
                        targetScripturalMonth = 1;
                    }
                }
            }
            int absoluteTargetDay = HebrewScripturalCalculator.AbsoluteFromHebrew(year, targetScripturalMonth, targetDay);

            return(LocalInstantFromAbsoluteDay(absoluteTargetDay, tickOfDay));
        }
Esempio n. 38
0
        public long Subtract(LocalInstant minuendInstant, LocalInstant subtrahendInstant)
        {
            int minuendYear    = calculator.GetYear(minuendInstant);
            int subtrahendYear = calculator.GetYear(subtrahendInstant);

            int diff = minuendYear - subtrahendYear;

            // If we just add the difference in years to subtrahendInstant, what do we get?
            LocalInstant simpleAddition = Add(subtrahendInstant, diff);

            if (subtrahendInstant <= minuendInstant)
            {
                // Moving forward: if the result of the simple addition is before or equal to the minuend,
                // we're done. Otherwise, rewind a year because we've overshot.
                return(simpleAddition <= minuendInstant ? diff : diff - 1);
            }
            else
            {
                // Moving backward: if the result of the simple addition (of a non-positive number)
                // is after or equal to the minuend, we're done. Otherwise, increment by a year because
                // we've overshot backwards.
                return(simpleAddition >= minuendInstant ? diff : diff + 1);
            }
        }
Esempio n. 39
0
 /// <summary>
 /// Subtract subtrahendInstant from minuendInstant, in terms of months.
 /// </summary>
 internal abstract int MonthsBetween(LocalInstant minuendInstant, LocalInstant subtrahendInstant);
Esempio n. 40
0
 internal abstract LocalInstant SetYear(LocalInstant localInstant, int year);
Esempio n. 41
0
 internal abstract LocalInstant AddMonths(LocalInstant localInstant, int months);
Esempio n. 42
0
        internal static int GetClockHourOfHalfDay(LocalInstant localInstant)
        {
            int hourOfHalfDay = GetHourOfHalfDay(localInstant);

            return(hourOfHalfDay == 0 ? 12 : hourOfHalfDay);
        }
Esempio n. 43
0
 internal static int GetMillisecondOfSecond(LocalInstant localInstant)
 {
     return(GetMillisecondOfSecondFromTickOfDay(GetTickOfDay(localInstant)));
 }
Esempio n. 44
0
 internal static int GetHourOfDay(LocalInstant localInstant)
 {
     return(GetHourOfDayFromTickOfDay(GetTickOfDay(localInstant)));
 }
Esempio n. 45
0
 internal static int GetMinuteOfHour(LocalInstant localInstant)
 {
     return(GetMinuteOfHourFromTickOfDay(GetTickOfDay(localInstant)));
 }
Esempio n. 46
0
        // TODO: addWrapField

        internal override LocalInstant SetValue(LocalInstant localInstant, long value)
        {
            FieldUtils.VerifyValueBounds(this, value, GetMinimumValue(), GetMaximumValue());
            long ticks = localInstant.Ticks;
            return new LocalInstant(ticks + (value - GetInt64Value(localInstant)) * UnitTicks);
        }
Esempio n. 47
0
 internal LocalInstantWrapper(LocalInstant value) => this.value = value;
 public void GetZoneIntervals_WithinFirstWinter()
 {
     var winter = new LocalInstant(2000, 12, 1, 0, 0);
     var pair = TestZone.GetZoneIntervals(winter);
     Assert.AreEqual("Winter", pair.EarlyInterval.Name);
     Assert.IsNull(pair.LateInterval);
 }
Esempio n. 49
0
 internal override long GetInt64Value(LocalInstant localInstant)
 {
     long ticks = localInstant.Ticks;
     return ticks >= 0 ? (ticks / UnitTicks) % effectiveRange : effectiveRange - 1 + (((ticks + 1) / UnitTicks) % effectiveRange);
 }
Esempio n. 50
0
 internal int GetDayOfYear(LocalInstant localInstant)
 {
     return(GetDayOfYear(localInstant, GetYear(localInstant)));
 }
Esempio n. 51
0
 internal static int GetSecondOfMinute(LocalInstant localInstant)
 {
     return(GetSecondOfMinuteFromTickOfDay(GetTickOfDay(localInstant)));
 }
Esempio n. 52
0
 internal virtual int GetMonthOfYear(LocalInstant localInstant)
 {
     return(GetMonthOfYear(localInstant, GetYear(localInstant)));
 }
Esempio n. 53
0
 internal static int GetMinuteOfDay(LocalInstant localInstant)
 {
     return((int)(GetTickOfDay(localInstant) / NodaConstants.TicksPerMinute));
 }
Esempio n. 54
0
 /// <summary>
 /// Returns the year-of-era for the given local instant. The base implementation is to return the plain
 /// year, which is suitable for single-era calendars.
 /// </summary>
 internal virtual int GetYearOfEra(LocalInstant localInstant)
 {
     return(GetYear(localInstant));
 }
Esempio n. 55
0
 internal static int GetHourOfHalfDay(LocalInstant localInstant)
 {
     return(GetHourOfDay(localInstant) % 12);
 }
Esempio n. 56
0
 /// <summary>
 /// Returns the era for the given local instant. The base implementation is to return 0, which is
 /// suitable for single-era calendars.
 /// </summary>
 internal virtual int GetEra(LocalInstant localInstant)
 {
     return(0);
 }
 public void GetZoneIntervals_MiddleOfArbitraryAmbiguity()
 {
     var ambiguity = new LocalInstant(2010, 10, 5, 1, 30);
     var pair = TestZone.GetZoneIntervals(ambiguity);
     Assert.AreEqual("Summer", pair.EarlyInterval.Name);
     Assert.AreEqual("Winter", pair.LateInterval.Name);
 }
Esempio n. 58
0
 protected abstract int GetMonthOfYear(LocalInstant localInstant, int year);
Esempio n. 59
0
 internal static int GetMillisecondOfDay(LocalInstant localInstant)
 {
     return((int)(GetTickOfDay(localInstant) / NodaConstants.TicksPerMillisecond));
 }
 public void GetZoneIntervals_AfterArbitraryAmbiguity()
 {
     var unambiguousWinter = new LocalInstant(2010, 10, 5, 2, 0);
     var pair = TestZone.GetZoneIntervals(unambiguousWinter);
     Assert.AreEqual("Winter", pair.EarlyInterval.Name);
     Assert.IsNull(pair.LateInterval);
 }