Skip to content

Commit 4d6aedf

Browse files
committed
Improve documentation of Parset::to_*
1 parent ac8c714 commit 4d6aedf

File tree

1 file changed

+82
-18
lines changed

1 file changed

+82
-18
lines changed

src/format/parsed.rs

Lines changed: 82 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,18 @@ impl Parsed {
570570
///
571571
/// Gregorian year and ISO week date year can have their century number (`*_div_100`) omitted,
572572
/// the two-digit year is used to guess the century number then.
573+
///
574+
/// It checks all given date fields are consistent with each other.
575+
///
576+
/// # Errors
577+
///
578+
/// This method returns:
579+
/// - `IMPOSSIBLE` if any of the date fields conflict.
580+
/// - `NOT_ENOUGH` if there are not enough fields set in `Parsed` for a complete date.
581+
/// - `OUT_OF_RANGE`
582+
/// - if any of the date fields of `Parsed` are set to a value beyond their acceptable range.
583+
/// - if the value would be outside the range of a [`NaiveDate`].
584+
/// - if the date does not exist.
573585
pub fn to_naive_date(&self) -> ParseResult<NaiveDate> {
574586
fn resolve_year(
575587
y: Option<i32>,
@@ -776,6 +788,14 @@ impl Parsed {
776788
/// - Hour, minute, second, nanosecond.
777789
///
778790
/// It is able to handle leap seconds when given second is 60.
791+
///
792+
/// # Errors
793+
///
794+
/// This method returns:
795+
/// - `OUT_OF_RANGE` if any of the time fields of `Parsed` are set to a value beyond
796+
/// their acceptable range.
797+
/// - `NOT_ENOUGH` if an hour field is missing, if AM/PM is missing in a 12-hour clock,
798+
/// if minutes are missing, or if seconds are missing while the nanosecond field is present.
779799
pub fn to_naive_time(&self) -> ParseResult<NaiveTime> {
780800
let hour_div_12 = match self.hour_div_12 {
781801
Some(v @ 0..=1) => v,
@@ -811,13 +831,25 @@ impl Parsed {
811831
NaiveTime::from_hms_nano_opt(hour, minute, second, nano).ok_or(OUT_OF_RANGE)
812832
}
813833

814-
/// Returns a parsed naive date and time out of given fields,
815-
/// except for the [`offset`](#structfield.offset) field (assumed to have a given value).
816-
/// This is required for parsing a local time or other known-timezone inputs.
834+
/// Returns a parsed naive date and time out of given fields, except for the offset field.
835+
///
836+
/// The offset is assumed to have a given value. It is not compared against the offset field set
837+
/// in the `Parsed` type, so it is allowed to be inconsistent.
838+
///
839+
/// This method is able to determine the combined date and time from date and time fields or
840+
/// from a single timestamp field. It checks all fields are consistent with each other.
841+
///
842+
/// # Errors
817843
///
818-
/// This method is able to determine the combined date and time
819-
/// from date and time fields or a single [`timestamp`](#structfield.timestamp) field.
820-
/// Either way those fields have to be consistent to each other.
844+
/// This method returns:
845+
/// - `IMPOSSIBLE` if any of the date fields conflict, or if a timestamp conflicts with any of
846+
/// the other fields.
847+
/// - `NOT_ENOUGH` if there are not enough fields set in `Parsed` for a complete datetime.
848+
/// - `OUT_OF_RANGE`
849+
/// - if any of the date or time fields of `Parsed` are set to a value beyond their acceptable
850+
/// range.
851+
/// - if the value would be outside the range of a [`NaiveDateTime`].
852+
/// - if the date does not exist.
821853
pub fn to_naive_datetime_with_offset(&self, offset: i32) -> ParseResult<NaiveDateTime> {
822854
let date = self.to_naive_date();
823855
let time = self.to_naive_time();
@@ -891,16 +923,32 @@ impl Parsed {
891923
}
892924

893925
/// Returns a parsed fixed time zone offset out of given fields.
926+
///
927+
/// # Errors
928+
///
929+
/// Returns `OUT_OF_RANGE` if the offset is out of range for a `FixedOffset`.
894930
pub fn to_fixed_offset(&self) -> ParseResult<FixedOffset> {
895931
self.offset.and_then(FixedOffset::east_opt).ok_or(OUT_OF_RANGE)
896932
}
897933

898934
/// Returns a parsed timezone-aware date and time out of given fields.
899935
///
900-
/// This method is able to determine the combined date and time
901-
/// from date and time fields or a single [`timestamp`](#structfield.timestamp) field,
902-
/// plus a time zone offset.
903-
/// Either way those fields have to be consistent to each other.
936+
/// This method is able to determine the combined date and time from date, time and offset
937+
/// fields, and/or from a single timestamp field. It checks all fields are consistent with each
938+
/// other.
939+
///
940+
/// # Errors
941+
///
942+
/// This method returns:
943+
/// - `IMPOSSIBLE` if any of the date fields conflict, or if a timestamp conflicts with any of
944+
/// the other fields.
945+
/// - `NOT_ENOUGH` if there are not enough fields set in `Parsed` for a complete datetime
946+
/// including offset from UTC.
947+
/// - `OUT_OF_RANGE`
948+
/// - if any of the fields of `Parsed` are set to a value beyond their acceptable
949+
/// range.
950+
/// - if the value would be outside the range of a [`NaiveDateTime`] or [`FixedOffset`].
951+
/// - if the date does not exist.
904952
pub fn to_datetime(&self) -> ParseResult<DateTime<FixedOffset>> {
905953
// If there is no explicit offset, consider a timestamp value as indication of a UTC value.
906954
let offset = match (self.offset, self.timestamp) {
@@ -919,14 +967,30 @@ impl Parsed {
919967
}
920968

921969
/// Returns a parsed timezone-aware date and time out of given fields,
922-
/// with an additional `TimeZone` used to interpret and validate the local date.
923-
///
924-
/// This method is able to determine the combined date and time
925-
/// from date and time fields or a single [`timestamp`](#structfield.timestamp) field,
926-
/// plus a time zone offset.
927-
/// Either way those fields have to be consistent to each other.
928-
/// If parsed fields include an UTC offset, it also has to be consistent to
929-
/// [`offset`](#structfield.offset).
970+
/// with an additional [`TimeZone`] used to interpret and validate the local date.
971+
///
972+
/// This method is able to determine the combined date and time from date and time, and/or from
973+
/// a single timestamp field. It checks all fields are consistent with each other.
974+
///
975+
/// If the parsed fields include an UTC offset, it also has to be consistent with the offset in
976+
/// the provided `tz` time zone for that datetime.
977+
///
978+
/// # Errors
979+
///
980+
/// This method returns:
981+
/// - `IMPOSSIBLE`
982+
/// - if any of the date fields conflict, if a timestamp conflicts with any of the other
983+
/// fields, or if the offset field is set but differs from the offset at that time in the
984+
/// `tz` time zone.
985+
/// - if the local datetime does not exists in the provided time zone (because it falls in a
986+
/// transition due to for example DST).
987+
/// - `NOT_ENOUGH` if there are not enough fields set in `Parsed` for a complete datetime, or if
988+
/// the local time in the provided time zone is ambiguous (because it falls in a transition
989+
/// due to for example DST) while there is no offset field or timestamp field set.
990+
/// - `OUT_OF_RANGE`
991+
/// - if the value would be outside the range of a [`NaiveDateTime`] or [`FixedOffset`].
992+
/// - if any of the fields of `Parsed` are set to a value beyond their acceptable range.
993+
/// - if the date does not exist.
930994
pub fn to_datetime_with_timezone<Tz: TimeZone>(&self, tz: &Tz) -> ParseResult<DateTime<Tz>> {
931995
// if we have `timestamp` specified, guess an offset from that.
932996
let mut guessed_offset = 0;

0 commit comments

Comments
 (0)