Skip to content

Implementation of ZonedDateTime.prototype.with #267

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

Merged
merged 7 commits into from
May 5, 2025

Conversation

lockels
Copy link
Contributor

@lockels lockels commented Apr 24, 2025

Fixes #260

@lockels lockels marked this pull request as draft April 24, 2025 18:06
@lockels
Copy link
Contributor Author

lockels commented Apr 24, 2025

Still unfinished, but this mostly works i think.


#[doc(hidden)]
#[macro_export]
macro_rules! impl_with_fallback_method_zoned {
Copy link
Member

Choose a reason for hiding this comment

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

suggestion: change this to a normal method over a macro.

Since this is a one off and can't really take advantage of the other macro, it would probably be better for this to be a direct method implementation on PartialDate so that it's easy to read and reason about in the future.

@lockels
Copy link
Contributor Author

lockels commented Apr 29, 2025

31/38 out of the current tests pass with my last commit. I'm unsure why 4 of the tests are saying this

Uncaught Error: Not yet implemented

This one for example

overflow-options.js

const zdt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789).toZonedDateTime("UTC");

const overflow = "constrain";
TemporalHelpers.assertZonedDateTimesEqual(
    zdt.with({ month: 29 }, { overflow }),
    Temporal.ZonedDateTime.from("1976-12-18T15:23:30.123456789+00:00[UTC]"));
TemporalHelpers.assertZonedDateTimesEqual(
    zdt.with({ day: 31 }, { overflow }),
    Temporal.ZonedDateTime.from("1976-11-30T15:23:30.123456789+00:00[UTC]"));
TemporalHelpers.assertZonedDateTimesEqual(
    zdt.with({ hour: 29 }, { overflow }),
    Temporal.ZonedDateTime.from("1976-11-18T23:23:30.123456789+00:00[UTC]"));
TemporalHelpers.assertZonedDateTimesEqual(
    zdt.with({ nanosecond: 9000 }, { overflow }),
    Temporal.ZonedDateTime.from("1976-11-18T15:23:30.123456999+00:00[UTC]"));

and this one

overflow-reject-throws.js

const zdt = new Temporal.PlainDateTime(1976, 11, 18, 15, 23, 30, 123, 456, 789).toZonedDateTime("UTC");

const overflow = "reject";
assert.throws(RangeError, () => zdt.with({ month: 29 }, { overflow }));
assert.throws(RangeError, () => zdt.with({ day: 31 }, { overflow }));
assert.throws(RangeError, () => zdt.with({ hour: 29 }, { overflow }));
assert.throws(RangeError, () => zdt.with({ nanosecond: 9000 }, { overflow }));

@lockels
Copy link
Contributor Author

lockels commented Apr 29, 2025

I haven't inspected it yet, but some overflow option tests are not doing what they should, might be an erorr in the implementation of 5.5.5 InterpretTemporalDateTimeFields

Overflow reject option here for example doesn't give the error variant.

        let provider = &FsTzdbProvider::default();
        let zdt =
            ZonedDateTime::try_new(217178610123456789, Calendar::default(), TimeZone::default())
                .unwrap();

        let overflow = ArithmeticOverflow::Reject;

        let result_2 = zdt.with(
            PartialZonedDateTime {
                date: PartialDate {
                    day: Some(31),
                    ..Default::default()
                },
                time: PartialTime::default(),
                offset: None,
                timezone: None,
            },
            None,
            None,
            Some(overflow),
            provider,
        );

        assert!(result_2.is_err()); // Fails

@nekevss
Copy link
Member

nekevss commented Apr 29, 2025

overflow-options.js

These are easy to answer. It looks like the implementation hasn't been "turned on" in Boa. That's partially my bad. I had a PR open yesterday to uncomment one of these methods, and I really should have searched for others. If you uncomment the lines in Boa, then it should hopefully pass 😄

The last case may genuinely be a bug that could be addressed. I haven't looked into it too much yet. If you want to dig into it, feel free to.

@lockels lockels force-pushed the zoneddatetime_with branch from c4c0450 to 969b124 Compare May 2, 2025 15:17
@lockels lockels marked this pull request as ready for review May 2, 2025 15:17
@lockels
Copy link
Contributor Author

lockels commented May 2, 2025

I haven't inspected it yet, but some overflow option tests are not doing what they should, might be an error in the implementation of 5.5.5 InterpretTemporalDateTimeFields

Managed to fix this bug, was a simple error in my own code. I was previously always passing Constriain to date_from_partial rather than the overflow parameter 😅

@lockels
Copy link
Contributor Author

lockels commented May 2, 2025

overflow-options.js

These are easy to answer. It looks like the implementation hasn't been "turned on" in Boa. That's partially my bad. I had a PR open yesterday to uncomment one of these methods, and I really should have searched for others. If you uncomment the lines in Boa, then it should hopefully pass 😄

The last case may genuinely be a bug that could be addressed. I haven't looked into it too much yet. If you want to dig into it, feel free to.

Thanks a lot, i honestly didn't think of that being the issue at all! But that seemed to fix it

Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

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

I have one thought / question, it's not necessarily blocking (although I guess it is softly blocking due to curiosity on my part), but I'd be curious to hear your thoughts prior to approval / merge.

let offset_option = offset_option.unwrap_or(OffsetDisambiguation::Reject);

// 23. Let dateTimeResult be ? InterpretTemporalDateTimeFields(calendar, fields, overflow).
let result_date = self.calendar.date_from_partial(
Copy link
Member

Choose a reason for hiding this comment

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

thought (non-blocking): this strikes me as a bit curious

Now that I'm looking at this in a bit more detail, I'm noticing that we have with_fallback_zoneddatetime, but then we call get_iso_datetime_for immediately after. Did you consider using the return value of get_iso_datetime_for at all to calculate the fallback at all?

Copy link
Member

@nekevss nekevss left a comment

Choose a reason for hiding this comment

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

Looks great! Thanks again for working on this! 😄

@nekevss nekevss merged commit 5d55f0e into boa-dev:main May 5, 2025
8 checks passed
@lockels lockels deleted the zoneddatetime_with branch May 7, 2025 12:15
nekevss pushed a commit that referenced this pull request May 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement with for ZonedDateTime
2 participants