-
Notifications
You must be signed in to change notification settings - Fork 760
refrenceDate
support for fromObject
#1632
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
886c801
c459f39
b655ae7
8e568e6
448be0e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
|
@@ -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.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 }, { referenceDate: { day: 10 } }).toISODate() //=> '1982-01-10' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example is actually wrong. The referenceDate is only used for units above the highest unit in the input. If There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh sorry, I just expected the old date-fns even uses proximity to guess the right input: https://date-fns.org/v3.6.0/docs/parse There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If that is added, it definitely needs to be opt in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tbh, I have no use-case for it. Just thinking, in an Englisch format, if only this first digits (month) is set, maybe someone wants to have a different day than the first (and not only a different year than todays?). But again, no hard feeling here! Can just stay as it is! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's keep it as it is for now then! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The example here still contradicts the actual behavior and implementation. |
||
* @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,10 +768,12 @@ export default class DateTime { | |
const normalized = normalizeObject(obj, normalizeUnitWithLocalWeeks); | ||
const { minDaysInFirstWeek, startOfWeek } = usesLocalWeekValues(normalized, loc); | ||
|
||
const tsNow = Settings.now(), | ||
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), | ||
|
@@ -797,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; | ||
|
@@ -874,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}) | ||
|
@@ -895,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') | ||
|
@@ -915,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') | ||
|
@@ -936,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 = {}) { | ||
|
@@ -974,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') | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -883,6 +883,12 @@ test("DateTime.fromObject takes a undefined to mean {}", () => { | |
expect(res.year).toBe(new Date().getFullYear()); | ||
}); | ||
|
||
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); | ||
}); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be good to also add tests for:
|
||
test("private language subtags don't break unicode subtags", () => { | ||
const res = DateTime.fromObject( | ||
{}, | ||
|
Uh oh!
There was an error while loading. Please reload this page.