@Nonnull private final Duration subtractIgnoringTimeStandard(@Nonnull JulianDate subtrahend) { int days = getDay() - subtrahend.getDay(); double seconds = getSecondsOfDay() - subtrahend.getSecondsOfDay(); return new Duration(days, seconds); }
/** * The constructor taking an integer Julian day number assumes that the desired {@link YearMonthDay} should represent the Gregorian day corresponding to the beginning of the provided Julian day number. If the {@link JulianDate} is more than one half day later than that, the calculation will be wrong. So, if {@code date} is more than 12 hours past the start of the Julian day, we instead use the Julian date number of tomorrow. * @param date The date. * @return The Julian day number that will produce the correct Gregorian day number. */ private static int getAdjustedJulianDayNumber(@Nonnull JulianDate date) { int day = date.getDay(); if (date.getSecondsOfDay() >= 43200.0) { ++day; } return day; }
@Nonnull private final JulianDate addIgnoringTimeStandard(@Nonnull Duration duration) { int days = getDay() + duration.getDays(); double seconds = getSecondsOfDay() + duration.getSeconds(); return new JulianDate(days, seconds, getStandard()); }
/** * Computes the number of minutes that have elapsed from this Julian date to the {@code other} Julian date. * <p> This method subtracts the Julian date on which it is called from the {@code other} Julian date and returns the number of minutes between them. The computation is done in the time standard of this Julian date, or the closest standard that is safe for arithmetic if this Julian date's time standard is not safe. For best performance, this Julian date and the {@code other} Julian date should have the same time standard and it should be safe for arithmetic. * @param other The other Julian date, which is the end of the interval. * @return The number of minutes that have elapsed from this Julian date to the other Julian date. */ @CS2JWarning("Unhandled attribute removed: Pure") public final double minutesDifference(@Nonnull JulianDate other) { JulianDate start = toInternationalAtomicTime(); JulianDate end = other.toInternationalAtomicTime(); long startDay = start.getDay(); long endDay = end.getDay(); return (endDay - startDay) * TimeConstants.MinutesPerDay + (end.getSecondsOfDay() - start.getSecondsOfDay()) / TimeConstants.SecondsPerMinute; }
/** * Computes the number of seconds that have elapsed from this Julian date to the {@code other} Julian date. * <p> This method subtracts the Julian date on which it is called from the {@code other} Julian date and returns the number of seconds between them. The computation is done in the time standard of this Julian date, or the closest standard that is safe for arithmetic if this Julian date's time standard is not safe. For best performance, this Julian date and the {@code other} Julian date should have the same time standard and it should be safe for arithmetic. * @param other The other Julian date, which is the end of the interval. * @return The number of seconds that have elapsed from this Julian date to the other Julian date. */ @CS2JWarning("Unhandled attribute removed: Pure") public final double secondsDifference(@Nonnull JulianDate other) { JulianDate start = toInternationalAtomicTime(); JulianDate end = other.toInternationalAtomicTime(); long startDay = start.getDay(); long endDay = end.getDay(); return (endDay - startDay) * TimeConstants.SecondsPerDay + (end.getSecondsOfDay() - start.getSecondsOfDay()); }
/** * Computes the number of days that have elapsed from this Julian date to the {@code other} Julian date. * <p> This method subtracts the Julian date on which it is called from the {@code other} Julian date and returns the number of days between them. The computation is done in the time standard of this Julian date, or the closest standard that is safe for arithmetic if this Julian date's time standard is not safe. For best performance, this Julian date and the {@code other} Julian date should have the same time standard and it should be safe for arithmetic. * @param other The other Julian date, which is the end of the interval. * @return The number of days that have elapsed from this Julian date to the other Julian date. */ @CS2JWarning("Unhandled attribute removed: Pure") public final double daysDifference(@Nonnull JulianDate other) { JulianDate start = toInternationalAtomicTime(); JulianDate end = other.toInternationalAtomicTime(); long startDay = start.getDay(); long endDay = end.getDay(); return endDay - startDay + (end.getSecondsOfDay() - start.getSecondsOfDay()) / TimeConstants.SecondsPerDay; }
/** * Converts this date to {@link TimeStandard#INTERNATIONAL_ATOMIC_TIME} (TAI). * @return An equivalent date expressed in TAI. */ @Nonnull public final JulianDate toInternationalAtomicTime() { if (getStandard() == TimeStandard.INTERNATIONAL_ATOMIC_TIME) { return this; } return new JulianDate(getDay(), getSecondsOfDay() + LeapSeconds.getInstance().getTaiMinusUtc(this), TimeStandard.INTERNATIONAL_ATOMIC_TIME); }
JulianDate taiCutoff = new JulianDate(lastDate.getDay(), lastDate.getSecondsOfDay(), TimeStandard.INTERNATIONAL_ATOMIC_TIME); taiCutoff = JulianDate.add(taiCutoff, Duration.fromSeconds(getOffsetForIndex(index))); if (JulianDate.lessThan(date, taiCutoff)) {
result[0] = new JulianDate(getDay(), getSecondsOfDay() + LeapSeconds.getInstance().getTaiMinusUtc(this), timeStandard); return true;
/** * Tests that initialization produces a {@link JulianDate} which is normalized with the time element in the range 0.0 <= seconds < 86400.0. */ @Test public final void testTimeGreaterThanADay() { JulianDate date = new JulianDate(2451545, 107000.0); Assert.assertEquals((int) 2451546, (int) date.getDay()); Assert.assertEquals(20600.0, date.getSecondsOfDay(), 0d); date = new JulianDate(2451545, 107000.0, TimeStandard.INTERNATIONAL_ATOMIC_TIME); Assert.assertEquals((int) 2451546, (int) date.getDay()); Assert.assertEquals(20600.0, date.getSecondsOfDay(), 0d); }
/** * Tests that initialization produces a {@link JulianDate} which is normalized with the time element in the range 0.0 <= seconds < 86400.0. */ @Test public final void testTimeNegative() { JulianDate date = new JulianDate(2451545, -45000.0); Assert.assertEquals((int) 2451544, (int) date.getDay()); Assert.assertEquals(41400.0, date.getSecondsOfDay(), 0d); date = new JulianDate(2451545, -45000.0, TimeStandard.INTERNATIONAL_ATOMIC_TIME); Assert.assertEquals((int) 2451544, (int) date.getDay()); Assert.assertEquals(41400.0, date.getSecondsOfDay(), 0d); }
/** * Tests that initialization produces a {@link JulianDate} which is normalized with the time element in the range 0.0 <= seconds < 86400.0. */ @Test public final void testTimeExactlyOneDay() { JulianDate date = new JulianDate(2451545, TimeConstants.SecondsPerDay); Assert.assertEquals((int) 2451546, (int) date.getDay()); Assert.assertEquals(0.0, date.getSecondsOfDay(), 0d); date = new JulianDate(2451545, TimeConstants.SecondsPerDay, TimeStandard.INTERNATIONAL_ATOMIC_TIME); Assert.assertEquals((int) 2451546, (int) date.getDay()); Assert.assertEquals(0.0, date.getSecondsOfDay(), 0d); }
/** * Tests that initialization of and access to the structure day and time elements is performed correctly. */ @Test public final void testRetainValue() { JulianDate date = new JulianDate(100, 123.456789012); Assert.assertEquals((int) 100, (int) date.getDay()); Assert.assertEquals(123.456789012, date.getSecondsOfDay(), 0d); }
/** * Tests construction of a {@link JulianDate} with a really small negative secondsOfDay. JulianDate will attempt to eliminate the negative SecondsOfDay by rolling the Day back one day, but doing so results in setting the SecondsOfDay to 86400.0 seconds. This is still not normalized, so JulianDate should bump the Day back up and round the SecondsOfDay to 0.0. */ @Test public final void testReallySmallNegativeSecondsOfDay() { JulianDate date = new JulianDate(2451545, -Constants.Epsilon13); Assert.assertEquals((int) date.getDay(), (int) 2451545); Assert.assertEquals(0.0, date.getSecondsOfDay(), 0d); }
/** * Tests the {@link JulianDate#subtractSeconds} method. */ @Test public final void testSubtractSeconds() { // Make sure SubtractSeconds produces the correct answer in the correct time standard. JulianDate test = new JulianDate(2451912, 0.0, TimeStandard.INTERNATIONAL_ATOMIC_TIME); JulianDate result = test.subtractSeconds(43200.25); AssertHelper.assertEquals(TimeStandard.INTERNATIONAL_ATOMIC_TIME, result.getStandard()); Assert.assertEquals((int) 2451911, (int) result.getDay()); Assert.assertEquals(43199.75, result.getSecondsOfDay(), 0d); }
/** * Tests the {@link JulianDate#addDays} method. */ @Test public final void testAddDays() { // Make sure AddDays produces the correct answer in the correct time standard. JulianDate test = new JulianDate(2451912, 43200.0, TimeStandard.INTERNATIONAL_ATOMIC_TIME); JulianDate result = test.addDays(45.5); AssertHelper.assertEquals(TimeStandard.INTERNATIONAL_ATOMIC_TIME, result.getStandard()); Assert.assertEquals((int) 2451958, (int) result.getDay()); Assert.assertEquals(0.0, result.getSecondsOfDay(), 0d); }
/** * Tests the {@link JulianDate#addSeconds} method. */ @Test public final void testAddSeconds() { // Make sure AddSeconds produces the correct answer in the correct time standard. JulianDate test = new JulianDate(2451912, 43200.0, TimeStandard.INTERNATIONAL_ATOMIC_TIME); JulianDate result = test.addSeconds(45.123); AssertHelper.assertEquals(TimeStandard.INTERNATIONAL_ATOMIC_TIME, result.getStandard()); Assert.assertEquals((int) 2451912, (int) result.getDay()); Assert.assertEquals(43245.123, result.getSecondsOfDay(), 0d); }
/** * Tests that epochs outside the leap second table range are supported. */ @Test public final void testExtremes() { LeapSeconds leapSeconds = new LeapSeconds(); JulianDate epochBefore = new JulianDate(2440000.5, TimeStandard.INTERNATIONAL_ATOMIC_TIME); Assert.assertEquals(10, leapSeconds.getTaiMinusUtc(epochBefore), 0d); epochBefore = new JulianDate(epochBefore.getDay(), epochBefore.getSecondsOfDay(), TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(10, leapSeconds.getTaiMinusUtc(epochBefore), 0d); JulianDate epochAfter = new JulianDate(2453770.5, TimeStandard.INTERNATIONAL_ATOMIC_TIME); Assert.assertEquals(33, leapSeconds.getTaiMinusUtc(epochAfter), 0d); epochAfter = new JulianDate(epochAfter.getDay(), epochAfter.getSecondsOfDay(), TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(33, leapSeconds.getTaiMinusUtc(epochAfter), 0d); }
@Test public final void testDaysDifference() { double totalElapsedTime = TimeConstants.SecondsPerDay * 2.5; double totalElapsedTimeDays = totalElapsedTime / TimeConstants.SecondsPerDay; //Test same time standard, both safe JulianDate first = new JulianDate(2451545.0); JulianDate second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); Assert.assertEquals(totalElapsedTimeDays, first.daysDifference(second), 0d); Assert.assertEquals((JulianDate.subtract(second, first)).getTotalSeconds() / TimeConstants.SecondsPerDay, first.daysDifference(second), 0d); //Test same time standard, both unsafe first = new JulianDate(2451545.0, TimeStandard.COORDINATED_UNIVERSAL_TIME); second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); second = new JulianDate(second.getDay(), second.getSecondsOfDay(), TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(totalElapsedTimeDays, first.daysDifference(second), 0d); Assert.assertEquals((JulianDate.subtract(second, first)).getTotalSeconds() / TimeConstants.SecondsPerDay, first.daysDifference(second), 0d); //Test diff time standard, one safe, one unsafe first = new JulianDate(2451545.0); second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); JulianDate secondDiffStandard = second.toTimeStandard(TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(totalElapsedTimeDays, first.daysDifference(secondDiffStandard), 0d); Assert.assertEquals((JulianDate.subtract(secondDiffStandard, first)).getTotalSeconds() / TimeConstants.SecondsPerDay, first.daysDifference(secondDiffStandard), 0d); first = JulianDate.getMinValue(); second = JulianDate.getMaxValue(); Assert.assertEquals((long) Integer.MAX_VALUE - Integer.MIN_VALUE, first.daysDifference(second), 0d); }
/** * Tests the function that determines the number of seconds between two Julian Dates */ @Test public final void testMinutesDifference() { double totalElapsedTime = TimeConstants.SecondsPerDay * 2.5; double totalElapsedTimeMinutes = totalElapsedTime / TimeConstants.SecondsPerMinute; //Test same time standard, both safe JulianDate first = new JulianDate(2451545.0); JulianDate second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); Assert.assertEquals(totalElapsedTimeMinutes, first.minutesDifference(second), 0d); Assert.assertEquals((JulianDate.subtract(second, first)).getTotalSeconds() / TimeConstants.SecondsPerMinute, first.minutesDifference(second), 0d); //Test same time standard, both unsafe first = new JulianDate(2451545.0, TimeStandard.COORDINATED_UNIVERSAL_TIME); second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); second = new JulianDate(second.getDay(), second.getSecondsOfDay(), TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(totalElapsedTimeMinutes, first.minutesDifference(second), 0d); Assert.assertEquals((JulianDate.subtract(second, first)).getTotalSeconds() / TimeConstants.SecondsPerMinute, first.minutesDifference(second), 0d); //Test diff time standard, one safe, one unsafe first = new JulianDate(2451545.0); second = JulianDate.add(first, Duration.fromSeconds(totalElapsedTime)); JulianDate secondDiffStandard = second.toTimeStandard(TimeStandard.COORDINATED_UNIVERSAL_TIME); Assert.assertEquals(totalElapsedTimeMinutes, first.minutesDifference(secondDiffStandard), 0d); Assert.assertEquals((JulianDate.subtract(secondDiffStandard, first)).getTotalSeconds() / TimeConstants.SecondsPerMinute, first.minutesDifference(secondDiffStandard), 0d); first = JulianDate.getMinValue(); second = JulianDate.getMaxValue(); Assert.assertEquals(((long) Integer.MAX_VALUE - Integer.MIN_VALUE) * TimeConstants.MinutesPerDay, first.minutesDifference(second), 0d); }