Skip to content
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

refrenceDate support for fromObject #1632

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/datetime.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
angelaki marked this conversation as resolved.
Show resolved Hide resolved
* @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' })
Expand All @@ -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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a member of the Luxon team, but here's my thoughts:

  • maybe tsNow would be better named tsRef
  • I don't think opts.refrenceDate should be used if it's undefined

offsetProvis = !isUndefined(opts.specificOffset)
? opts.specificOffset
: zoneToUse.offset(tsNow),
Expand Down
6 changes: 6 additions & 0 deletions test/datetime/create.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to also add tests for:

  • Calling fromObject with actual data and check that the data is "merged in".
  • Checking that referenceDate is only used for units higher than the highest unit in the source object (i.e. fromObject({ day: 10 }) reads only year and month from the reference date.
  • Some tests for methods that indirectly call fromObject (like fromISO) with referenceDate.

test("private language subtags don't break unicode subtags", () => {
const res = DateTime.fromObject(
{},
Expand Down