Class DurationFormat

java.lang.Object
org.apache.nifi.time.DurationFormat

public class DurationFormat extends Object
  • Field Details

    • TIME_DURATION_REGEX

      public static final String TIME_DURATION_REGEX
    • TIME_DURATION_PATTERN

      public static final Pattern TIME_DURATION_PATTERN
  • Method Details

    • getTimeDuration

      public static long getTimeDuration(String value, TimeUnit desiredUnit)
      Returns a time duration in the requested TimeUnit after parsing the String input. If the resulting value is a decimal (i.e. 25 hours -> TimeUnit.DAYS = 1.04), the value is rounded. Use getPreciseTimeDuration(String, TimeUnit) if fractional values are desirable
      Parameters:
      value - the raw String input (i.e. "28 minutes")
      desiredUnit - the requested output TimeUnit
      Returns:
      the whole number value of this duration in the requested units
      See Also:
    • getPreciseTimeDuration

      public static double getPreciseTimeDuration(String value, TimeUnit desiredUnit)
      Returns the parsed and converted input in the requested units.

      If the value is 0 <= x < 1 in the provided units, the units will first be converted to a smaller unit to get a value >= 1 (i.e. 0.5 seconds -> 500 milliseconds). This is because the underlying unit conversion cannot handle decimal values.

      If the value is x >= 1 but x is not a whole number, the units will first be converted to a smaller unit to attempt to get a whole number value (i.e. 1.5 seconds -> 1500 milliseconds).

      If the value is x < 1000 and the units are TimeUnit.NANOSECONDS, the result will be a whole number of nanoseconds, rounded (i.e. 123.4 ns -> 123 ns).

      This method handles decimal values over 1 ns, but < 1 ns will return 0 in any other unit.

      Examples:

      "10 seconds", TimeUnit.MILLISECONDS -> 10_000.0 "0.010 s", TimeUnit.MILLISECONDS -> 10.0 "0.010 s", TimeUnit.SECONDS -> 0.010 "0.010 ns", TimeUnit.NANOSECONDS -> 1 "0.010 ns", TimeUnit.MICROSECONDS -> 0

      Parameters:
      value - the String input
      desiredUnit - the desired output TimeUnit
      Returns:
      the parsed and converted amount (without a unit)
    • determineTimeUnit

      protected static TimeUnit determineTimeUnit(String rawUnit)
      Returns the TimeUnit enum that maps to the provided raw String input. The highest time unit is TimeUnit.DAYS. Any input that cannot be parsed will result in an IllegalArgumentException.
      Parameters:
      rawUnit - the String to parse
      Returns:
      the TimeUnit