pub struct TimeParts {Show 19 fields
pub yr: Option<i64>,
pub mo: Option<u8>,
pub day: Option<u8>,
pub hr: Option<u8>,
pub min: Option<u8>,
pub sec: Option<u8>,
pub attos: Option<u64>,
pub offset: Option<Offset>,
pub iana_name: Option<LiteStr<49>>,
pub is_leap_sec: bool,
pub scale: Scale,
pub wkday: Option<Weekday>,
pub day_of_yr: Option<u16>,
pub iso_wk_yr: Option<i64>,
pub iso_wk: Option<u8>,
pub wk_sun: Option<u8>,
pub wk_mon: Option<u8>,
pub meridiem: Option<Meridiem>,
pub unix_timestamp_seconds: Option<i64>,
}Expand description
A flexible, partially-filled representation of a civil datetime.
TimeParts is the central intermediate type used throughout the library
for parsing, formatting, and converting between different time representations
(CCSDS, ISO-like strings, chrono, jiff, Dt, etc.).
Most fields are optional, allowing partial dates/times. It also carries
metadata such as the time scale, IANA zone name, leap-second flag,
and various weekday/week-number representations.
- Convert to [
Dt] usingTimeParts::to_dt. - Conversions to types from other crates require relevant features to be enabled.
Fields§
§yr: Option<i64>Year (can be negative for BCE dates).
mo: Option<u8>Month of the year (1–12).
day: Option<u8>Day of the month (1–31).
hr: Option<u8>Hour of the day (0–23).
min: Option<u8>Minute of the hour (0–59).
sec: Option<u8>Second of the minute (0–60). Value 60 is used for leap seconds.
attos: Option<u64>Attoseconds (0 ≤ value < 10¹⁸).
offset: Option<Offset>Timezone offset from UTC.
iana_name: Option<LiteStr<49>>IANA timezone name (e.g. "America/New_York"), stored as ASCII.
is_leap_sec: boolWhether this instant represents a leap second.
scale: ScaleThe time scale this value belongs to (TAI, UTC, etc.).
wkday: Option<Weekday>Day of the week.
day_of_yr: Option<u16>Day of the year (1–366), corresponding to %j.
iso_wk_yr: Option<i64>ISO week year (%G / %g).
iso_wk: Option<u8>ISO week number (1–53), corresponding to %V.
wk_sun: Option<u8>Week number with Sunday as first day of week (0–53), %U.
wk_mon: Option<u8>Week number with Monday as first day of week (0–53), %W.
meridiem: Option<Meridiem>AM / PM indicator.
unix_timestamp_seconds: Option<i64>Unix timestamp in seconds (%s).
Implementations§
Source§impl TimeParts
impl TimeParts
Sourcepub const WIRE_VERSION: u8 = 1
pub const WIRE_VERSION: u8 = 1
Current wire format version.
Sourcepub fn to_wire_bytes(&self) -> [u8; 120]
pub fn to_wire_bytes(&self) -> [u8; 120]
Serializes TimeParts into a fixed 120-byte buffer.
Layout:
- Byte 0: Version (
WIRE_VERSION) - Bytes 1..120: Data (119 bytes)
Sourcepub fn from_wire_bytes(bytes: &[u8]) -> Option<Self>
pub fn from_wire_bytes(bytes: &[u8]) -> Option<Self>
Deserializes TimeParts from exactly 120 bytes.
Returns None if the version byte is unknown or the data is invalid.
Source§impl TimeParts
impl TimeParts
Sourcepub fn days_since_1958_to_gregorian(days_since_epoch: i64) -> (i64, u8, u8)
pub fn days_since_1958_to_gregorian(days_since_epoch: i64) -> (i64, u8, u8)
Converts days since 1958-01-01 (midnight UTC/TAI) into Gregorian date. Pure integer arithmetic matching the CCSDS 301.0-B-4 Level 1 epoch.
Sourcepub fn gregorian_to_days_since_1958(year: i64, month: u8, day: u8) -> i64
pub fn gregorian_to_days_since_1958(year: i64, month: u8, day: u8) -> i64
Exact inverse of days_since_1958_to_gregorian.
Sourcepub fn from_ccsds_ccs(input: &[u8]) -> Result<TimeParts, DtErr>
pub fn from_ccsds_ccs(input: &[u8]) -> Result<TimeParts, DtErr>
Parses a CCSDS Calendar Segmented Time Code (CCS) into TimeParts.
Implements CCSDS 301.0-B-4 §3.4 (Level 1 only).
This function accepts a single-byte P-field followed by a BCD-encoded T-field. It supports both calendar variants:
- Month/Day format (most common)
- Day-of-Year format
§P-field
- Must not have the extension bit set (only 1-byte P-fields are supported).
- Code ID must be
101. - Subsecond resolution: 0 to 6 BCD octets (0–12 decimal digits).
§T-field
- Year is encoded as 4 BCD digits (0001–9999).
- Time of day uses BCD with leap second support (
second == 60). - When a leap second is present,
secondis normalized to 59 andis_leap_secondis set totruein the returnedTimeParts.
§Epoch
1958-01-01 00:00:00 UTC (identical to CDS).
§Errors
Returns an error if the P-field is extended, the Code ID is wrong, BCD digits are invalid, field lengths are insufficient, or any component (month, day, DOY, hour, minute, second) is out of range.
The resulting TimeParts has scale = UTC.
Sourcepub fn from_ccsds_c(input: &[u8]) -> Result<TimeParts, DtErr>
pub fn from_ccsds_c(input: &[u8]) -> Result<TimeParts, DtErr>
Parses a CCSDS Unsegmented Time Code (CUC) into TimeParts.
Implements CCSDS 301.0-B-4 §3.2 (Level 1), including full support for the extended 2-byte P-field defined in Issue 4.
§P-field
- Supports both 1-byte and 2-byte P-fields.
- Code ID must be
001(1958-01-01 TAI epoch). - Coarse time: 1–7 octets total.
- Fractional time: 0–10 octets total.
- P-fields longer than 2 bytes are rejected.
§T-field
- Coarse time is interpreted as seconds since the 1958 TAI epoch.
- Fractional time is converted to attoseconds using exact integer scaling
(
value / 2^(8·n_frac)).
§Epoch
1958-01-01 00:00:00 TAI.
§Errors
Returns an error for empty input, insufficient length, invalid Code ID, unsupported further P-field extensions, or malformed T-field data.
The resulting TimeParts has scale = TAI.
Sourcepub fn from_ccsds_d(input: &[u8]) -> Result<TimeParts, DtErr>
pub fn from_ccsds_d(input: &[u8]) -> Result<TimeParts, DtErr>
Parses a CCSDS Day Segmented Time Code (CDS) into TimeParts.
Implements CCSDS 301.0-B-4 §3.3 (Level 1).
§P-field
- Supports optional 2-byte P-field.
- Code ID must be
100. - Epoch bit must be
0(1958-01-01 UTC epoch only). - Day count: 2 or 3 bytes.
- Sub-millisecond resolution: none, 2 bytes (µs), or 4 bytes (2⁻³² of a ms).
§T-field
- Day count is days since 1958-01-01 UTC.
- Milliseconds since midnight are always 4 bytes.
- Sub-millisecond field (if present) is converted to attoseconds.
§Leap Second Handling
This implementation correctly supports leap seconds. When millis_of_day
represents 23:59:60 (i.e. ≥ 86,400,000 ms), second is set to 60 and
is_leap_second is set to true in the returned TimeParts.
§Epoch
1958-01-01 00:00:00 UTC.
§Errors
Returns an error for empty input, wrong Code ID, non-Level-1 epoch, unsupported sub-millisecond code, insufficient length, or invalid data.
The resulting TimeParts has scale = UTC.
Sourcepub fn from_ccsds_bin(input: &[u8]) -> Result<TimeParts, DtErr>
pub fn from_ccsds_bin(input: &[u8]) -> Result<TimeParts, DtErr>
Auto-detects and parses a CCSDS binary time code (CUC, CDS, or CCS).
Examines the Code ID in the first P-field byte and dispatches to the appropriate parser:
001→TimeParts::from_ccsds_c(CUC)100→TimeParts::from_ccsds_d(CDS)101→TimeParts::from_ccsds_ccs(CCS)
This is a convenience wrapper. For stricter control or when the format
is known in advance, prefer calling the specific from_ccsds_* function directly.
§Errors
Returns an error if the input is empty or the Code ID is not one of the three recognized Level 1 values.
Source§impl TimeParts
impl TimeParts
Sourcepub fn from_str(
fmt: &str,
input: &str,
inp_can_end_before_fmt: bool,
fmt_can_end_before_inp: bool,
allow_partial_date: bool,
) -> Result<TimeParts, DtErr>
pub fn from_str( fmt: &str, input: &str, inp_can_end_before_fmt: bool, fmt_can_end_before_inp: bool, allow_partial_date: bool, ) -> Result<TimeParts, DtErr>
Low-level parser equivalent to strptime with a provided format string.
This is the core entry point for format-string based parsing in the library.
It supports a rich set of % directives (similar to C strptime, Python
strftime/strptime, and common extensions used by chrono/jiff).
The parser populates a TimeParts struct with all fields that can be
extracted from the input. After parsing, Self::finish is called
automatically to apply defaults and validation.
§Parameters
fmt: The format string containing%directives.input: The string to parse.inp_can_end_before_fmt: Iftrue, the input may end before the format string is fully consumed (extra format specifiers are ignored).fmt_can_end_before_inp: Iftrue, the format may end before the input is fully consumed (trailing characters in the input are allowed).allow_partial_date: Iftrue, a missing month/day will be defaulted to1instead of returning an [Incomplete] error.
§Errors
Returns DtErr for:
- Parse failures (
InvalidFormat,OutOfRange, etc.) - Incomplete data when
allow_partial_dateisfalse - Trailing characters (when
fmt_can_end_before_inpisfalse)
Sourcepub fn finish(&mut self, allow_partial_date: bool) -> Result<&mut Self, DtErr>
pub fn finish(&mut self, allow_partial_date: bool) -> Result<&mut Self, DtErr>
Finalizes a TimeParts after parsing by applying sensible defaults and
performing validation.
This is called automatically by the various parsing paths (from_str,
CCSDS parsers, etc.). It ensures the struct is in a consistent state
before being turned into a full [Dt] or passed to other converters.
§Behavior
- If a Unix timestamp is present, it takes precedence and the time
components are defaulted to
00:00:00.000000000with a UTC offset. - Otherwise:
- Hour/minute/second/attoseconds/offset are defaulted to
0/Utc. - Leap seconds (
second == 60) are detected and flagged.
- Hour/minute/second/attoseconds/offset are defaulted to
- Date completeness is checked in this priority order:
- Calendar date (
year,month,day) - Ordinal date (
year,day_of_year) - ISO week date (
iso_week_year,iso_week)
- Calendar date (
- If
allow_partial_dateistrue, missing month/day are defaulted to1.
§Errors
DtErrKind::Incompleteif no valid date representation is present.DtErrKind::OutOfRangefor seconds outside0..=60.
Source§impl TimeParts
impl TimeParts
Sourcepub fn from_str_ccsds(input: &str) -> Result<Self, DtErr>
pub fn from_str_ccsds(input: &str) -> Result<Self, DtErr>
Generalized CCSDS ASCII Time Code parser (A or B variant).
- Handles both calendar (
%Y-%m-%d) and day-of-year (%Y-%j) formats.- e.g. 2000-01-01T12:00:00
- All time components after the date portion are optional.
Source§impl TimeParts
impl TimeParts
Sourcepub fn to_ccsds_c(
&self,
n_coarse: u8,
n_frac: u8,
extension: bool,
) -> Result<([u8; 32], usize), DtErr>
pub fn to_ccsds_c( &self, n_coarse: u8, n_frac: u8, extension: bool, ) -> Result<([u8; 32], usize), DtErr>
Formats this TimeParts as a CCSDS C (CUC) binary time code.
- Fully configurable for round-tripping with [
from_ccsds_c]. - Conforms to CCSDS 301.0-B-4 §3.2 (Level 1), including full support
for the extended P-field (second octet) when
n_coarse > 4orn_frac > 3.
§Parameters
n_coarse: 1–7 (number of coarse-time octets)n_frac: 0–10 (number of fractional octets)extension: advisory flag (ignored when larger sizes force the second octet)
Sourcepub fn to_ccsds_d(
&self,
n_day: u8,
sub_ms_code: u8,
extension: bool,
) -> Result<([u8; 32], usize), DtErr>
pub fn to_ccsds_d( &self, n_day: u8, sub_ms_code: u8, extension: bool, ) -> Result<([u8; 32], usize), DtErr>
Formats this TimeParts as a CCSDS D (CDS) binary time code.
- Fully configurable for round-tripping with [
from_ccsds_d]. - Conforms to CCSDS 301.0-B-4 §3.3 (Level 1): UTC day count + ms-of-day since 1958-01-01 UTC.
Sourcepub fn to_ccsds_ccs(
&self,
use_doy: bool,
n_subsec: u8,
) -> Result<([u8; 14], usize), DtErr>
pub fn to_ccsds_ccs( &self, use_doy: bool, n_subsec: u8, ) -> Result<([u8; 14], usize), DtErr>
Formats this TimeParts as a CCSDS CCS (Calendar Segmented Time Code).
Implements CCSDS 301.0-B-4 §3.4 (Level 1 only).
§Parameters
use_doy:false= Month/Day variant (most common),true= Day-of-Year variantn_subsec: Number of subsecond BCD octets (0–6). Each octet holds 2 decimal digits.
§Returns
(buffer, written_len) — the P-field + T-field (big-endian BCD).
§Precision & Rounding
Fractional seconds are rounded to the nearest representable value at the chosen precision
(exactly as to_ccsds_d does for milliseconds).
Sourcepub fn to_ccsds_bin(&self) -> Result<([u8; 32], usize), DtErr>
pub fn to_ccsds_bin(&self) -> Result<([u8; 32], usize), DtErr>
Convenience method that automatically selects the most appropriate
CCSDS binary time code based on this TimeParts’s [Scale].
§Automatic selection (matches common mission practice)
Scale::TAI→ CUC (4 coarse + 4 fractional bytes)- Any other
Scale(UTC, TT, GPS, TCG, …) → converted to UTC and uses CDS (2 day bytes + 4 ms bytes + 2-byte sub-ms)
Source§impl TimeParts
impl TimeParts
Sourcepub fn to_str_ccsds(&self) -> Result<String, DtErr>
pub fn to_str_ccsds(&self) -> Result<String, DtErr>
Returns this instant as a CCSDS ASCII Time Code (calendar variant A).
Example: "2025-04-17T14:30:45.123456789Z"
- Uses
Tseparator and trailingZ. - Fractional seconds are trimmed (no trailing zeros, no dot if zero).
- Round-trips with
Dt::from_str_ccsds/TimeParts::from_str_ccsds.
Sourcepub fn to_str_ccsds_nf(&self, max_precision: usize) -> Result<String, DtErr>
pub fn to_str_ccsds_nf(&self, max_precision: usize) -> Result<String, DtErr>
Same as [to_str_ccsds] but lets you control the maximum number of fractional digits (0–18).
Sourcepub fn to_ccsds_doy_str(&self) -> Result<String, DtErr>
pub fn to_ccsds_doy_str(&self) -> Result<String, DtErr>
Returns this instant as a CCSDS ASCII Time Code B (day-of-year variant).
Example: "2025-107T14:30:45.123456789Z"
Source§impl TimeParts
impl TimeParts
Sourcepub fn to_chrono_naive_datetime(&self) -> Result<NaiveDateTime, DtErr>
pub fn to_chrono_naive_datetime(&self) -> Result<NaiveDateTime, DtErr>
Converts TimeParts → chrono::NaiveDateTime (civil time, no TZ).
Sourcepub fn to_chrono_datetime(&self) -> Result<DateTime<FixedOffset>, DtErr>
pub fn to_chrono_datetime(&self) -> Result<DateTime<FixedOffset>, DtErr>
Converts TimeParts → chrono::DateTime.
- If this
TimePartshas a unix timestamp then it is used instead of anything else, timezones are ignored in this route. - If the
"chrono-tz"feature is enabled then chronos tz features are used to parse the IANA name. - If the
"chrono-tz"feature is not enabled then the library’s own tz handling will be used.
Sourcepub fn to_chrono_timestamp(&self) -> Result<i64, DtErr>
pub fn to_chrono_timestamp(&self) -> Result<i64, DtErr>
- If this
TimePartshas a unix timestamp then it is used instead of anything else, timezones are ignored in this route. - If the
"chrono-tz"feature is enabled then chronos tz features are used to parse the IANA name. - If the
"chrono-tz"feature is not enabled then the library’s own tz handling will be used. - Uses
TimeParts::to_chrono_datetimeinternally.
Source§impl TimeParts
impl TimeParts
Sourcepub fn to_jiff_broken_down_time(&self) -> Result<BrokenDownTime, DtErr>
pub fn to_jiff_broken_down_time(&self) -> Result<BrokenDownTime, DtErr>
Converts TimeParts → jiff::fmt::strtime::BrokenDownTime.
Sourcepub fn to_jiff_zoned(&self) -> Result<Zoned, DtErr>
pub fn to_jiff_zoned(&self) -> Result<Zoned, DtErr>
Converts TimeParts → jiff::Zoned.
Sourcepub fn to_jiff_timestamp(&self) -> Result<Timestamp, DtErr>
pub fn to_jiff_timestamp(&self) -> Result<Timestamp, DtErr>
Converts TimeParts → jiff::Timestamp.