Skip to content

Add intro documentation to ZonedDateTime and PlainDateTime #253

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 2 commits into from
Mar 31, 2025
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
13 changes: 12 additions & 1 deletion src/builtins/core/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,18 @@ impl PartialDateTime {
}
}

/// The native Rust implementation of `Temporal.PlainDateTime`
// TODO: Example doctest
/// The native Rust implementation of a Temporal `PlainDateTime`.
///
/// The `PlainDateTime` represents a date and time without a
/// time zone. The fundemental represenation of a `PlainDateTime`
/// is it's internal ISO date and time fields and a calendar.
///
/// ## Reference
///
/// For more information, see the [MDN documentation][mdn-datetime].
///
/// [mdn-datetime]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime
#[non_exhaustive]
#[derive(Debug, Default, Clone, PartialEq, Eq)]
pub struct PlainDateTime {
Expand Down
50 changes: 48 additions & 2 deletions src/builtins/core/zoneddatetime.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! This module implements `ZonedDateTime` and any directly related algorithms.
//! This module contains the core implementation of the `ZonedDateTime`
//! builtin type.

use alloc::string::String;
use core::{cmp::Ordering, num::NonZeroU128};
Expand Down Expand Up @@ -80,7 +81,47 @@ impl PartialZonedDateTime {
}
}

/// The native Rust implementation of `Temporal.ZonedDateTime`.
/// The native Rust implementation of a Temporal `ZonedDateTime`.
///
/// A `ZonedDateTime` represents a date and time in a specific time
/// zone and calendar. A `ZonedDateTime` is represented as an instant
/// of unix epoch nanoseconds with a calendar, and a time zone.
///
/// ## Time zone provider` API
///
/// The core implementation of `ZonedDateTime` are primarily time zone
/// provider APIs denoted by a `*_with_provider` suffix. This means a
/// provider that implements the `TimeZoneProvider` trait must be provided.
///
/// A default file system time zone provider, `FsTzdbProvider`, can be
/// enabled with the `tzdb` feature flag.
///
/// The non time zone provider API, which is a default implementation of the
/// methods using `FsTzdbProvider` can be enabled with the `compiled_data`
/// feature flag.
///
/// ## Example
///
/// ```rust
/// use temporal_rs::{Calendar, Instant, TimeZone, ZonedDateTime};
///
/// let zoned_date_time = ZonedDateTime::try_new(
/// 0,
/// Calendar::default(),
/// TimeZone::default(),
/// ).unwrap();
///
/// assert_eq!(zoned_date_time.epoch_milliseconds(), 0);
/// assert_eq!(zoned_date_time.epoch_nanoseconds().as_i128(), 0);
/// assert_eq!(zoned_date_time.timezone().identifier().unwrap(), "UTC");
/// assert_eq!(zoned_date_time.calendar().identifier(), "iso8601");
/// ```
///
/// ## Reference
///
/// For more information, see the [MDN documentation][mdn-zoneddatetime].
///
/// [mdn-zoneddatetime]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ZonedDateTime {
Expand Down Expand Up @@ -405,6 +446,7 @@ impl ZonedDateTime {
&self.tz
}

/// Create a `ZonedDateTime` from a `PartialZonedDateTime`.
#[inline]
pub fn from_partial_with_provider(
partial: PartialZonedDateTime,
Expand Down Expand Up @@ -648,13 +690,15 @@ impl ZonedDateTime {
Ok(iso.time.nanosecond)
}

/// Returns an offset string for the current `ZonedDateTime`.
pub fn offset_with_provider(&self, provider: &impl TimeZoneProvider) -> TemporalResult<String> {
let offset = self
.tz
.get_offset_nanos_for(self.epoch_nanoseconds().as_i128(), provider)?;
Ok(nanoseconds_to_formattable_offset(offset).to_string())
}

/// Returns the offset nanoseconds for the current `ZonedDateTime`.
pub fn offset_nanoseconds_with_provider(
&self,
provider: &impl TimeZoneProvider,
Expand Down Expand Up @@ -700,6 +744,7 @@ pub(crate) fn nanoseconds_to_formattable_offset(nanoseconds: i128) -> Formattabl
// ==== Core calendar method implementations ====

impl ZonedDateTime {
/// Returns the era for the current `ZonedDateTime`.
pub fn era_with_provider(
&self,
provider: &impl TimeZoneProvider,
Expand All @@ -709,6 +754,7 @@ impl ZonedDateTime {
Ok(self.calendar.era(&pdt.iso.date))
}

/// Returns the era-specific year for the current `ZonedDateTime`.
pub fn era_year_with_provider(
&self,
provider: &impl TimeZoneProvider,
Expand Down