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

adding methods for yearMonth and MonthDay #44

Merged
merged 17 commits into from
Jul 24, 2024
Merged

adding methods for yearMonth and MonthDay #44

merged 17 commits into from
Jul 24, 2024

Conversation

jasonwilliams
Copy link
Member

@jasonwilliams jasonwilliams commented May 17, 2024

Adds methods to yearMonth and monthDay

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 good to me whenever you want to take it off draft.

The one thing missing is maybe the default implementation and some unit tests, but I don't think those are necessarily needed to merge 😄

@jasonwilliams jasonwilliams marked this pull request as ready for review May 24, 2024 16:08
@jasonwilliams
Copy link
Member Author

@nekevss let me know your thoughts

I've been trying to add add() but it seems it's iso is using the date version of add_date_duration, but it seems like i need an implementation more like dateTime's:

temporal/src/iso.rs

Lines 125 to 164 in dd51ebb

/// Specification equivalent to 5.5.9 `AddDateTime`.
pub(crate) fn add_date_duration<C: CalendarProtocol>(
&self,
calendar: &CalendarSlot<C>,
date_duration: &DateDuration,
norm: NormalizedTimeDuration,
overflow: Option<ArithmeticOverflow>,
context: &mut C::Context,
) -> TemporalResult<Self> {
// 1. Assert: IsValidISODate(year, month, day) is true.
// 2. Assert: ISODateTimeWithinLimits(year, month, day, hour, minute, second, millisecond, microsecond, nanosecond) is true.
// 3. Let timeResult be AddTime(hour, minute, second, millisecond, microsecond, nanosecond, norm).
let t_result = self.time.add(norm);
// 4. Let datePart be ! CreateTemporalDate(year, month, day, calendarRec.[[Receiver]]).
let date = Date::new_unchecked(self.date, calendar.clone());
// 5. Let dateDuration be ? CreateTemporalDuration(years, months, weeks, days + timeResult.[[Days]], 0, 0, 0, 0, 0, 0).
let duration = Duration::new(
date_duration.years,
date_duration.months,
date_duration.weeks,
date_duration.days + f64::from(t_result.0),
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
)?;
// 6. Let addedDate be ? AddDate(calendarRec, datePart, dateDuration, options).
let added_date = date.add_date(&duration, overflow, context)?;
// 7. Return ISO Date-Time Record { [[Year]]: addedDate.[[ISOYear]], [[Month]]: addedDate.[[ISOMonth]],
// [[Day]]: addedDate.[[ISODay]], [[Hour]]: timeResult.[[Hour]], [[Minute]]: timeResult.[[Minute]],
// [[Second]]: timeResult.[[Second]], [[Millisecond]]: timeResult.[[Millisecond]],
// [[Microsecond]]: timeResult.[[Microsecond]], [[Nanosecond]]: timeResult.[[Nanosecond]] }.
Ok(Self::new_unchecked(added_date.iso, t_result.1))
}

The former doesn't use the calendar at all by the looks of it

secondly why does the leap_year not return a bool? Is that wasteful?

temporal/src/utils.rs

Lines 230 to 233 in dd51ebb

/// Returns either 1 (true) or 0 (false)
pub(crate) fn mathematical_in_leap_year(t: f64) -> i32 {
mathematical_days_in_year(epoch_time_to_epoch_year(t)) - 365
}

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.

Here's some feedback regarding the most recent changes 😄

I don't think YearMonth or MonthDay call add or subtract directly. It's mostly done with Date in the middle

src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/fields.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
@jasonwilliams jasonwilliams changed the title adding month_code for yearMonth adding methods for yearMonth and MonthDay Jun 25, 2024
src/components/duration.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
- set_month was setting self.year
- Duration adds an enum for add/subtract
- add contextual_add_or_subtract_duration
- Create add/subtract duration public methods which call into contextual_add_or_subtract_duration
- Fields within TemporalFields need to be visible within the crate to achieve the From<> improvement.
@jasonwilliams jasonwilliams marked this pull request as ready for review July 22, 2024 23:05
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.

There's one last thing that I missed the last couple times around. But outside of that this is overall looking fantastic!

src/components/month_day.rs Outdated Show resolved Hide resolved
src/components/year_month.rs Show resolved Hide resolved
src/components/year_month.rs Outdated Show resolved Hide resolved
- rename iso day/month
- use getCalendar in leap year
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!

@jasonwilliams jasonwilliams merged commit 9a36aa1 into main Jul 24, 2024
5 checks passed
@jedel1043
Copy link
Member

jedel1043 commented Jul 24, 2024

Oh, I had an unsent pending review 😅 I'll push the comment and I think we can address it on a separate PR

Comment on lines +20 to +21
// Subset of `TemporalFields` representing just the `YearMonthFields`
pub struct YearMonthFields(pub i32, pub u8);
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to expose the fields as public?
Also, I think this should be a proper struct with named fields for clarity

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.

3 participants