aboutsummaryrefslogtreecommitdiffstats
path: root/npm_assets/node_modules/luxon/src/duration.js
diff options
context:
space:
mode:
authorLibravatarUnit 193 <unit193@unit193.net>2022-04-20 00:12:09 -0400
committerLibravatarUnit 193 <unit193@unit193.net>2022-04-20 00:12:09 -0400
commit942e313727d1ad886a1024c24fe4a9e8e2e0bb3e (patch)
tree1c4d5d826655cdb812c88563a25410f8b54e41d2 /npm_assets/node_modules/luxon/src/duration.js
parent8eeed31eb2f86ac982fa4b26f93b15828289c56d (diff)
New upstream version 8.2.0.upstream/8.2.0
Diffstat (limited to 'npm_assets/node_modules/luxon/src/duration.js')
-rw-r--r--npm_assets/node_modules/luxon/src/duration.js107
1 files changed, 99 insertions, 8 deletions
diff --git a/npm_assets/node_modules/luxon/src/duration.js b/npm_assets/node_modules/luxon/src/duration.js
index 3c6889a..4fcfa4b 100644
--- a/npm_assets/node_modules/luxon/src/duration.js
+++ b/npm_assets/node_modules/luxon/src/duration.js
@@ -2,7 +2,7 @@ import { InvalidArgumentError, InvalidDurationError, InvalidUnitError } from "./
import Formatter from "./impl/formatter.js";
import Invalid from "./impl/invalid.js";
import Locale from "./impl/locale.js";
-import { parseISODuration } from "./impl/regexParser.js";
+import { parseISODuration, parseISOTimeOnly } from "./impl/regexParser.js";
import {
asNumber,
hasOwnProperty,
@@ -216,7 +216,7 @@ export default class Duration {
}
/**
- * Create a Duration from a Javascript object with keys like 'years' and 'hours.
+ * Create a Duration from a JavaScript object with keys like 'years' and 'hours'.
* If this object is empty then a zero milliseconds duration is returned.
* @param {Object} obj - the object to create the DateTime from
* @param {number} obj.years
@@ -277,6 +277,31 @@ export default class Duration {
}
/**
+ * Create a Duration from an ISO 8601 time string.
+ * @param {string} text - text to parse
+ * @param {Object} opts - options for parsing
+ * @param {string} [opts.locale='en-US'] - the locale to use
+ * @param {string} opts.numberingSystem - the numbering system to use
+ * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Times
+ * @example Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 }
+ * @example Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
+ * @example Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
+ * @example Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
+ * @example Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
+ * @return {Duration}
+ */
+ static fromISOTime(text, opts) {
+ const [parsed] = parseISOTimeOnly(text);
+ if (parsed) {
+ const obj = Object.assign(parsed, opts);
+ return Duration.fromObject(obj);
+ } else {
+ return Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`);
+ }
+ }
+
+ /**
* Create an invalid Duration.
* @param {string} reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent
* @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information
@@ -383,7 +408,7 @@ export default class Duration {
}
/**
- * Returns a Javascript object with this Duration's values.
+ * Returns a JavaScript object with this Duration's values.
* @param opts - options for generating the object
* @param {boolean} [opts.includeConfig=false] - include configuration attributes in the output
* @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 }
@@ -434,6 +459,58 @@ export default class Duration {
}
/**
+ * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day.
+ * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours.
+ * @see https://en.wikipedia.org/wiki/ISO_8601#Times
+ * @param {Object} opts - options
+ * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0
+ * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0
+ * @param {boolean} [opts.includePrefix=false] - include the `T` prefix
+ * @param {string} [opts.format='extended'] - choose between the basic and extended format
+ * @example Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000'
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00'
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00'
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000'
+ * @example Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000'
+ * @return {string}
+ */
+ toISOTime(opts = {}) {
+ if (!this.isValid) return null;
+
+ const millis = this.toMillis();
+ if (millis < 0 || millis >= 86400000) return null;
+
+ opts = Object.assign(
+ {
+ suppressMilliseconds: false,
+ suppressSeconds: false,
+ includePrefix: false,
+ format: "extended"
+ },
+ opts
+ );
+
+ const value = this.shiftTo("hours", "minutes", "seconds", "milliseconds");
+
+ let fmt = opts.format === "basic" ? "hhmm" : "hh:mm";
+
+ if (!opts.suppressSeconds || value.seconds !== 0 || value.milliseconds !== 0) {
+ fmt += opts.format === "basic" ? "ss" : ":ss";
+ if (!opts.suppressMilliseconds || value.milliseconds !== 0) {
+ fmt += ".SSS";
+ }
+ }
+
+ let str = value.toFormat(fmt);
+
+ if (opts.includePrefix) {
+ str = "T" + str;
+ }
+
+ return str;
+ }
+
+ /**
* Returns an ISO 8601 representation of this Duration appropriate for use in JSON.
* @return {string}
*/
@@ -453,11 +530,19 @@ export default class Duration {
* Returns an milliseconds value of this Duration.
* @return {number}
*/
- valueOf() {
+ toMillis() {
return this.as("milliseconds");
}
/**
+ * Returns an milliseconds value of this Duration. Alias of {@link toMillis}
+ * @return {number}
+ */
+ valueOf() {
+ return this.toMillis();
+ }
+
+ /**
* Make this Duration longer by the specified amount. Return a newly-constructed Duration.
* @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
* @return {Duration}
@@ -508,9 +593,9 @@ export default class Duration {
/**
* Get the value of unit.
* @param {string} unit - a unit such as 'minute' or 'day'
- * @example Duration.fromObject({years: 2, days: 3}).years //=> 2
- * @example Duration.fromObject({years: 2, days: 3}).months //=> 0
- * @example Duration.fromObject({years: 2, days: 3}).days //=> 3
+ * @example Duration.fromObject({years: 2, days: 3}).get('years') //=> 2
+ * @example Duration.fromObject({years: 2, days: 3}).get('months') //=> 0
+ * @example Duration.fromObject({years: 2, days: 3}).get('days') //=> 3
* @return {number}
*/
get(unit) {
@@ -762,8 +847,14 @@ export default class Duration {
return false;
}
+ function eq(v1, v2) {
+ // Consider 0 and undefined as equal
+ if (v1 === undefined || v1 === 0) return v2 === undefined || v2 === 0;
+ return v1 === v2;
+ }
+
for (const u of orderedUnits) {
- if (this.values[u] !== other.values[u]) {
+ if (!eq(this.values[u], other.values[u])) {
return false;
}
}