Skip to content

Add with_* methods to Date and DateTime #84

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 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions src/components/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ use icu_calendar::{
};
use tinystr::TinyAsciiStr;

use super::ZonedDateTime;

/// The ECMAScript defined protocol methods
pub const CALENDAR_PROTOCOL_METHODS: [&str; 21] = [
"dateAdd",
Expand Down Expand Up @@ -618,6 +620,36 @@ impl Calendar {
}
}

impl From<Date> for Calendar {
fn from(value: Date) -> Self {
value.calendar().clone()
}
}

impl From<DateTime> for Calendar {
fn from(value: DateTime) -> Self {
value.calendar().clone()
}
}

impl From<ZonedDateTime> for Calendar {
fn from(value: ZonedDateTime) -> Self {
value.calendar().clone()
}
}

impl From<MonthDay> for Calendar {
fn from(value: MonthDay) -> Self {
value.calendar().clone()
}
}

impl From<YearMonth> for Calendar {
fn from(value: YearMonth) -> Self {
value.calendar().clone()
}
}

#[cfg(all(test, feature = "compiled_data"))]
mod tests {
use super::*;
Expand Down
16 changes: 9 additions & 7 deletions src/components/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,15 @@ impl Date {
Ok(Self::new_unchecked(iso, calendar))
}

#[must_use]
/// Creates a `Date` from a `DateTime`.
pub fn from_datetime(dt: &DateTime) -> Self {
Self {
iso: dt.iso_date(),
calendar: dt.calendar().clone(),
}
/// Creates a new `Date` from the current `Date` and the provided calendar.
pub fn with_calendar(&self, calendar: Calendar) -> TemporalResult<Self> {
Self::new(
self.iso_year(),
self.iso_month().into(),
self.iso_day().into(),
calendar,
ArithmeticOverflow::Reject,
)
}

#[inline]
Expand Down
34 changes: 33 additions & 1 deletion src/components/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tinystr::TinyAsciiStr;
use super::{
calendar::{CalendarDateLike, GetTemporalCalendar},
duration::normalized::{NormalizedTimeDuration, RelativeRoundResult},
Date, Duration,
Date, Duration, Time,
};

/// The native Rust implementation of `Temporal.PlainDateTime`
Expand Down Expand Up @@ -191,6 +191,38 @@ impl DateTime {
))
}

/// Creates a new `DateTime` from the current `DateTime` and the provided `Time`.
pub fn with_time(&self, time: Time) -> TemporalResult<Self> {
Self::new(
self.iso_year(),
self.iso_month().into(),
self.iso_day().into(),
time.hour().into(),
time.minute().into(),
time.second().into(),
time.millisecond().into(),
time.microsecond().into(),
time.nanosecond().into(),
self.calendar.clone(),
)
}

/// Creates a new `DateTime` from the current `DateTime` and a provided `Calendar`.
pub fn with_calendar(&self, calendar: Calendar) -> TemporalResult<Self> {
Self::new(
self.iso_year(),
self.iso_month().into(),
self.iso_day().into(),
self.hour().into(),
self.minute().into(),
self.second().into(),
self.millisecond().into(),
self.microsecond().into(),
self.nanosecond().into(),
calendar,
)
}

/// Validates whether ISO date slots are within iso limits at noon.
#[inline]
pub fn validate<T: IsoDateSlots>(target: &T) -> bool {
Expand Down