Skip to content

Commit 26e5fc3

Browse files
committed
Improve documentation of Parset::to_*
1 parent 1a2ee3a commit 26e5fc3

File tree

1 file changed

+81
-18
lines changed

1 file changed

+81
-18
lines changed

src/format/parsed.rs

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

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

893924
/// Returns a parsed fixed time zone offset out of given fields.
925+
///
926+
/// # Errors
927+
///
928+
/// Returns `OUT_OF_RANGE` if the offset is out of range for a `FixedOffset`.
894929
pub fn to_fixed_offset(&self) -> ParseResult<FixedOffset> {
895930
self.offset.and_then(FixedOffset::east_opt).ok_or(OUT_OF_RANGE)
896931
}
897932

898933
/// Returns a parsed timezone-aware date and time out of given fields.
899934
///
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.
935+
/// This method is able to determine the combined date and time from date, time and offset
936+
/// fields, and/or from a single timestamp field. It checks all fields are consistent with each
937+
/// other.
938+
///
939+
/// # Errors
940+
///
941+
/// This method returns:
942+
/// - `IMPOSSIBLE` if any of the date fields conflict, or if a timestamp conflicts with any of
943+
/// the other fields.
944+
/// - `OUT_OF_RANGE` if any of the fields of `Parsed` are set to a value beyond their acceptable
945+
/// range.
946+
/// - `OUT_OF_RANGE` if the value would be outside the range of a [`NaiveDateTime`] or
947+
/// [`FixedOffset`].
948+
/// - `NOT_ENOUGH` if there are not enough fields set in `Parsed` for a complete datetime
949+
/// including offset from UTC.
950+
/// - `OUT_OF_RANGE` if the date does not exist.
904951
pub fn to_datetime(&self) -> ParseResult<DateTime<FixedOffset>> {
905952
// If there is no explicit offset, consider a timestamp value as indication of a UTC value.
906953
let offset = match (self.offset, self.timestamp) {
@@ -919,14 +966,30 @@ impl Parsed {
919966
}
920967

921968
/// 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).
969+
/// with an additional [`TimeZone`] used to interpret and validate the local date.
970+
///
971+
/// This method is able to determine the combined date and time from date and time, and/or from
972+
/// a single timestamp field. It checks all fields are consistent with each other.
973+
///
974+
/// If the parsed fields include an UTC offset, it also has to be consistent with the offset in
975+
/// the provided `tz` time zone for that datetime.
976+
///
977+
/// # Errors
978+
///
979+
/// This method returns:
980+
/// - `IMPOSSIBLE` if any of the date fields conflict, if a timestamp conflicts with any of
981+
/// the other fields, or if the offset field is set but differs from the offset at that time
982+
/// in the `tz` time zone.
983+
/// - `OUT_OF_RANGE` if the value would be outside the range of a [`NaiveDateTime`] or
984+
/// [`FixedOffset`].
985+
/// - `OUT_OF_RANGE` if any of the fields of `Parsed` are set to a value beyond their acceptable
986+
/// range.
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` if the date does not exist, or `IMPOSSIBLE` if the local datetime does not
991+
/// exists in the provided time zone (because it falls in a transition due to for example
992+
/// DST).
930993
pub fn to_datetime_with_timezone<Tz: TimeZone>(&self, tz: &Tz) -> ParseResult<DateTime<Tz>> {
931994
// if we have `timestamp` specified, guess an offset from that.
932995
let mut guessed_offset = 0;

0 commit comments

Comments
 (0)