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

Implement toZonedDateTime in PlainDate #192

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

JohannesHelleve
Copy link

@JohannesHelleve JohannesHelleve commented Feb 12, 2025

Related to #148

First time contributer, not sure if is correct. Tought this might be a good place to get some feedback while showcasing the code.

Struggling to choose type for the item and how to check if it is an object.

Thanks for the feedback!

Link to method

In kahoots with @brageh01

@JohannesHelleve JohannesHelleve changed the title Impl to zoned date time Implement toZonedDateTime in PlainDate Feb 12, 2025
@jedel1043
Copy link
Member

Thank you for contributing!

Yeah, the most difficult part about this is that we can't do a 1-to-1 implementation of the spec because temporal_rs fits between JS engines and the ICU APIs used for timezone and calendar calculations, so certain things like object handling and property fetching are delegated to the engines to do.

As a good rule of thumb, every time you see something like "if o is an object" or "Get(o, 'property')", assume those calculations are made by the engine, and try to receive a struct or an enum as a parameter instead.

@jedel1043
Copy link
Member

In this case, you should just take an Option<PlainTime> and a Timezone as parameters.

@nekevss
Copy link
Member

nekevss commented Feb 12, 2025

Agree with the comments above.

For further reference to a similar method implementation wise if you'd like to see how this was handled elsewhere in the library, feel free to check out ZonedDateTime.prototype.withPlainTime in temporal_rs and in Boa.

@nekevss nekevss requested review from jedel1043 and nekevss February 13, 2025 22:02
@JohannesHelleve
Copy link
Author

Updated the code now. Think im closer to a somewhat solution now. Would appricate some feedback! @jedel1043 @nekevss . Thanks!

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.

This is a good start so far! I've left a few comments on points that could be improved.

My general recommendation is try and follow steps by coupling the code together with the commented steps (see duration for an example). I believe most of the internals used in these steps do exist in the codebase, so using the specification steps as a guide can be incredibly useful.

This method is a bit tricky as steps 1-4 require engine specific details, but we can guarantee they are completed by the types of the function args, so the real starting point, as already identified, is step 5.

@jedel1043 jedel1043 added C-enhancement New feature or request C-api Changes related to the public API labels Feb 24, 2025
@JohannesHelleve
Copy link
Author

JohannesHelleve commented Feb 26, 2025

Updated now!

Little bit hard to test since the api needs to work correctly. I think?

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.

Looking closer, I left a handful of things I noticed when looking.

When working with rust, it's useful to use the compiler and cargo to help you.

I'd recommend running:

cargo fmt -all, cargo clippy --no-default-features, and cargo clippy --all-features --all-targets

The compiler errors and lints can help massively when working on some code. Also, we use those commands in our CI, so it is needed to pass CI too.

};

// 7. Return ! CreateTemporalZonedDateTime(epochNs, timeZone, temporalDate.[[Calendar]]).
Self::try_new(epoch_ns.0, self.calendar.clone(), tz)
Copy link
Member

Choose a reason for hiding this comment

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

issue: missing bracket to close function

impl Date {

/// Converts a `Date` to a `ZonedDateTime` in the UTC time zone.
pub fn to_zoned_date_time (self, time_zone: &str) -> TemporalResult<crate::ZonedDateTime> {
Copy link
Member

Choose a reason for hiding this comment

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

issue: function signature here should be the same as to_zoned_date_time_with_provider.

Also, it looks like there is a space after the function name. Although, I'm not entirely sure whether that may affect anything.

let provider = TZ_PROVIDER
.lock()
.map_err(|_| TemporalError::general("Unable to acquire lock"))?;
self.to_zoned_date_time_with_provider(time_zone, &*provider)
Copy link
Member

Choose a reason for hiding this comment

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

issue: to_zoned_date_time_with_provider is called with the wrong args.

self.to_zoned_date_time_with_provider(time_zone, plain_time, &*provider)

// c. If ISODateTimeWithinLimits(isoDateTime) is false, throw a RangeError exception.
// d. Let epochNs be ? GetEpochNanosecondsFor(timeZone, isoDateTime, compatible).
let epoch_ns = if let Some(time) = plain_time {
let temporal_time = Time::new_unchecked(time)?;
Copy link
Member

Choose a reason for hiding this comment

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

issue: I'm not entirely sure what the time is referring to here. But it should be unneeded.

You should be able to access PlainTime's iso field by using time.iso in line 685.

@@ -0,0 +1,12 @@
use crate::{builtins::TZ_PROVIDER, TemporalError, TemporalResult, Date};

impl Date {
Copy link
Member

Choose a reason for hiding this comment

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

issue: wrong struct name on the impl block -> PlainDate

@JohannesHelleve
Copy link
Author

JohannesHelleve commented Feb 27, 2025

Thanks that helped a lot!!

Now stuck on this thing:
image
Trying to debug, but i can't seem to either use dbg! or println! without importing crates. Others managed without importing.

@nekevss
Copy link
Member

nekevss commented Feb 27, 2025

dbg! or println! without importing crates

Ah, currently temporal_rs is setup for #![no_std], which can be seen here, so std functions and methods are feature flagged.

Due the current default sys feature flag, you should be able to use the full path std::println! vs. println!. The other option is to comment out the #![no_std] in lib.rs when you are debugging, but be careful about not committing the commented out line to the git history (although, it's happened to us all)

@JohannesHelleve
Copy link
Author

The code compilies now! But all the test fails :(.

My expirence with rust is slim to zero, so i was wondering what the best practice is to debug the code. For now i've tried to insert code in the functions, but it don't get printed in the terminal. I want to see the value of a variable at a certain point.

I've heard with some others and they decided to create their own test. Is this the optimal way?

Thanks again!

@jedel1043
Copy link
Member

My expirence with rust is slim to zero, so i was wondering what the best practice is to debug the code. For now i've tried to insert code in the functions, but it don't get printed in the terminal. I want to see the value of a variable at a certain point.

If you use rust-analyzer on VSCode, you can pretty easily hit the "debug" label on any test and it should put you on a debugger environment.
If you use a Jetbrains' IDE (possibly RustRover IIRC), it should have equivalent functionality.
Any other IDE should support lldb or an equivalent to debug a binary program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-api Changes related to the public API C-enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants