/** * Returns a {@link Duration} that represents the specified number of days. * @param days The number of days to be represented by the returned {@link Duration}. * @return A {@link Duration} representing the specified number of days. */ @Nonnull public static Duration fromDays(double days) { int wholeDays = (int) days; double seconds = (days - wholeDays) * TimeConstants.SecondsPerDay; return new Duration(wholeDays, seconds); }
/** * Returns a {@link Duration} that represents the specified number of seconds. * @param seconds The number of seconds to be represented by the returned {@link Duration}. * @return A {@link Duration} representing the specified number of seconds. */ @Nonnull public static Duration fromSeconds(double seconds) { return new Duration(0, seconds); }
/** * Adds the specified {@link Duration} to this instance. * @param other The {@link Duration} to add to this instance. * @return A {@link Duration} that represents the value of this instance plus the value of {@code other}. */ @Nonnull public final Duration add(@Nonnull Duration other) { int days = m_days + other.m_days; double seconds = m_seconds + other.m_seconds; return new Duration(days, seconds); }
/** * Subtracts the specified {@link Duration} from this instance. * @param other The {@link Duration} to subtract from this instance. * @return A {@link Duration} that represents the value of this instance minus the value of {@code other}. */ @Nonnull public final Duration subtract(@Nonnull Duration other) { int days = m_days - other.m_days; double seconds = m_seconds - other.m_seconds; return new Duration(days, seconds); }
/** * Inverts a specified {@link Duration}. For example, if the {@code value} represents a positive quantity of time, the returned {@link Duration} will be a negative quantity of time. * @param value The value to invert. * @return The inverted {@link Duration}. */ @CS2JInfo("This method implements the functionality of the overloaded operator: 'Duration -(Duration)'") @Nonnull public static Duration negate(@Nonnull Duration value) { return new Duration(-value.m_days, -value.m_seconds); }
/** * Divides the {@link Duration} by a constant. * @param constant The constant by which to divide the {@link Duration}. * @return A {@link Duration} that represents the value of this instance divided by the constant. */ @CS2JWarning("Unhandled attribute removed: Pure") @Nonnull public final Duration divide(double constant) { double days = m_days / constant; int wholeDays = (int) days; double fractionOfDay = days - wholeDays; double seconds = fractionOfDay * TimeConstants.SecondsPerDay + m_seconds / constant; return new Duration(wholeDays, seconds); }
/** * Multiplies the {@link Duration} by a constant. * @param constant The constant by which to multiply the {@link Duration}. * @return A {@link Duration} that represents the value of this instance multiplied by the constant. */ @CS2JWarning("Unhandled attribute removed: Pure") @Nonnull public final Duration multiply(double constant) { double days = m_days * constant; int wholeDays = (int) days; double fractionOfDay = days - wholeDays; double seconds = fractionOfDay * TimeConstants.SecondsPerDay + m_seconds * constant; return new Duration(wholeDays, seconds); }
@Nonnull private final GregorianDate rolloverTime(double days, double hours, double minutes, double seconds, @Nonnull TimeStandard timeStandard) { int wholeDays = (int) days; seconds += (days - wholeDays) * TimeConstants.SecondsPerDay; int wholeHours = (int) hours; seconds += (hours - wholeHours) * TimeConstants.SecondsPerHour; int wholeMinutes = (int) minutes; seconds += (minutes - wholeMinutes) * TimeConstants.SecondsPerMinute; Duration timeToAdd = new Duration(wholeDays, wholeHours, wholeMinutes, seconds); JulianDate julianResult = toJulianDate(timeStandard).add(timeToAdd); return new GregorianDate(julianResult, timeStandard); }
@Nonnull private final Duration subtractIgnoringTimeStandard(@Nonnull JulianDate subtrahend) { int days = getDay() - subtrahend.getDay(); double seconds = getSecondsOfDay() - subtrahend.getSecondsOfDay(); return new Duration(days, seconds); }
/** * Subtracts a {@link Duration} from this Julian date, producing a new Julian date. * @param duration The duration to subtract. * @return A new {@link JulianDate} that is the result of the subtraction. */ @CS2JWarning("Unhandled attribute removed: Pure") @Nonnull public final JulianDate subtract(@Nonnull Duration duration) { return add(new Duration(-duration.getDays(), -duration.getSeconds())); }
/** * Tests that Duration.GetHashCode returns something at least reasonably random. */ @Test public final void testGetHashCode() { Duration duration1 = new Duration(1, 0.0); Duration duration2 = new Duration(1, 1.0); Duration duration3 = new Duration(1, 0.0); AssertHelper.assertNotEqual(duration1.hashCode(), duration2.hashCode()); Assert.assertEquals((int) duration1.hashCode(), (int) duration3.hashCode()); }
/** * Tests the Duration.TotalDays property */ @Test public final void testTotalDays() { Duration duration = new Duration(1, 43200.0); Assert.assertEquals(1.5, duration.getTotalDays(), 0d); }
/** * Test the {@link Duration#toString} method. */ @Test public final void testToString() { cesiumlanguagewriter.Duration duration = new Duration(1, 43200.0); Assert.assertEquals("1:43200", duration.toString()); }
/** * Tests division of a {@link Duration} by another. */ @Test public final void testDivisionByDuration() { Duration one = new Duration(1, 0.0); Duration two = new Duration(0, 60.0); Assert.assertEquals(86400.0 / 60.0, one.divide(two), Constants.Epsilon10); Assert.assertEquals(60 / 86400.0, Duration.divide(two, one), Constants.Epsilon10); Duration three = new Duration(1, 43200.0); Assert.assertEquals((86400 + 43200) / 60.0, Duration.divide(three, two), Constants.Epsilon10); Assert.assertEquals(60.0 / (86400.0 + 43200.0), two.divide(three), Constants.Epsilon10); Duration four = new Duration(1, 0D); Duration five = new Duration(0, -3600D); Assert.assertEquals(-24D, Duration.divide(four, five), Constants.Epsilon10); Assert.assertEquals(-1.0 / 24.0, Duration.divide(five, four), Constants.Epsilon10); Duration six = new Duration(-2, 0D); Assert.assertEquals(-0.5, Duration.divide(four, six), 0d); Assert.assertEquals(48, Duration.divide(six, five), 0d); Duration seven = new Duration(0, -0.5); Assert.assertEquals(-48 * 3600, Duration.divide(four, seven), 0d); }
/** * Tests the Duration constructors that take days, minutes, hours, and seconds. */ @Test public final void testDaysHoursMinutesSecondsConstructors() { Duration duration = new Duration(1, 1, 1, 1.0); Assert.assertEquals((int) 1, (int) duration.getDays()); Assert.assertEquals(3661, duration.getSeconds(), 0d); }
/** * Tests that initialization produces a {@link Duration} which is normalized with the seconds element in the range -86400.0 < seconds < 86400.0. */ @Test public final void testSecondsGreaterThanADay() { Duration duration = new Duration(0, 107000.0); Assert.assertEquals((int) 1, (int) duration.getDays()); Assert.assertEquals(20600.0, duration.getSeconds(), 0d); }
/** * Tests construction of a {@link Duration} with a really small negative seconds. Duration will attempt to eliminate the negative Seconds by rolling the Days back one day, but doing so results in setting the Seconds to 86400.0 seconds. This is still not normalized, so Duration should bump the Days back up and round the Seconds to 0.0. */ @Test public final void testReallySmallSeconds() { Duration duration = new Duration(10, -Constants.Epsilon13); Assert.assertEquals((int) duration.getDays(), (int) 10); Assert.assertEquals(0.0, duration.getSeconds(), 0d); duration = new Duration(-10, Constants.Epsilon13); Assert.assertEquals((int) duration.getDays(), (int) -10); Assert.assertEquals(0.0, duration.getSeconds(), 0d); }
/** * Tests that initialization produces a {@link Duration} in canonical form with day and seconds elements being either both positive or both negative. */ @Test public final void testNormalization() { // A duration's day and time should either be both negative or both positive Duration duration = new Duration(1, 100.0); Assert.assertEquals((int) 1, (int) duration.getDays()); Assert.assertEquals(100.0, duration.getSeconds(), 0d); duration = new Duration(-1, -100.0); Assert.assertEquals((int) -1, (int) duration.getDays()); Assert.assertEquals(-100.0, duration.getSeconds(), 0d); duration = new Duration(-1, 100.0); Assert.assertEquals((int) 0, (int) duration.getDays()); Assert.assertEquals(-86300.0, duration.getSeconds(), 0d); duration = new Duration(1, -100.0); Assert.assertEquals((int) 0, (int) duration.getDays()); Assert.assertEquals(86300.0, duration.getSeconds(), 0d); }
/** * Tests the Duration.AddDays method. */ @Test public final void testAddDays() { Duration test = new Duration(5, 43200.0); Duration result = test.addDays(45.5); Assert.assertEquals((int) 51, (int) result.getDays()); Assert.assertEquals(0.0, result.getSeconds(), 0d); }
/** * Tests the Duration.AddSeconds method. */ @Test public final void testAddSeconds() { Duration test = new Duration(5, 43200.0); Duration result = test.addSeconds(45.123); Assert.assertEquals((int) 5, (int) result.getDays()); Assert.assertEquals(43245.123, result.getSeconds(), 0d); }