pub struct DicomTime(/* private fields */);
Expand description
Represents a Dicom time (TM) value with a partial precision, where some time components may be missing.
Unlike chrono::NaiveTime, this implementation has only 6 digit precision for fraction of a second.
DicomTime
implements AsRange trait, enabling to retrieve specific
time values.
§Example
use chrono::NaiveTime;
use dicom_core::value::{DicomTime, AsRange};
let time = DicomTime::from_hm(12, 30)?;
assert_eq!(
Some(time.latest()?),
NaiveTime::from_hms_micro_opt(12, 30, 59, 999_999)
);
let milli = DicomTime::from_hms_milli(12, 30, 59, 123)?;
// value still not precise to microsecond
assert_eq!(milli.is_precise(), false);
assert_eq!(milli.to_string(), "12:30:59.123");
// for convenience, is precise enough to be retrieved as a NaiveTime
assert_eq!(
Some(milli.to_naive_time()?),
NaiveTime::from_hms_micro_opt(12, 30, 59, 123_000)
);
let time = DicomTime::try_from(&NaiveTime::from_hms_opt(12, 30, 59).unwrap())?;
// conversion from chrono value leads to a precise value
assert_eq!(time.is_precise(), true);
Implementations§
Source§impl DicomTime
impl DicomTime
Sourcepub fn from_h(hour: u8) -> Result<DicomTime, Error>
pub fn from_h(hour: u8) -> Result<DicomTime, Error>
Constructs a new DicomTime
with hour precision
(HH
).
Sourcepub fn from_hm(hour: u8, minute: u8) -> Result<DicomTime, Error>
pub fn from_hm(hour: u8, minute: u8) -> Result<DicomTime, Error>
Constructs a new DicomTime
with hour and minute precision
(HHMM
).
Sourcepub fn from_hms(hour: u8, minute: u8, second: u8) -> Result<DicomTime, Error>
pub fn from_hms(hour: u8, minute: u8, second: u8) -> Result<DicomTime, Error>
Constructs a new DicomTime
with hour, minute and second precision
(HHMMSS
).
Sourcepub fn from_hms_milli(
hour: u8,
minute: u8,
second: u8,
millisecond: u32,
) -> Result<DicomTime, Error>
pub fn from_hms_milli( hour: u8, minute: u8, second: u8, millisecond: u32, ) -> Result<DicomTime, Error>
Constructs a new DicomTime
from an hour, minute, second and millisecond value,
which leads to the precision HHMMSS.FFF
. Millisecond cannot exceed 999
.
Sourcepub fn from_hms_micro(
hour: u8,
minute: u8,
second: u8,
microsecond: u32,
) -> Result<DicomTime, Error>
pub fn from_hms_micro( hour: u8, minute: u8, second: u8, microsecond: u32, ) -> Result<DicomTime, Error>
Constructs a new DicomTime
from an hour, minute, second and microsecond value,
which leads to the full precision HHMMSS.FFFFFF
.
Microsecond cannot exceed 999_999
.
Instead, leap seconds can be represented by setting second
to 60.
Sourcepub fn millisecond(&self) -> Option<u32>
pub fn millisecond(&self) -> Option<u32>
Retrieves the fraction of a second in milliseconds.
Only returns Some(_)
if the time is precise to the millisecond or more.
Any precision beyond the millisecond is discarded.
Sourcepub fn fraction_micro(&self) -> Option<u32>
pub fn fraction_micro(&self) -> Option<u32>
Retrieves the total known fraction of a second in microseconds.
Only returns None
if the time value defines no fraction of a second.
§Example
let time: DicomTime = "202346.2500".parse()?;
assert_eq!(time.fraction_micro(), Some(250_000));
let time: DicomTime = "202346".parse()?;
assert_eq!(time.fraction_micro(), None);
Sourcepub fn fraction_ms(&self) -> Option<u32>
pub fn fraction_ms(&self) -> Option<u32>
Retrieves the total known fraction of a second in milliseconds.
This may result in precision loss if the time value was more precise than 3 decimal places.
Only returns None
if the time value defines no fraction of a second.
§Example
let time: DicomTime = "202346.2500".parse()?;
assert_eq!(time.fraction_ms(), Some(250));
let time: DicomTime = "202346".parse()?;
assert_eq!(time.fraction_ms(), None);
Sourcepub fn fraction_precision(&self) -> u8
pub fn fraction_precision(&self) -> u8
Retrieves the precision of the fraction of a second,
in number of decimal places (in 0..=6
).
Sourcepub fn fraction_str(&self) -> String
pub fn fraction_str(&self) -> String
Retrieves the fraction of a second encoded as a small string. The length of the string matches the number of known decimal places in the fraction of a second. Returns an empty string if the time value defines no fraction of a second.
§Example
let time: DicomTime = "202346.2500".parse()?;
assert_eq!(&time.fraction_str(), "2500");
let time: DicomTime = "202346".parse()?;
assert_eq!(&time.fraction_str(), "");
Trait Implementations§
Source§impl AsRange for DicomTime
impl AsRange for DicomTime
type PreciseValue = NaiveTime
type Range = TimeRange
Source§fn is_precise(&self) -> bool
fn is_precise(&self) -> bool
Source§fn earliest(&self) -> Result<Self::PreciseValue, Error>
fn earliest(&self) -> Result<Self::PreciseValue, Error>
DateComponent
s, it fails.Source§fn latest(&self) -> Result<Self::PreciseValue, Error>
fn latest(&self) -> Result<Self::PreciseValue, Error>
DateComponent
s, it fails.Source§impl From<DicomTime> for PrimitiveValue
impl From<DicomTime> for PrimitiveValue
impl Copy for DicomTime
impl StructuralPartialEq for DicomTime
Auto Trait Implementations§
impl Freeze for DicomTime
impl RefUnwindSafe for DicomTime
impl Send for DicomTime
impl Sync for DicomTime
impl Unpin for DicomTime
impl UnwindSafe for DicomTime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more