Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

duration.js 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. import { InvalidArgumentError, InvalidDurationError, InvalidUnitError } from "./errors.js";
  2. import Formatter from "./impl/formatter.js";
  3. import Invalid from "./impl/invalid.js";
  4. import Locale from "./impl/locale.js";
  5. import { parseISODuration, parseISOTimeOnly } from "./impl/regexParser.js";
  6. import {
  7. asNumber,
  8. hasOwnProperty,
  9. isNumber,
  10. isUndefined,
  11. normalizeObject,
  12. roundTo
  13. } from "./impl/util.js";
  14. import Settings from "./settings.js";
  15. const INVALID = "Invalid Duration";
  16. // unit conversion constants
  17. const lowOrderMatrix = {
  18. weeks: {
  19. days: 7,
  20. hours: 7 * 24,
  21. minutes: 7 * 24 * 60,
  22. seconds: 7 * 24 * 60 * 60,
  23. milliseconds: 7 * 24 * 60 * 60 * 1000
  24. },
  25. days: {
  26. hours: 24,
  27. minutes: 24 * 60,
  28. seconds: 24 * 60 * 60,
  29. milliseconds: 24 * 60 * 60 * 1000
  30. },
  31. hours: { minutes: 60, seconds: 60 * 60, milliseconds: 60 * 60 * 1000 },
  32. minutes: { seconds: 60, milliseconds: 60 * 1000 },
  33. seconds: { milliseconds: 1000 }
  34. },
  35. casualMatrix = Object.assign(
  36. {
  37. years: {
  38. quarters: 4,
  39. months: 12,
  40. weeks: 52,
  41. days: 365,
  42. hours: 365 * 24,
  43. minutes: 365 * 24 * 60,
  44. seconds: 365 * 24 * 60 * 60,
  45. milliseconds: 365 * 24 * 60 * 60 * 1000
  46. },
  47. quarters: {
  48. months: 3,
  49. weeks: 13,
  50. days: 91,
  51. hours: 91 * 24,
  52. minutes: 91 * 24 * 60,
  53. seconds: 91 * 24 * 60 * 60,
  54. milliseconds: 91 * 24 * 60 * 60 * 1000
  55. },
  56. months: {
  57. weeks: 4,
  58. days: 30,
  59. hours: 30 * 24,
  60. minutes: 30 * 24 * 60,
  61. seconds: 30 * 24 * 60 * 60,
  62. milliseconds: 30 * 24 * 60 * 60 * 1000
  63. }
  64. },
  65. lowOrderMatrix
  66. ),
  67. daysInYearAccurate = 146097.0 / 400,
  68. daysInMonthAccurate = 146097.0 / 4800,
  69. accurateMatrix = Object.assign(
  70. {
  71. years: {
  72. quarters: 4,
  73. months: 12,
  74. weeks: daysInYearAccurate / 7,
  75. days: daysInYearAccurate,
  76. hours: daysInYearAccurate * 24,
  77. minutes: daysInYearAccurate * 24 * 60,
  78. seconds: daysInYearAccurate * 24 * 60 * 60,
  79. milliseconds: daysInYearAccurate * 24 * 60 * 60 * 1000
  80. },
  81. quarters: {
  82. months: 3,
  83. weeks: daysInYearAccurate / 28,
  84. days: daysInYearAccurate / 4,
  85. hours: (daysInYearAccurate * 24) / 4,
  86. minutes: (daysInYearAccurate * 24 * 60) / 4,
  87. seconds: (daysInYearAccurate * 24 * 60 * 60) / 4,
  88. milliseconds: (daysInYearAccurate * 24 * 60 * 60 * 1000) / 4
  89. },
  90. months: {
  91. weeks: daysInMonthAccurate / 7,
  92. days: daysInMonthAccurate,
  93. hours: daysInMonthAccurate * 24,
  94. minutes: daysInMonthAccurate * 24 * 60,
  95. seconds: daysInMonthAccurate * 24 * 60 * 60,
  96. milliseconds: daysInMonthAccurate * 24 * 60 * 60 * 1000
  97. }
  98. },
  99. lowOrderMatrix
  100. );
  101. // units ordered by size
  102. const orderedUnits = [
  103. "years",
  104. "quarters",
  105. "months",
  106. "weeks",
  107. "days",
  108. "hours",
  109. "minutes",
  110. "seconds",
  111. "milliseconds"
  112. ];
  113. const reverseUnits = orderedUnits.slice(0).reverse();
  114. // clone really means "create another instance just like this one, but with these changes"
  115. function clone(dur, alts, clear = false) {
  116. // deep merge for vals
  117. const conf = {
  118. values: clear ? alts.values : Object.assign({}, dur.values, alts.values || {}),
  119. loc: dur.loc.clone(alts.loc),
  120. conversionAccuracy: alts.conversionAccuracy || dur.conversionAccuracy
  121. };
  122. return new Duration(conf);
  123. }
  124. function antiTrunc(n) {
  125. return n < 0 ? Math.floor(n) : Math.ceil(n);
  126. }
  127. // NB: mutates parameters
  128. function convert(matrix, fromMap, fromUnit, toMap, toUnit) {
  129. const conv = matrix[toUnit][fromUnit],
  130. raw = fromMap[fromUnit] / conv,
  131. sameSign = Math.sign(raw) === Math.sign(toMap[toUnit]),
  132. // ok, so this is wild, but see the matrix in the tests
  133. added =
  134. !sameSign && toMap[toUnit] !== 0 && Math.abs(raw) <= 1 ? antiTrunc(raw) : Math.trunc(raw);
  135. toMap[toUnit] += added;
  136. fromMap[fromUnit] -= added * conv;
  137. }
  138. // NB: mutates parameters
  139. function normalizeValues(matrix, vals) {
  140. reverseUnits.reduce((previous, current) => {
  141. if (!isUndefined(vals[current])) {
  142. if (previous) {
  143. convert(matrix, vals, previous, vals, current);
  144. }
  145. return current;
  146. } else {
  147. return previous;
  148. }
  149. }, null);
  150. }
  151. /**
  152. * A Duration object represents a period of time, like "2 months" or "1 day, 1 hour". Conceptually, it's just a map of units to their quantities, accompanied by some additional configuration and methods for creating, parsing, interrogating, transforming, and formatting them. They can be used on their own or in conjunction with other Luxon types; for example, you can use {@link DateTime.plus} to add a Duration object to a DateTime, producing another DateTime.
  153. *
  154. * Here is a brief overview of commonly used methods and getters in Duration:
  155. *
  156. * * **Creation** To create a Duration, use {@link Duration.fromMillis}, {@link Duration.fromObject}, or {@link Duration.fromISO}.
  157. * * **Unit values** See the {@link Duration.years}, {@link Duration.months}, {@link Duration.weeks}, {@link Duration.days}, {@link Duration.hours}, {@link Duration.minutes}, {@link Duration.seconds}, {@link Duration.milliseconds} accessors.
  158. * * **Configuration** See {@link Duration.locale} and {@link Duration.numberingSystem} accessors.
  159. * * **Transformation** To create new Durations out of old ones use {@link Duration.plus}, {@link Duration.minus}, {@link Duration.normalize}, {@link Duration.set}, {@link Duration.reconfigure}, {@link Duration.shiftTo}, and {@link Duration.negate}.
  160. * * **Output** To convert the Duration into other representations, see {@link Duration.as}, {@link Duration.toISO}, {@link Duration.toFormat}, and {@link Duration.toJSON}
  161. *
  162. * There's are more methods documented below. In addition, for more information on subtler topics like internationalization and validity, see the external documentation.
  163. */
  164. export default class Duration {
  165. /**
  166. * @private
  167. */
  168. constructor(config) {
  169. const accurate = config.conversionAccuracy === "longterm" || false;
  170. /**
  171. * @access private
  172. */
  173. this.values = config.values;
  174. /**
  175. * @access private
  176. */
  177. this.loc = config.loc || Locale.create();
  178. /**
  179. * @access private
  180. */
  181. this.conversionAccuracy = accurate ? "longterm" : "casual";
  182. /**
  183. * @access private
  184. */
  185. this.invalid = config.invalid || null;
  186. /**
  187. * @access private
  188. */
  189. this.matrix = accurate ? accurateMatrix : casualMatrix;
  190. /**
  191. * @access private
  192. */
  193. this.isLuxonDuration = true;
  194. }
  195. /**
  196. * Create Duration from a number of milliseconds.
  197. * @param {number} count of milliseconds
  198. * @param {Object} opts - options for parsing
  199. * @param {string} [opts.locale='en-US'] - the locale to use
  200. * @param {string} opts.numberingSystem - the numbering system to use
  201. * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use
  202. * @return {Duration}
  203. */
  204. static fromMillis(count, opts) {
  205. return Duration.fromObject(Object.assign({ milliseconds: count }, opts));
  206. }
  207. /**
  208. * Create a Duration from a JavaScript object with keys like 'years' and 'hours.
  209. * If this object is empty then a zero milliseconds duration is returned.
  210. * @param {Object} obj - the object to create the DateTime from
  211. * @param {number} obj.years
  212. * @param {number} obj.quarters
  213. * @param {number} obj.months
  214. * @param {number} obj.weeks
  215. * @param {number} obj.days
  216. * @param {number} obj.hours
  217. * @param {number} obj.minutes
  218. * @param {number} obj.seconds
  219. * @param {number} obj.milliseconds
  220. * @param {string} [obj.locale='en-US'] - the locale to use
  221. * @param {string} obj.numberingSystem - the numbering system to use
  222. * @param {string} [obj.conversionAccuracy='casual'] - the conversion system to use
  223. * @return {Duration}
  224. */
  225. static fromObject(obj) {
  226. if (obj == null || typeof obj !== "object") {
  227. throw new InvalidArgumentError(
  228. `Duration.fromObject: argument expected to be an object, got ${
  229. obj === null ? "null" : typeof obj
  230. }`
  231. );
  232. }
  233. return new Duration({
  234. values: normalizeObject(obj, Duration.normalizeUnit, [
  235. "locale",
  236. "numberingSystem",
  237. "conversionAccuracy",
  238. "zone" // a bit of debt; it's super inconvenient internally not to be able to blindly pass this
  239. ]),
  240. loc: Locale.fromObject(obj),
  241. conversionAccuracy: obj.conversionAccuracy
  242. });
  243. }
  244. /**
  245. * Create a Duration from an ISO 8601 duration string.
  246. * @param {string} text - text to parse
  247. * @param {Object} opts - options for parsing
  248. * @param {string} [opts.locale='en-US'] - the locale to use
  249. * @param {string} opts.numberingSystem - the numbering system to use
  250. * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use
  251. * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
  252. * @example Duration.fromISO('P3Y6M1W4DT12H30M5S').toObject() //=> { years: 3, months: 6, weeks: 1, days: 4, hours: 12, minutes: 30, seconds: 5 }
  253. * @example Duration.fromISO('PT23H').toObject() //=> { hours: 23 }
  254. * @example Duration.fromISO('P5Y3M').toObject() //=> { years: 5, months: 3 }
  255. * @return {Duration}
  256. */
  257. static fromISO(text, opts) {
  258. const [parsed] = parseISODuration(text);
  259. if (parsed) {
  260. const obj = Object.assign(parsed, opts);
  261. return Duration.fromObject(obj);
  262. } else {
  263. return Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`);
  264. }
  265. }
  266. /**
  267. * Create a Duration from an ISO 8601 time string.
  268. * @param {string} text - text to parse
  269. * @param {Object} opts - options for parsing
  270. * @param {string} [opts.locale='en-US'] - the locale to use
  271. * @param {string} opts.numberingSystem - the numbering system to use
  272. * @param {string} [opts.conversionAccuracy='casual'] - the conversion system to use
  273. * @see https://en.wikipedia.org/wiki/ISO_8601#Times
  274. * @example Duration.fromISOTime('11:22:33.444').toObject() //=> { hours: 11, minutes: 22, seconds: 33, milliseconds: 444 }
  275. * @example Duration.fromISOTime('11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
  276. * @example Duration.fromISOTime('T11:00').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
  277. * @example Duration.fromISOTime('1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
  278. * @example Duration.fromISOTime('T1100').toObject() //=> { hours: 11, minutes: 0, seconds: 0 }
  279. * @return {Duration}
  280. */
  281. static fromISOTime(text, opts) {
  282. const [parsed] = parseISOTimeOnly(text);
  283. if (parsed) {
  284. const obj = Object.assign(parsed, opts);
  285. return Duration.fromObject(obj);
  286. } else {
  287. return Duration.invalid("unparsable", `the input "${text}" can't be parsed as ISO 8601`);
  288. }
  289. }
  290. /**
  291. * Create an invalid Duration.
  292. * @param {string} reason - simple string of why this datetime is invalid. Should not contain parameters or anything else data-dependent
  293. * @param {string} [explanation=null] - longer explanation, may include parameters and other useful debugging information
  294. * @return {Duration}
  295. */
  296. static invalid(reason, explanation = null) {
  297. if (!reason) {
  298. throw new InvalidArgumentError("need to specify a reason the Duration is invalid");
  299. }
  300. const invalid = reason instanceof Invalid ? reason : new Invalid(reason, explanation);
  301. if (Settings.throwOnInvalid) {
  302. throw new InvalidDurationError(invalid);
  303. } else {
  304. return new Duration({ invalid });
  305. }
  306. }
  307. /**
  308. * @private
  309. */
  310. static normalizeUnit(unit) {
  311. const normalized = {
  312. year: "years",
  313. years: "years",
  314. quarter: "quarters",
  315. quarters: "quarters",
  316. month: "months",
  317. months: "months",
  318. week: "weeks",
  319. weeks: "weeks",
  320. day: "days",
  321. days: "days",
  322. hour: "hours",
  323. hours: "hours",
  324. minute: "minutes",
  325. minutes: "minutes",
  326. second: "seconds",
  327. seconds: "seconds",
  328. millisecond: "milliseconds",
  329. milliseconds: "milliseconds"
  330. }[unit ? unit.toLowerCase() : unit];
  331. if (!normalized) throw new InvalidUnitError(unit);
  332. return normalized;
  333. }
  334. /**
  335. * Check if an object is a Duration. Works across context boundaries
  336. * @param {object} o
  337. * @return {boolean}
  338. */
  339. static isDuration(o) {
  340. return (o && o.isLuxonDuration) || false;
  341. }
  342. /**
  343. * Get the locale of a Duration, such 'en-GB'
  344. * @type {string}
  345. */
  346. get locale() {
  347. return this.isValid ? this.loc.locale : null;
  348. }
  349. /**
  350. * Get the numbering system of a Duration, such 'beng'. The numbering system is used when formatting the Duration
  351. *
  352. * @type {string}
  353. */
  354. get numberingSystem() {
  355. return this.isValid ? this.loc.numberingSystem : null;
  356. }
  357. /**
  358. * Returns a string representation of this Duration formatted according to the specified format string. You may use these tokens:
  359. * * `S` for milliseconds
  360. * * `s` for seconds
  361. * * `m` for minutes
  362. * * `h` for hours
  363. * * `d` for days
  364. * * `M` for months
  365. * * `y` for years
  366. * Notes:
  367. * * Add padding by repeating the token, e.g. "yy" pads the years to two digits, "hhhh" pads the hours out to four digits
  368. * * The duration will be converted to the set of units in the format string using {@link Duration.shiftTo} and the Durations's conversion accuracy setting.
  369. * @param {string} fmt - the format string
  370. * @param {Object} opts - options
  371. * @param {boolean} [opts.floor=true] - floor numerical values
  372. * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("y d s") //=> "1 6 2"
  373. * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("yy dd sss") //=> "01 06 002"
  374. * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toFormat("M S") //=> "12 518402000"
  375. * @return {string}
  376. */
  377. toFormat(fmt, opts = {}) {
  378. // reverse-compat since 1.2; we always round down now, never up, and we do it by default
  379. const fmtOpts = Object.assign({}, opts, {
  380. floor: opts.round !== false && opts.floor !== false
  381. });
  382. return this.isValid
  383. ? Formatter.create(this.loc, fmtOpts).formatDurationFromString(this, fmt)
  384. : INVALID;
  385. }
  386. /**
  387. * Returns a JavaScript object with this Duration's values.
  388. * @param opts - options for generating the object
  389. * @param {boolean} [opts.includeConfig=false] - include configuration attributes in the output
  390. * @example Duration.fromObject({ years: 1, days: 6, seconds: 2 }).toObject() //=> { years: 1, days: 6, seconds: 2 }
  391. * @return {Object}
  392. */
  393. toObject(opts = {}) {
  394. if (!this.isValid) return {};
  395. const base = Object.assign({}, this.values);
  396. if (opts.includeConfig) {
  397. base.conversionAccuracy = this.conversionAccuracy;
  398. base.numberingSystem = this.loc.numberingSystem;
  399. base.locale = this.loc.locale;
  400. }
  401. return base;
  402. }
  403. /**
  404. * Returns an ISO 8601-compliant string representation of this Duration.
  405. * @see https://en.wikipedia.org/wiki/ISO_8601#Durations
  406. * @example Duration.fromObject({ years: 3, seconds: 45 }).toISO() //=> 'P3YT45S'
  407. * @example Duration.fromObject({ months: 4, seconds: 45 }).toISO() //=> 'P4MT45S'
  408. * @example Duration.fromObject({ months: 5 }).toISO() //=> 'P5M'
  409. * @example Duration.fromObject({ minutes: 5 }).toISO() //=> 'PT5M'
  410. * @example Duration.fromObject({ milliseconds: 6 }).toISO() //=> 'PT0.006S'
  411. * @return {string}
  412. */
  413. toISO() {
  414. // we could use the formatter, but this is an easier way to get the minimum string
  415. if (!this.isValid) return null;
  416. let s = "P";
  417. if (this.years !== 0) s += this.years + "Y";
  418. if (this.months !== 0 || this.quarters !== 0) s += this.months + this.quarters * 3 + "M";
  419. if (this.weeks !== 0) s += this.weeks + "W";
  420. if (this.days !== 0) s += this.days + "D";
  421. if (this.hours !== 0 || this.minutes !== 0 || this.seconds !== 0 || this.milliseconds !== 0)
  422. s += "T";
  423. if (this.hours !== 0) s += this.hours + "H";
  424. if (this.minutes !== 0) s += this.minutes + "M";
  425. if (this.seconds !== 0 || this.milliseconds !== 0)
  426. // this will handle "floating point madness" by removing extra decimal places
  427. // https://stackoverflow.com/questions/588004/is-floating-point-math-broken
  428. s += roundTo(this.seconds + this.milliseconds / 1000, 3) + "S";
  429. if (s === "P") s += "T0S";
  430. return s;
  431. }
  432. /**
  433. * Returns an ISO 8601-compliant string representation of this Duration, formatted as a time of day.
  434. * Note that this will return null if the duration is invalid, negative, or equal to or greater than 24 hours.
  435. * @see https://en.wikipedia.org/wiki/ISO_8601#Times
  436. * @param {Object} opts - options
  437. * @param {boolean} [opts.suppressMilliseconds=false] - exclude milliseconds from the format if they're 0
  438. * @param {boolean} [opts.suppressSeconds=false] - exclude seconds from the format if they're 0
  439. * @param {boolean} [opts.includePrefix=false] - include the `T` prefix
  440. * @param {string} [opts.format='extended'] - choose between the basic and extended format
  441. * @example Duration.fromObject({ hours: 11 }).toISOTime() //=> '11:00:00.000'
  442. * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressMilliseconds: true }) //=> '11:00:00'
  443. * @example Duration.fromObject({ hours: 11 }).toISOTime({ suppressSeconds: true }) //=> '11:00'
  444. * @example Duration.fromObject({ hours: 11 }).toISOTime({ includePrefix: true }) //=> 'T11:00:00.000'
  445. * @example Duration.fromObject({ hours: 11 }).toISOTime({ format: 'basic' }) //=> '110000.000'
  446. * @return {string}
  447. */
  448. toISOTime(opts = {}) {
  449. if (!this.isValid) return null;
  450. const millis = this.toMillis();
  451. if (millis < 0 || millis >= 86400000) return null;
  452. opts = Object.assign(
  453. {
  454. suppressMilliseconds: false,
  455. suppressSeconds: false,
  456. includePrefix: false,
  457. format: "extended"
  458. },
  459. opts
  460. );
  461. const value = this.shiftTo("hours", "minutes", "seconds", "milliseconds");
  462. let fmt = opts.format === "basic" ? "hhmm" : "hh:mm";
  463. if (!opts.suppressSeconds || value.seconds !== 0 || value.milliseconds !== 0) {
  464. fmt += opts.format === "basic" ? "ss" : ":ss";
  465. if (!opts.suppressMilliseconds || value.milliseconds !== 0) {
  466. fmt += ".SSS";
  467. }
  468. }
  469. let str = value.toFormat(fmt);
  470. if (opts.includePrefix) {
  471. str = "T" + str;
  472. }
  473. return str;
  474. }
  475. /**
  476. * Returns an ISO 8601 representation of this Duration appropriate for use in JSON.
  477. * @return {string}
  478. */
  479. toJSON() {
  480. return this.toISO();
  481. }
  482. /**
  483. * Returns an ISO 8601 representation of this Duration appropriate for use in debugging.
  484. * @return {string}
  485. */
  486. toString() {
  487. return this.toISO();
  488. }
  489. /**
  490. * Returns an milliseconds value of this Duration.
  491. * @return {number}
  492. */
  493. toMillis() {
  494. return this.as("milliseconds");
  495. }
  496. /**
  497. * Returns an milliseconds value of this Duration. Alias of {@link toMillis}
  498. * @return {number}
  499. */
  500. valueOf() {
  501. return this.toMillis();
  502. }
  503. /**
  504. * Make this Duration longer by the specified amount. Return a newly-constructed Duration.
  505. * @param {Duration|Object|number} duration - The amount to add. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
  506. * @return {Duration}
  507. */
  508. plus(duration) {
  509. if (!this.isValid) return this;
  510. const dur = friendlyDuration(duration),
  511. result = {};
  512. for (const k of orderedUnits) {
  513. if (hasOwnProperty(dur.values, k) || hasOwnProperty(this.values, k)) {
  514. result[k] = dur.get(k) + this.get(k);
  515. }
  516. }
  517. return clone(this, { values: result }, true);
  518. }
  519. /**
  520. * Make this Duration shorter by the specified amount. Return a newly-constructed Duration.
  521. * @param {Duration|Object|number} duration - The amount to subtract. Either a Luxon Duration, a number of milliseconds, the object argument to Duration.fromObject()
  522. * @return {Duration}
  523. */
  524. minus(duration) {
  525. if (!this.isValid) return this;
  526. const dur = friendlyDuration(duration);
  527. return this.plus(dur.negate());
  528. }
  529. /**
  530. * Scale this Duration by the specified amount. Return a newly-constructed Duration.
  531. * @param {function} fn - The function to apply to each unit. Arity is 1 or 2: the value of the unit and, optionally, the unit name. Must return a number.
  532. * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit(x => x * 2) //=> { hours: 2, minutes: 60 }
  533. * @example Duration.fromObject({ hours: 1, minutes: 30 }).mapUnit((x, u) => u === "hour" ? x * 2 : x) //=> { hours: 2, minutes: 30 }
  534. * @return {Duration}
  535. */
  536. mapUnits(fn) {
  537. if (!this.isValid) return this;
  538. const result = {};
  539. for (const k of Object.keys(this.values)) {
  540. result[k] = asNumber(fn(this.values[k], k));
  541. }
  542. return clone(this, { values: result }, true);
  543. }
  544. /**
  545. * Get the value of unit.
  546. * @param {string} unit - a unit such as 'minute' or 'day'
  547. * @example Duration.fromObject({years: 2, days: 3}).get('years') //=> 2
  548. * @example Duration.fromObject({years: 2, days: 3}).get('months') //=> 0
  549. * @example Duration.fromObject({years: 2, days: 3}).get('days') //=> 3
  550. * @return {number}
  551. */
  552. get(unit) {
  553. return this[Duration.normalizeUnit(unit)];
  554. }
  555. /**
  556. * "Set" the values of specified units. Return a newly-constructed Duration.
  557. * @param {Object} values - a mapping of units to numbers
  558. * @example dur.set({ years: 2017 })
  559. * @example dur.set({ hours: 8, minutes: 30 })
  560. * @return {Duration}
  561. */
  562. set(values) {
  563. if (!this.isValid) return this;
  564. const mixed = Object.assign(this.values, normalizeObject(values, Duration.normalizeUnit, []));
  565. return clone(this, { values: mixed });
  566. }
  567. /**
  568. * "Set" the locale and/or numberingSystem. Returns a newly-constructed Duration.
  569. * @example dur.reconfigure({ locale: 'en-GB' })
  570. * @return {Duration}
  571. */
  572. reconfigure({ locale, numberingSystem, conversionAccuracy } = {}) {
  573. const loc = this.loc.clone({ locale, numberingSystem }),
  574. opts = { loc };
  575. if (conversionAccuracy) {
  576. opts.conversionAccuracy = conversionAccuracy;
  577. }
  578. return clone(this, opts);
  579. }
  580. /**
  581. * Return the length of the duration in the specified unit.
  582. * @param {string} unit - a unit such as 'minutes' or 'days'
  583. * @example Duration.fromObject({years: 1}).as('days') //=> 365
  584. * @example Duration.fromObject({years: 1}).as('months') //=> 12
  585. * @example Duration.fromObject({hours: 60}).as('days') //=> 2.5
  586. * @return {number}
  587. */
  588. as(unit) {
  589. return this.isValid ? this.shiftTo(unit).get(unit) : NaN;
  590. }
  591. /**
  592. * Reduce this Duration to its canonical representation in its current units.
  593. * @example Duration.fromObject({ years: 2, days: 5000 }).normalize().toObject() //=> { years: 15, days: 255 }
  594. * @example Duration.fromObject({ hours: 12, minutes: -45 }).normalize().toObject() //=> { hours: 11, minutes: 15 }
  595. * @return {Duration}
  596. */
  597. normalize() {
  598. if (!this.isValid) return this;
  599. const vals = this.toObject();
  600. normalizeValues(this.matrix, vals);
  601. return clone(this, { values: vals }, true);
  602. }
  603. /**
  604. * Convert this Duration into its representation in a different set of units.
  605. * @example Duration.fromObject({ hours: 1, seconds: 30 }).shiftTo('minutes', 'milliseconds').toObject() //=> { minutes: 60, milliseconds: 30000 }
  606. * @return {Duration}
  607. */
  608. shiftTo(...units) {
  609. if (!this.isValid) return this;
  610. if (units.length === 0) {
  611. return this;
  612. }
  613. units = units.map(u => Duration.normalizeUnit(u));
  614. const built = {},
  615. accumulated = {},
  616. vals = this.toObject();
  617. let lastUnit;
  618. for (const k of orderedUnits) {
  619. if (units.indexOf(k) >= 0) {
  620. lastUnit = k;
  621. let own = 0;
  622. // anything we haven't boiled down yet should get boiled to this unit
  623. for (const ak in accumulated) {
  624. own += this.matrix[ak][k] * accumulated[ak];
  625. accumulated[ak] = 0;
  626. }
  627. // plus anything that's already in this unit
  628. if (isNumber(vals[k])) {
  629. own += vals[k];
  630. }
  631. const i = Math.trunc(own);
  632. built[k] = i;
  633. accumulated[k] = own - i; // we'd like to absorb these fractions in another unit
  634. // plus anything further down the chain that should be rolled up in to this
  635. for (const down in vals) {
  636. if (orderedUnits.indexOf(down) > orderedUnits.indexOf(k)) {
  637. convert(this.matrix, vals, down, built, k);
  638. }
  639. }
  640. // otherwise, keep it in the wings to boil it later
  641. } else if (isNumber(vals[k])) {
  642. accumulated[k] = vals[k];
  643. }
  644. }
  645. // anything leftover becomes the decimal for the last unit
  646. // lastUnit must be defined since units is not empty
  647. for (const key in accumulated) {
  648. if (accumulated[key] !== 0) {
  649. built[lastUnit] +=
  650. key === lastUnit ? accumulated[key] : accumulated[key] / this.matrix[lastUnit][key];
  651. }
  652. }
  653. return clone(this, { values: built }, true).normalize();
  654. }
  655. /**
  656. * Return the negative of this Duration.
  657. * @example Duration.fromObject({ hours: 1, seconds: 30 }).negate().toObject() //=> { hours: -1, seconds: -30 }
  658. * @return {Duration}
  659. */
  660. negate() {
  661. if (!this.isValid) return this;
  662. const negated = {};
  663. for (const k of Object.keys(this.values)) {
  664. negated[k] = -this.values[k];
  665. }
  666. return clone(this, { values: negated }, true);
  667. }
  668. /**
  669. * Get the years.
  670. * @type {number}
  671. */
  672. get years() {
  673. return this.isValid ? this.values.years || 0 : NaN;
  674. }
  675. /**
  676. * Get the quarters.
  677. * @type {number}
  678. */
  679. get quarters() {
  680. return this.isValid ? this.values.quarters || 0 : NaN;
  681. }
  682. /**
  683. * Get the months.
  684. * @type {number}
  685. */
  686. get months() {
  687. return this.isValid ? this.values.months || 0 : NaN;
  688. }
  689. /**
  690. * Get the weeks
  691. * @type {number}
  692. */
  693. get weeks() {
  694. return this.isValid ? this.values.weeks || 0 : NaN;
  695. }
  696. /**
  697. * Get the days.
  698. * @type {number}
  699. */
  700. get days() {
  701. return this.isValid ? this.values.days || 0 : NaN;
  702. }
  703. /**
  704. * Get the hours.
  705. * @type {number}
  706. */
  707. get hours() {
  708. return this.isValid ? this.values.hours || 0 : NaN;
  709. }
  710. /**
  711. * Get the minutes.
  712. * @type {number}
  713. */
  714. get minutes() {
  715. return this.isValid ? this.values.minutes || 0 : NaN;
  716. }
  717. /**
  718. * Get the seconds.
  719. * @return {number}
  720. */
  721. get seconds() {
  722. return this.isValid ? this.values.seconds || 0 : NaN;
  723. }
  724. /**
  725. * Get the milliseconds.
  726. * @return {number}
  727. */
  728. get milliseconds() {
  729. return this.isValid ? this.values.milliseconds || 0 : NaN;
  730. }
  731. /**
  732. * Returns whether the Duration is invalid. Invalid durations are returned by diff operations
  733. * on invalid DateTimes or Intervals.
  734. * @return {boolean}
  735. */
  736. get isValid() {
  737. return this.invalid === null;
  738. }
  739. /**
  740. * Returns an error code if this Duration became invalid, or null if the Duration is valid
  741. * @return {string}
  742. */
  743. get invalidReason() {
  744. return this.invalid ? this.invalid.reason : null;
  745. }
  746. /**
  747. * Returns an explanation of why this Duration became invalid, or null if the Duration is valid
  748. * @type {string}
  749. */
  750. get invalidExplanation() {
  751. return this.invalid ? this.invalid.explanation : null;
  752. }
  753. /**
  754. * Equality check
  755. * Two Durations are equal iff they have the same units and the same values for each unit.
  756. * @param {Duration} other
  757. * @return {boolean}
  758. */
  759. equals(other) {
  760. if (!this.isValid || !other.isValid) {
  761. return false;
  762. }
  763. if (!this.loc.equals(other.loc)) {
  764. return false;
  765. }
  766. function eq(v1, v2) {
  767. // Consider 0 and undefined as equal
  768. if (v1 === undefined || v1 === 0) return v2 === undefined || v2 === 0;
  769. return v1 === v2;
  770. }
  771. for (const u of orderedUnits) {
  772. if (!eq(this.values[u], other.values[u])) {
  773. return false;
  774. }
  775. }
  776. return true;
  777. }
  778. }
  779. /**
  780. * @private
  781. */
  782. export function friendlyDuration(durationish) {
  783. if (isNumber(durationish)) {
  784. return Duration.fromMillis(durationish);
  785. } else if (Duration.isDuration(durationish)) {
  786. return durationish;
  787. } else if (typeof durationish === "object") {
  788. return Duration.fromObject(durationish);
  789. } else {
  790. throw new InvalidArgumentError(
  791. `Unknown duration argument ${durationish} of type ${typeof durationish}`
  792. );
  793. }
  794. }