Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Time

Encapsulates a nanosecond-based time value.

Instances of this class are capable of being converted from and to / to big decimal other time units and also perform basic arithmetics.

Times can also be approximated, and operations deriving from their use will also be approximated. This doesn't affect the time object value in any way, but is used by TimeWriter to demonstrate approximations.

All instances are immutable, thus not affected by methods (new instances are always created instead). Also, all time values are necessarily positive (negative times are not a thing).

Hierarchy

  • Time

Index

Constructors

constructor

  • Initializes a new instance of the Time class using the given time value parameter.

    throws

    Error When the value is invalid.

    Parameters

    • value: TimeValueSource

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number.

    • Optional approximated: undefined | false | true

      Whether the given is an approximation or not. The result of any arithmetic operations involving this time will also be approximated.

    Returns Time

  • Initializes a new instance of the Time class using the given time value parameter.

    throws

    Error When the value is invalid.

    Parameters

    • value: Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number.

    Returns Time

Accessors

bigValue

  • get bigValue(): Decimal

isApproximated

  • get isApproximated(): boolean
  • Gets whether the value of this is an approximation. This is determined at construction and it remains in all operations with that this time is involved.

    Returns boolean

nanoseconds

  • get nanoseconds(): number
  • Gets the numeric representation of the time length in nanoseconds.

    deprecated

    Since v1.1.0 - In favor of bigValue and value (will be removed in v2).

    Returns number

value

  • get value(): number
  • Gets the numeric representation of the time length, in nanoseconds.

    Returns number

Methods

add

  • Adds the value of this time with the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be added to this time.

    • Optional timeUnit: TimeUnitSource

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided.

    Returns Time

    A Time object with the values of both times added to each other.

  • Adds the value of this time with the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be added to this time.

    • Optional timeUnit: string | TimeUnit

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided. Accepts plurals and case variations.

    Returns Time

    A Time object with the values of both times added to each other.

approximate

  • approximate(): Time
  • Creates and returns a new time object that contains the value of this time, however with the approximated flag.

    Returns Time

    Approximated time in relation to this time.

divide

  • Divides the value of this time by the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to divide this time.

    • Optional timeUnit: TimeUnitSource

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided.

    Returns Time

    A Time object with the value of this time divided by the value of the other time.

  • Divides the value of this time by the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to divide this time.

    • Optional timeUnit: string | TimeUnit

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided. Accepts plurals and case variations.

    Returns Time

    A Time object with the value of this time divided by the value of the other time.

multiply

  • Multiplies the value of this time with the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be multiplied by this time.

    • Optional timeUnit: TimeUnitSource

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided.

    Returns Time

    A Time object with the value of both times multiplied by each other.

  • Multiplies the value of this time with the value of another time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be multiplied by this time.

    • Optional timeUnit: string | TimeUnit

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided. Accepts plurals and case variations.

    Returns Time

    A Time object with the value of both times multiplied by each other.

subtract

  • Subtracts the value of another time from the value of this time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be subtracted from this time.

    • Optional timeUnit: TimeUnitSource

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided.

    Returns Time

    A Time object with the value of this time divided by the value of the other time.

  • Subtracts the value of another time from the value of this time. If either are approximations, the result will also be.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource | Time

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number -or- another Time instance used to be subtracted from this time.

    • Optional timeUnit: string | TimeUnit

      Time unit used as source for the conversion of value. This defaults to "nanosecond" and is ignored when a Time object is provided. Accepts plurals and case variations.

    Returns Time

    A Time object with the value of this time divided by the value of the other time.

to

  • Converts the value of this nanosecond-based time into any other time unit.

    For the big decimal representation, see toBig.

    Parameters

    • timeUnit: TimeUnitSource

      Time unit used as target for the conversion.

    Returns number

    Numeric representation of the converted time, measured using the given time unit.

  • Converts the value of this nanosecond-based time into any other time unit.

    For the big decimal representation, see toBig.

    Parameters

    • timeUnit: string | TimeUnit

      Time unit used as target for the conversion. Accepts plurals and case variations.

    Returns number

    Numeric representation of the converted time, measured using the given time unit.

toBig

  • Converts the value of this nanosecond-based time into any other time unit, using big decimals.

    Parameters

    • timeUnit: TimeUnitSource

      Time unit used as target for the conversion.

    Returns Decimal

    Big decimal representation of the converted time, measured using the given time unit.

  • Converts the value of this nanosecond-based time into any other time unit, using big decimals.

    Parameters

    • timeUnit: string | TimeUnit

      Time unit used as target for the conversion. Accepts plurals and care variations.

    Returns Decimal

    Big decimal representation of the converted time, measured using the given time unit.

toString

  • toString(): string
  • Gets a string representing the of value of this time, in nanoseconds.

    Returns string

    Numeric representation of the time length plus the symbol "ns".

Static from

  • Converts a time value, measuring it with the specified time unit, and creates a new Time instance with an equivalent length of time, but measured in nanoseconds.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number.

    • timeUnit: TimeUnitSource

      Time unit used as source for the conversion.

    Returns Time

    A nanosecond-based time obtained from the conversion of the given parameters.

  • Converts a time value, measuring it with the specified time unit, and creates a new Time instance with an equivalent length of time, but measured in nanoseconds.

    throws

    Error When the value is invalid -or- the time unit is invalid.

    Parameters

    • value: TimeValueSource

      A value representing the length of time, accepted as a string that parses to a number (e.g. "1000" or "1e-10"), a big decimal or a number.

    • timeUnit: string | TimeUnit

      Time unit used as source for the conversion. Accepts plurals and case variations.

    Returns Time

    A nanosecond-based time obtained from the conversion of the given parameters.

Generated using TypeDoc