Skip to content

Commit 49fa75b

Browse files
authored
Add intro documentation to ZonedDateTime and PlainDateTime (#253)
This PR adds some intro documentation to `ZonedDateTime` and `PlainDateTime`. Something I noticed while adding this is that we may benefit from having at least one `TimeZoneProvider` implementation available by default (this would mean adding `tzdb` to default).
1 parent abf429c commit 49fa75b

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

src/builtins/core/datetime.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@ impl PartialDateTime {
5353
}
5454
}
5555

56-
/// The native Rust implementation of `Temporal.PlainDateTime`
56+
// TODO: Example doctest
57+
/// The native Rust implementation of a Temporal `PlainDateTime`.
58+
///
59+
/// The `PlainDateTime` represents a date and time without a
60+
/// time zone. The fundemental represenation of a `PlainDateTime`
61+
/// is it's internal ISO date and time fields and a calendar.
62+
///
63+
/// ## Reference
64+
///
65+
/// For more information, see the [MDN documentation][mdn-datetime].
66+
///
67+
/// [mdn-datetime]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/PlainDateTime
5768
#[non_exhaustive]
5869
#[derive(Debug, Default, Clone, PartialEq, Eq)]
5970
pub struct PlainDateTime {

src/builtins/core/zoneddatetime.rs

+48-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! This module implements `ZonedDateTime` and any directly related algorithms.
1+
//! This module contains the core implementation of the `ZonedDateTime`
2+
//! builtin type.
23
34
use alloc::string::String;
45
use core::{cmp::Ordering, num::NonZeroU128};
@@ -80,7 +81,47 @@ impl PartialZonedDateTime {
8081
}
8182
}
8283

83-
/// The native Rust implementation of `Temporal.ZonedDateTime`.
84+
/// The native Rust implementation of a Temporal `ZonedDateTime`.
85+
///
86+
/// A `ZonedDateTime` represents a date and time in a specific time
87+
/// zone and calendar. A `ZonedDateTime` is represented as an instant
88+
/// of unix epoch nanoseconds with a calendar, and a time zone.
89+
///
90+
/// ## Time zone provider` API
91+
///
92+
/// The core implementation of `ZonedDateTime` are primarily time zone
93+
/// provider APIs denoted by a `*_with_provider` suffix. This means a
94+
/// provider that implements the `TimeZoneProvider` trait must be provided.
95+
///
96+
/// A default file system time zone provider, `FsTzdbProvider`, can be
97+
/// enabled with the `tzdb` feature flag.
98+
///
99+
/// The non time zone provider API, which is a default implementation of the
100+
/// methods using `FsTzdbProvider` can be enabled with the `compiled_data`
101+
/// feature flag.
102+
///
103+
/// ## Example
104+
///
105+
/// ```rust
106+
/// use temporal_rs::{Calendar, Instant, TimeZone, ZonedDateTime};
107+
///
108+
/// let zoned_date_time = ZonedDateTime::try_new(
109+
/// 0,
110+
/// Calendar::default(),
111+
/// TimeZone::default(),
112+
/// ).unwrap();
113+
///
114+
/// assert_eq!(zoned_date_time.epoch_milliseconds(), 0);
115+
/// assert_eq!(zoned_date_time.epoch_nanoseconds().as_i128(), 0);
116+
/// assert_eq!(zoned_date_time.timezone().identifier().unwrap(), "UTC");
117+
/// assert_eq!(zoned_date_time.calendar().identifier(), "iso8601");
118+
/// ```
119+
///
120+
/// ## Reference
121+
///
122+
/// For more information, see the [MDN documentation][mdn-zoneddatetime].
123+
///
124+
/// [mdn-zoneddatetime]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Temporal/ZonedDateTime
84125
#[non_exhaustive]
85126
#[derive(Debug, Clone, PartialEq, Eq)]
86127
pub struct ZonedDateTime {
@@ -405,6 +446,7 @@ impl ZonedDateTime {
405446
&self.tz
406447
}
407448

449+
/// Create a `ZonedDateTime` from a `PartialZonedDateTime`.
408450
#[inline]
409451
pub fn from_partial_with_provider(
410452
partial: PartialZonedDateTime,
@@ -648,13 +690,15 @@ impl ZonedDateTime {
648690
Ok(iso.time.nanosecond)
649691
}
650692

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

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

702746
impl ZonedDateTime {
747+
/// Returns the era for the current `ZonedDateTime`.
703748
pub fn era_with_provider(
704749
&self,
705750
provider: &impl TimeZoneProvider,
@@ -709,6 +754,7 @@ impl ZonedDateTime {
709754
Ok(self.calendar.era(&pdt.iso.date))
710755
}
711756

757+
/// Returns the era-specific year for the current `ZonedDateTime`.
712758
pub fn era_year_with_provider(
713759
&self,
714760
provider: &impl TimeZoneProvider,

0 commit comments

Comments
 (0)