Parses a DataAPIDuration
from a string in one of the supported formats.
See DataAPIDuration for more info about the supported formats.
The duration to parse
new DataAPIDuration('1y2mo3w4d5h6m7s8ms9us10ns');
new DataAPIDuration('P1Y2M3DT4H5M6.007S');
duration('-2w');
duration('P0001-02-03T04:05:06');
Internal
Should not be called by user directly.
Creates a DataAPIDuration
from the given months, days, and nanoseconds.
Either all parts must be positive, or all parts must be negative, to represent the duration's sign.
The parts must be integers in the following ranges:
months
and days
must be less than or equal to 2147483647
(2^31 - 1)nanoseconds
must be less than or equal to 9223372036854775807
(2^31 - 1)The months component of the duration
The days component of the duration
The nanoseconds component of the duration
new DataAPIDuration(2, 1, 0);
duration(0, 10, 1000 * 60 * 60 * 24 * 3).negate();
Readonly
daysThe days component of this DataAPIDuration
.
May be negative if and only if the other components are also negative.
Readonly
monthsThe months component of this DataAPIDuration
.
May be negative if and only if the other components are also negative.
Readonly
nanosecondsThe nanoseconds component of this DataAPIDuration
.
May be negative if and only if the other components are also negative.
Static
Readonly
NS_Nanoseconds per hour.
Static
Readonly
NS_Nanoseconds per minute.
Static
Readonly
NS_Nanoseconds per millisecond.
Static
Readonly
NS_Nanoseconds per second.
Static
Readonly
NS_Nanoseconds per microsecond.
Implementation of $SerializeForTable
for TableCodec
A new DataAPIDuration
with the sign flipped
duration('1y').abs() // '1y'
duration('-1y').abs() // '1y'
Checks if this DataAPIDuration
is equal to another DataAPIDuration
.
Two durations are only equal if all of their individuals components are equal to each other.
The other DataAPIDuration
to compare to
true
if the durations are exactly equal, or false
otherwise
duration('1y2d').equals(duration('P1Y2D')) // true
duration('-7d').equals(duration('-P1W')) // true
duration('1y').equals(duration('12mo')) // true
duration('1y').equals(duration('365d')) // false
Checks if this DataAPIDuration
has day precision—that is, if the nanoseconds component is zero.
This means that no hours, minutes, seconds, milliseconds, microseconds, or nanoseconds are present.
true
if this DataAPIDuration
has day precision, or false
otherwise
Checks if this DataAPIDuration
has millisecond precision—that is, if the nanoseconds component is a multiple of 1,000,000.
This means that no microseconds or nanoseconds are present.
If true
, it entails that DataAPIDuration.nanoseconds & DataAPIDuration.toMicros may be safely cast to number
.
true
if this DataAPIDuration
has millisecond precision, or false
otherwise
A new DataAPIDuration
with the sign flipped
duration('1y').negate() // '-1y'
duration('-1y').negate() // '1y'
Returns a new DataAPIDuration
that is the sum of this DataAPIDuration
and another DataAPIDuration
.
Each component of the other DataAPIDuration
is added to the corresponding component of this DataAPIDuration
.
However, if the signs of the two durations differ, null
is returned. A positive duration cannot be added to a negative duration, and vice versa.
The other DataAPIDuration
to add to this DataAPIDuration
A new DataAPIDuration
that is the sum of this DataAPIDuration
and the other DataAPIDuration
, or null
if the signs differ
duration('1y').plus(duration('1y')) // '2y'
duration('1y').plus(duration('1mo1s')) // '1y1mo1s'
duration('1y').plus(duration('-1mo').abs()) // '1y1mo'
duration('1y').plus(duration('-1mo')) // null
Note that this may lead to an error being thrown if any of the individual components exceed their maximum values.
Returns the number of hours in this DataAPIDuration
, calculated solely from the nanoseconds
component.
Note: this does not factor in the months
or days
components.
Equivalent to Number(duration.nanoseconds / 3_600_000_000_000)
.
The number of hours in this DataAPIDuration
derived from the nanoseconds
component
duration('10m').toHours() // 0
duration('-50h150m').toHours() // -52
duration('1y15mo1h').toHours() // 1
duration('500d').toHours() // 0
Returns the number of microseconds in this DataAPIDuration
, calculated solely from the nanoseconds
component.
Note: this does not factor in the months
or days
components.
Equivalent to Number(duration.nanoseconds / 1_000)
.
The number of microseconds in this DataAPIDuration
derived from the nanoseconds
component
duration('1y50us').toMicros() // 50n
duration('1y50ns').toMicros() // 0n
duration('1h50ns').toMicros() // 3600000000n
Returns the number of milliseconds in this DataAPIDuration
, calculated solely from the nanoseconds
component.
Note: this does not factor in the months
or days
components.
Equivalent to Number(duration.nanoseconds / 1_000_000)
.
The number of milliseconds in this DataAPIDuration
derived from the nanoseconds
component
duration('10ns').toMillis() // 0
duration('1y50h150m').toMillis() // 189000000
duration('-1y15mo1h').toMillis() // -3600000
Returns the number of minutes in this DataAPIDuration
, calculated solely from the nanoseconds
component.
Note: this does not factor in the months
or days
components.
Equivalent to Number(duration.nanoseconds / 60_000_000_000)
.
The number of minutes in this DataAPIDuration
derived from the nanoseconds
component
duration('10ms').toMinutes() // 0
duration('2y50h150m').toMinutes() // 3150
duration('-1y15mo1h').toMinutes() // -60
Returns the number of seconds in this DataAPIDuration
, calculated solely from the nanoseconds
component.
Note: this does not factor in the months
or days
components.
Equivalent to Number(duration.nanoseconds / 1_000_000_000)
.
The number of seconds in this DataAPIDuration
derived from the nanoseconds
component
duration('10ns').toSeconds() // 0
duration('1y50h150m').toSeconds() // 189000
duration('-1y15mo1h').toSeconds() // -3600
Returns the number of years in this DataAPIDuration
, calculated solely from the months
component.
Note: this does not factor in the days
or nanoseconds
components.
Equivalent to Math.floor(duration.months / 12)
.
The number of years in this DataAPIDuration
derived from the months
component
duration('1y15mo').toYears() // 2
duration('-1y15mo').toYears() // -2
duration('1y800d').toYears() // 1
Static
[$Implementation of $DeserializeForTable
for TableCodec
Static
builderHelpful utility for manually creating new DataAPIDuration
instances.
Contains builder methods for incrementally adding duration components, and negating the final result.
You may call each .add*()
method any number of times, in any order, before calling build
.
The .negate(sign?)
method may be called at any time to negate the final result.
.negate(sign?)
to definitively set the signA base
duration may be provided to initialize the builder with its components and its sign.
Optional
base: DataAPIDurationThe base DataAPIDuration
to initialize the builder with, if any
const base = duration('1y');
// '-1y3d15h'
const span = duration.builder(base)
.addHours(10)
.addDays(3)
.addHours(5)
.negate()
.build();
DataAPIDurationBuilder
Overview
Represents a
duration
column for Data API tables.Internally represented as a number of months, days, and nanoseconds (units which are not directly convertible to each other).
Format
The duration may be one of four different formats:
Standard duration format
Matches
-?(<number><unit>)+
, where the unit is one of:y
(years; 12 months)mo
(months)w
(weeks; 7 days)d
(days)h
(hours; 3,600,000,000,000 nanoseconds)m
(minutes; 60,000,000,000 nanoseconds)s
(seconds; 1,000,000,000 nanoseconds)ms
(milliseconds; 1,000,000 nanoseconds)us
orµs
(microseconds; 1,000 nanoseconds)ns
(nanoseconds)At least one of the above units must be present, and they must be in the order shown above.
Units in this format are case-insensitive.
Example
ISO 8601 duration format
Matches
-?P<date>[T<time>]?
, where<date>
is(<number><date_unit>)*
and<time>
is(<number><time_unit>)+
.<date_unit>
is one of:Y
(years)M
(months)D
(days)<time_unit>
is one of:H
(hours)M
(minutes)S
(seconds)The P delimiter is required, and the T delimiter is only required if
<time>
is present.Units (case-sensitive) must appear in the order shown above.
Milli/micro/nanoseconds may be provided as a fractional component of the seconds unit.
Example
ISO 8601 week duration format
Matches
-?P<weeks>W
exactly. No trailing T, or any other units, are allowed in this (case-sensitive) format.Example
ISO 8601 alternate duration format
Matches
-?P<YYYY>-<MM>-<DD>T<hh>:<mm>:<ss>
exactly.The date and time components must be in the order, length, and case shown, and the P & T delimiters are required.
Example
Creation
There are a few different ways to initialize a new
DataAPIDuration
:Example
The
duration
shorthandYou may use the duration shorthand function-object anywhere when creating new
DataAPIDuration
s.Example
See the official DataStax documentation for more information.
See