From 886c8014b34b2e3f3373d3200ae370ebfe3d862f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Spr=C3=B6=C3=9Fer?= Date: Fri, 17 May 2024 11:09:49 +0200 Subject: [PATCH 1/5] `refrenceDate` support for `fromObject` --- src/datetime.js | 6 +++++- test/datetime/create.test.js | 6 ++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/datetime.js b/src/datetime.js index 09a389e7..979f1bdf 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -745,8 +745,10 @@ export default class DateTime { * @param {string} [opts.locale='system\'s locale'] - a locale to set on the resulting DateTime instance * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.refrenceDate - the reference date to taken for missing parts * @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25' * @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01' + * @example DateTime.fromObject({ year: 1982 }, { refrenceDate: { day: 10 } }).toISODate() //=> '1982-01-10' * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06 * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }), * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' }) @@ -766,7 +768,9 @@ export default class DateTime { const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks); const { minDaysInFirstWeek, startOfWeek } = usesLocalWeekValues(normalized, loc); - const tsNow = Settings.now(), + const tsNow = isUndefined(opts.refrenceDate) + ? friendlyDateTime(opts.refrenceDate).toUnixInteger() + : opts.refrenceDate, offsetProvis = !isUndefined(opts.specificOffset) ? opts.specificOffset : zoneToUse.offset(tsNow), diff --git a/test/datetime/create.test.js b/test/datetime/create.test.js index 67613a61..fc3f037c 100644 --- a/test/datetime/create.test.js +++ b/test/datetime/create.test.js @@ -883,6 +883,12 @@ test("DateTime.fromObject takes a undefined to mean {}", () => { expect(res.year).toBe(new Date().getFullYear()); }); +test("DateTime.fromObject respects `refrenceDate`", () => { + const res = DateTime.fromObject(undefined, { refrenceDate: { day: 10 } }); + expect(res.year).toBe(new Date().getFullYear()); + expect(res.day).toBe(10); +}); + test("private language subtags don't break unicode subtags", () => { const res = DateTime.fromObject( {}, From c459f393990c9bac4c2ee073437462de793944cc Mon Sep 17 00:00:00 2001 From: Tristan <38611461+angelaki@users.noreply.github.com> Date: Fri, 17 May 2024 21:05:32 +0200 Subject: [PATCH 2/5] Update src/datetime.js Co-authored-by: Dasa Paddock --- src/datetime.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/datetime.js b/src/datetime.js index 979f1bdf..c1e02433 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -745,7 +745,7 @@ export default class DateTime { * @param {string} [opts.locale='system\'s locale'] - a locale to set on the resulting DateTime instance * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance - * @param {DateTime|Date|Object} opts.refrenceDate - the reference date to taken for missing parts + * @param {DateTime|Date|Object} opts.refrenceDate - the reference date to take for missing parts * @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25' * @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01' * @example DateTime.fromObject({ year: 1982 }, { refrenceDate: { day: 10 } }).toISODate() //=> '1982-01-10' From b655ae7f0d3720429adff0a46b2f70ef5b3185d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Spr=C3=B6=C3=9Fer?= Date: Tue, 4 Jun 2024 12:39:48 +0200 Subject: [PATCH 3/5] Fixes typo `refrenceDate` --> `referenceDate` --- src/datetime.js | 10 +++++----- test/datetime/create.test.js | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/datetime.js b/src/datetime.js index c1e02433..d900add5 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -745,10 +745,10 @@ export default class DateTime { * @param {string} [opts.locale='system\'s locale'] - a locale to set on the resulting DateTime instance * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance - * @param {DateTime|Date|Object} opts.refrenceDate - the reference date to take for missing parts + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @example DateTime.fromObject({ year: 1982, month: 5, day: 25}).toISODate() //=> '1982-05-25' * @example DateTime.fromObject({ year: 1982 }).toISODate() //=> '1982-01-01' - * @example DateTime.fromObject({ year: 1982 }, { refrenceDate: { day: 10 } }).toISODate() //=> '1982-01-10' + * @example DateTime.fromObject({ year: 1982 }, { referenceDate: { day: 10 } }).toISODate() //=> '1982-01-10' * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }) //~> today at 10:26:06 * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'utc' }), * @example DateTime.fromObject({ hour: 10, minute: 26, second: 6 }, { zone: 'local' }) @@ -768,9 +768,9 @@ export default class DateTime { const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks); const { minDaysInFirstWeek, startOfWeek } = usesLocalWeekValues(normalized, loc); - const tsNow = isUndefined(opts.refrenceDate) - ? friendlyDateTime(opts.refrenceDate).toUnixInteger() - : opts.refrenceDate, + const tsNow = isUndefined(opts.referenceDate) + ? friendlyDateTime(opts.referenceDate).toUnixInteger() + : opts.referenceDate, offsetProvis = !isUndefined(opts.specificOffset) ? opts.specificOffset : zoneToUse.offset(tsNow), diff --git a/test/datetime/create.test.js b/test/datetime/create.test.js index fc3f037c..ffe72ed9 100644 --- a/test/datetime/create.test.js +++ b/test/datetime/create.test.js @@ -883,8 +883,8 @@ test("DateTime.fromObject takes a undefined to mean {}", () => { expect(res.year).toBe(new Date().getFullYear()); }); -test("DateTime.fromObject respects `refrenceDate`", () => { - const res = DateTime.fromObject(undefined, { refrenceDate: { day: 10 } }); +test("DateTime.fromObject respects `referenceDate`", () => { + const res = DateTime.fromObject(undefined, { referenceDate: { day: 10 } }); expect(res.year).toBe(new Date().getFullYear()); expect(res.day).toBe(10); }); From 8e568e68c5131900d0ae432f53f8fed0fd9002b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Spr=C3=B6=C3=9Fer?= Date: Tue, 4 Jun 2024 12:45:06 +0200 Subject: [PATCH 4/5] Rename tsNow --> tsRef Fixed isUndefined usage --- src/datetime.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/datetime.js b/src/datetime.js index d900add5..3873cab3 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -387,8 +387,8 @@ function normalizeUnitWithLocalWeeks(unit) { // single timestamp for all zones to make things a bit more predictable. // // This is safe for quickDT (used by local() and utc()) because we don't fill in -// higher-order units from tsNow (as we do in fromObject, this requires that -// offset is calculated from tsNow). +// higher-order units from tsRef (as we do in fromObject, this requires that +// offset is calculated from tsRef). function guessOffsetForZone(zone) { if (!zoneOffsetGuessCache[zone]) { if (zoneOffsetTs === undefined) { @@ -768,12 +768,12 @@ export default class DateTime { const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks); const { minDaysInFirstWeek, startOfWeek } = usesLocalWeekValues(normalized, loc); - const tsNow = isUndefined(opts.referenceDate) - ? friendlyDateTime(opts.referenceDate).toUnixInteger() - : opts.referenceDate, + const tsRef = isUndefined(opts.referenceDate) + ? Settings.now() + : friendlyDateTime(opts.referenceDate).toUnixInteger(), offsetProvis = !isUndefined(opts.specificOffset) ? opts.specificOffset - : zoneToUse.offset(tsNow), + : zoneToUse.offset(tsRef), containsOrdinal = !isUndefined(normalized.ordinal), containsGregorYear = !isUndefined(normalized.year), containsGregorMD = !isUndefined(normalized.month) || !isUndefined(normalized.day), @@ -801,7 +801,7 @@ export default class DateTime { // configure ourselves to deal with gregorian dates or week stuff let units, defaultValues, - objNow = tsToObj(tsNow, offsetProvis); + objNow = tsToObj(tsRef, offsetProvis); if (useWeekData) { units = orderedWeekUnits; defaultValues = defaultWeekUnitValues; From 448be0e3663baf2906641232cc13e7562289c333 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tristan=20Spr=C3=B6=C3=9Fer?= Date: Wed, 5 Jun 2024 11:17:16 +0200 Subject: [PATCH 5/5] opts.referenceDate description on all methods relying on `fromObject` --- src/datetime.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/datetime.js b/src/datetime.js index 3873cab3..92972dbe 100644 --- a/src/datetime.js +++ b/src/datetime.js @@ -878,6 +878,7 @@ export default class DateTime { * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance * @param {string} [opts.outputCalendar] - the output calendar to set on the resulting DateTime instance * @param {string} [opts.numberingSystem] - the numbering system to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @example DateTime.fromISO('2016-05-25T09:08:34.123') * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00') * @example DateTime.fromISO('2016-05-25T09:08:34.123+06:00', {setZone: true}) @@ -899,6 +900,7 @@ export default class DateTime { * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @example DateTime.fromRFC2822('25 Nov 2016 13:23:12 GMT') * @example DateTime.fromRFC2822('Fri, 25 Nov 2016 13:23:12 +0600') * @example DateTime.fromRFC2822('25 Nov 2016 13:23 Z') @@ -919,6 +921,7 @@ export default class DateTime { * @param {string} [opts.locale='system's locale'] - a locale to set on the resulting DateTime instance * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance * @param {string} opts.numberingSystem - the numbering system to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @example DateTime.fromHTTP('Sun, 06 Nov 1994 08:49:37 GMT') * @example DateTime.fromHTTP('Sunday, 06-Nov-94 08:49:37 GMT') * @example DateTime.fromHTTP('Sun Nov 6 08:49:37 1994') @@ -940,6 +943,7 @@ export default class DateTime { * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @return {DateTime} */ static fromFormat(text, fmt, opts = {}) { @@ -978,6 +982,7 @@ export default class DateTime { * @param {string} [opts.locale='en-US'] - a locale string to use when parsing. Will also set the DateTime to this locale * @param {string} opts.numberingSystem - the numbering system to use when parsing. Will also set the resulting DateTime to this numbering system * @param {string} opts.outputCalendar - the output calendar to set on the resulting DateTime instance + * @param {DateTime|Date|Object} opts.referenceDate - the reference date to take for missing parts * @example DateTime.fromSQL('2017-05-15') * @example DateTime.fromSQL('2017-05-15 09:12:34') * @example DateTime.fromSQL('2017-05-15 09:12:34.342')