Package gov.nasa.jpl.time
Class Duration
java.lang.Object
gov.nasa.jpl.time.Duration
- All Implemented Interfaces:
ConvertableFromString
,Comparable<Duration>
This class is used to represent Durations of time. It is the complement of the SPICE-backed Time class in the same
package - subtracting two Times returns a Duration, for example. It keeps the same backing structure as the time class
(TAI tics of 10 nanoseconds each), so doing math between them is extremely easy. It supports many math functions
that planning system developers have found useful in the past, and outputs to the JPL-standard duration string format.
Even though conceptually a Duration is always a positive value, this class can represent negative Durations as well,
since it is a very common use case to offset Times in a negative direction.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Duration
static final String
static final Pattern
static final Duration
static final Pattern
static final String
static final String
static final double
static final Duration
static final Duration
static final long
static final long
static final long
static final long
static final long
static final long
static final Duration
static final String
static final double
static final long
static final Duration
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns the absolute value of the duration.Adds the parameter duration to the calling object.Rounds the duration up to an input resolution.int
div
(double divideBy) Syntactic sugar that calls divide(double)div
(int divideBy) Syntactic sugar that calls divide(int)div
(long divideBy) Syntactic sugar that calls divide(long)double
Syntactic sugar that calls divide(Duration)divide
(double divideBy) Divides the duration tics by the input double value and returns a duration.divide
(int divideBy) Divides the duration tics (floating point division) by the input int value and returns a duration.divide
(long divideBy) Divides the duration tics (floating point division) by the input long value and returns a duration.double
Divides the duration by an input duration and returns a double value.boolean
boolean
equalToWithin
(Duration d2, Duration resolution) Compares the duration to another duration object and returns true if d2 is within an input resolution of d1.Rounds the duration down to an input resolution.Returns string that is the underlying duration formatted according to the format string The format string is defined by the apache commons DurationFormatUtils specification It only returns down to millisecond accuracy, so it cannot be used for fine precisionstatic Duration
fromDays
(double days) Takes in a number of days as a double and returns a corresponding duration.static Duration
fromDays
(long days) Takes in a number of days as a long and returns a corresponding duration.static Duration
fromHours
(double hours) Takes in a number of hours as a double and returns a corresponding duration.static Duration
fromHours
(long hours) Takes in a number of hours as a long and returns a corresponding duration.static Duration
fromMarsDur
(String marsDurString) Takes in a Mars duration string of the form ddddMhh:mm:ss.fff or Mhh:mm:ss.fff where dddd is the sol number, hh is the hour, mm is the minutes, and ss.fff is the seconds.static Duration
fromMatcher
(Matcher parsedDuration) Takes a Matcher object and returns a Duration object.static Duration
fromMilliseconds
(double ms) Takes in a number of milliseconds as a double and returns a corresponding duration.static Duration
fromMilliseconds
(long ms) Takes in a number of milliseconds as a long and returns a corresponding duration.static Duration
fromMinutes
(double minutes) Takes in a number of minutes as a double and returns a corresponding duration.static Duration
fromMinutes
(long minutes) Takes in a number of minutes as a long and returns a corresponding duration.static Duration
fromSeconds
(double seconds) Takes in a number of seconds as a double and returns a corresponding duration.static Duration
fromSeconds
(long seconds) Takes in a number of seconds as a long and returns a corresponding duration.static Duration
fromTics
(long tics) Takes in a number of tics as a long and returns a corresponding duration.long
getDays()
static int
Returns the current default output decimal precision.long
getHours()
long
long
long
long
long
getTics()
boolean
greaterThan
(Duration t2) Returns true if the calling object is greater than (closer to positive infinity) the parameter, false otherwiseboolean
Returns true if the calling object is greater than or equal to the parameter, false otherwiseint
hashCode()
boolean
Returns true if the calling object is less than (closer to negative infinity) the parameter, false otherwiseboolean
Returns true if the calling object is less than or equal to the parameter, false otherwisestatic Duration
Returns the largest of a list of Durations.static Duration
Returns the smallest of a list of Durations.Syntactic sugar that calls subtract()Modulus operator, also known as remainder.multiply
(double multiplyBy) Multiplies the duration by the input double value.multiply
(int multiplyBy) Multiplies the duration by the input int value.multiply
(long multiplyBy) Multiplies the duration by the input long value.Syntactic sugar that calls add()Allows you to do t2 = d.plus(t1), which could be useful for operator overloadingRounds the duration to an input resolution.static void
setDefaultOutputPrecision
(int defaultOutputPrecision) Sets the default output decimal precision.Subtracts the parameter duration from the calling objectReturns a string representing the duration as a mars duration, using the default decimal precision.toMarsDurString
(int numberDecimalDigits) Returns a string representing the duration as a mars duration.toString()
Calls toString() with default precisiontoString
(int numDecimalPlaces) Returns string representation to number of decimal places specifieddouble
this one is special because it is a floatvoid
Given a Duration string, mutates the calling object to represent that duration Implements the convertableFromString interface so engines can call valueOf on whatever object including Durations.
-
Field Details
-
TICS_PER_SECOND_LONG
public static final long TICS_PER_SECOND_LONG- See Also:
-
TICS_PER_SECOND_DOUBLE
public static final double TICS_PER_SECOND_DOUBLE- See Also:
-
ONE_SECOND
public static final long ONE_SECOND- See Also:
-
ONE_MINUTE
public static final long ONE_MINUTE- See Also:
-
ONE_HOUR
public static final long ONE_HOUR- See Also:
-
ONE_DAY
public static final long ONE_DAY- See Also:
-
ONE_MILLISECOND
public static final long ONE_MILLISECOND- See Also:
-
ONE_MICROSECOND
public static final long ONE_MICROSECOND- See Also:
-
ZERO_DURATION
-
MICROSECOND_DURATION
-
SECOND_DURATION
-
MINUTE_DURATION
-
HOUR_DURATION
-
DAY_DURATION
-
DURATION_REGEX
- See Also:
-
durationPattern
-
SOLS_IN_DURATION_REGEX
- See Also:
-
MARS_DURATION_LESS_THAN_ONE_SOL_REGEX
- See Also:
-
MARS_DUR_REGEX
- See Also:
-
MARS_DUR_PATTERN
-
MARS_TIME_SCALE
public static final double MARS_TIME_SCALE- See Also:
-
-
Constructor Details
-
Duration
The main constructor everyone should be using - given a duration string, returns an equivalent Duration object- Parameters:
d
- A string in format HH:MM:SS.ssssss (though it is pretty forgiving about not including leading or trailing zeros)
-
Duration
public Duration()This is the empty Duration constructor, which will use 0 tics for the input. It is needed when you need to create Duration objects then assign them a value later (which should be a rare case).
-
-
Method Details
-
fromTics
Takes in a number of tics as a long and returns a corresponding duration. IMPORTANT: only intended for fast deserialization and IO assuming one backing representation of tics and is not portable between programs due to useSpiceForMath and/or backing changes in the future -
fromMilliseconds
Takes in a number of milliseconds as a double and returns a corresponding duration.- Parameters:
ms
-- Returns:
-
fromMilliseconds
Takes in a number of milliseconds as a long and returns a corresponding duration.- Parameters:
ms
-- Returns:
-
fromSeconds
Takes in a number of seconds as a double and returns a corresponding duration.- Parameters:
seconds
-- Returns:
-
fromSeconds
Takes in a number of seconds as a long and returns a corresponding duration.- Parameters:
seconds
-- Returns:
-
fromMinutes
Takes in a number of minutes as a double and returns a corresponding duration.- Parameters:
minutes
-- Returns:
-
fromMinutes
Takes in a number of minutes as a long and returns a corresponding duration.- Parameters:
minutes
-- Returns:
-
fromHours
Takes in a number of hours as a double and returns a corresponding duration.- Parameters:
hours
-- Returns:
-
fromHours
Takes in a number of hours as a long and returns a corresponding duration.- Parameters:
hours
-- Returns:
-
fromDays
Takes in a number of days as a double and returns a corresponding duration.- Parameters:
days
-- Returns:
-
fromDays
Takes in a number of days as a long and returns a corresponding duration.- Parameters:
days
-- Returns:
-
fromMarsDur
Takes in a Mars duration string of the form ddddMhh:mm:ss.fff or Mhh:mm:ss.fff where dddd is the sol number, hh is the hour, mm is the minutes, and ss.fff is the seconds.- Parameters:
marsDurString
-- Returns:
-
fromMatcher
Takes a Matcher object and returns a Duration object. IMPORTANT: assumes that find() has already been called, the matcher is queued with the groups to parse, and higher-level nice error messages are available to tell the user why the call failed/will fail. This is for performance reasons - if the calling program is doing its own input validation, you don't want to do it twice -
getTics
public long getTics()- Returns:
- A long, the underlying number of tics that comprise the duration
-
getMicroseconds
public long getMicroseconds()- Returns:
- A long, the number of microseconds in the duration. Truncates instead of rounding
-
getMilliseconds
public long getMilliseconds()- Returns:
- A long, the number of milliseconds in the duration. Truncates instead of rounding
-
totalSeconds
public double totalSeconds()this one is special because it is a float- Returns:
- A float representing the fractional seconds in the span of time. Does not round or truncate
-
getSeconds
public long getSeconds()- Returns:
- A long, the number of seconds in the duration. Truncates instead of rounding
-
getMinutes
public long getMinutes()- Returns:
- A long, the number of minutes in the duration. Truncates instead of rounding
-
getHours
public long getHours()- Returns:
- A long, the number of hours in the duration. Truncates instead of rounding
-
getDays
public long getDays()- Returns:
- A long, the number of days in the duration. Truncates instead of rounding
-
add
Adds the parameter duration to the calling object.- Parameters:
d2
-- Returns:
- A new Duration object
-
plus
Syntactic sugar that calls add()- Parameters:
d2
-- Returns:
- A new Duration object
-
plus
Allows you to do t2 = d.plus(t1), which could be useful for operator overloading- Parameters:
t2
- A time you want to add to- Returns:
- A new Time object
-
subtract
Subtracts the parameter duration from the calling object- Parameters:
d2
-- Returns:
- A new Duration object
-
minus
Syntactic sugar that calls subtract()- Parameters:
d2
-- Returns:
- A new Duration object
-
multiply
Multiplies the duration by the input double value. Rounds to the nearest microsecond or double floating point precision, whichever is larger.- Parameters:
multiplyBy
-- Returns:
- A new Duration object
-
multiply
Multiplies the duration by the input long value.- Parameters:
multiplyBy
-- Returns:
- A new Duration object
-
multiply
Multiplies the duration by the input int value.- Parameters:
multiplyBy
-- Returns:
- A new Duration object
-
divide
Divides the duration tics by the input double value and returns a duration. Rounds to the nearest microsecond or double floating point precision, whichever is larger.- Parameters:
divideBy
-- Returns:
- A new Duration object
-
div
Syntactic sugar that calls divide(double)- Parameters:
divideBy
-- Returns:
- A new Duration object
-
divide
Divides the duration tics (floating point division) by the input long value and returns a duration. Rounds to the nearest microsecond or double floating point precision, whichever is larger.- Parameters:
divideBy
-- Returns:
- A new Duration object
-
div
Syntactic sugar that calls divide(long)- Parameters:
divideBy
-- Returns:
- A new Duration object
-
divide
Divides the duration tics (floating point division) by the input int value and returns a duration. Rounds to the nearest microsecond or double floating point precision, whichever is larger.- Parameters:
divideBy
-- Returns:
- A new Duration object
-
div
Syntactic sugar that calls divide(int)- Parameters:
divideBy
-- Returns:
- A new Duration object
-
divide
Divides the duration by an input duration and returns a double value.- Parameters:
divideByDuration
-- Returns:
- double containing ratio of two Durations
-
div
Syntactic sugar that calls divide(Duration)- Parameters:
divideBy
-- Returns:
- double containing ratio of two Durations
-
mod
Modulus operator, also known as remainder. Follows Java's built in % operator in terms of behavior with negative values- Parameters:
modulus
-- Returns:
- The calling duration mod the input parameter
-
abs
Returns the absolute value of the duration. i.e. -10:00:00 will become 10:00:00.- Returns:
-
round
Rounds the duration to an input resolution.- Parameters:
resolution
-- Returns:
-
ceil
Rounds the duration up to an input resolution.- Parameters:
resolution
-- Returns:
-
floor
Rounds the duration down to an input resolution.- Parameters:
resolution
-- Returns:
-
lessThan
Returns true if the calling object is less than (closer to negative infinity) the parameter, false otherwise- Parameters:
t2
-- Returns:
- a boolean
-
greaterThan
Returns true if the calling object is greater than (closer to positive infinity) the parameter, false otherwise- Parameters:
t2
-- Returns:
-
lessThanOrEqualTo
Returns true if the calling object is less than or equal to the parameter, false otherwise- Parameters:
t2
-- Returns:
- a boolean
-
greaterThanOrEqualTo
Returns true if the calling object is greater than or equal to the parameter, false otherwise- Parameters:
t2
-- Returns:
- a boolean
-
equalToWithin
Compares the duration to another duration object and returns true if d2 is within an input resolution of d1.- Parameters:
d2
-resolution
-- Returns:
-
min
Returns the smallest of a list of Durations. Wraps Collections.min()- Parameters:
durs
- Duration objects- Returns:
- A Duration object
-
max
Returns the largest of a list of Durations. Wraps Collections.max()- Parameters:
durs
- Duration objects- Returns:
- A duration object
-
toString
Calls toString() with default precision -
toString
Returns string representation to number of decimal places specified- Parameters:
numDecimalPlaces
- The number of places to print subseconds to- Returns:
- A string representation of the duration
-
toMarsDurString
Returns a string representing the duration as a mars duration.- Parameters:
numberDecimalDigits
-- Returns:
-
toMarsDurString
Returns a string representing the duration as a mars duration, using the default decimal precision.- Returns:
-
format
Returns string that is the underlying duration formatted according to the format string The format string is defined by the apache commons DurationFormatUtils specification It only returns down to millisecond accuracy, so it cannot be used for fine precision- Parameters:
format
- Format string, an example of which would be "d 'days' HH 'hours' mm 'minutes' ss 'seconds and' S 'milliseconds'"
-
valueOf
Given a Duration string, mutates the calling object to represent that duration Implements the convertableFromString interface so engines can call valueOf on whatever object including Durations.- Specified by:
valueOf
in interfaceConvertableFromString
- Parameters:
s
-
-
compareTo
- Specified by:
compareTo
in interfaceComparable<Duration>
-
equals
-
hashCode
public int hashCode() -
setDefaultOutputPrecision
public static void setDefaultOutputPrecision(int defaultOutputPrecision) Sets the default output decimal precision.- Parameters:
defaultOutputPrecision
-
-
getDefaultOutputPrecision
public static int getDefaultOutputPrecision()Returns the current default output decimal precision.- Returns:
-