pub struct DOSDateTime { /* private fields */ }Expand description
A datetime in MS-Dos format.
Datetimes are often stored as little-endian 4-byte values, with the first 2-bytes representing
the time and the second 2-bytes representing the date. For a more complete explanation on time
format, see the documentation for DOSDate and DOSTime.
use dostime::{DOSDate, DOSTime, DOSDateTime};
let date = DOSDate::new(2017, 4, 6).unwrap();
let time = DOSTime::new(13, 24, 54).unwrap();
let datetime1 = DOSDateTime::new(2017, 4, 6, 13, 24, 54).unwrap();
let datetime2 = DOSDateTime::try_from((date, time)).unwrap();
let datetime3 = DOSDateTime::try_from([0x1B, 0x6B, 0x86, 0x4A]).unwrap();
assert_eq!(datetime1, datetime2);
assert_eq!(datetime1, datetime3);
let int: u32 = datetime1.into();
assert_eq!(int, 0x4A86_6B1B);
let bytes: [u8; 4] = datetime2.into();
assert_eq!(bytes, [0x1B, 0x6B, 0x86, 0x4A]);The functions that convert to and from u32s interpret the value as big-endian since that is
consistent with the behavior of the u16 conversion for DOSDate and DOSTime. The functions
that convert to and from [u8; 4] interpret the values as little-endian since the bytes are
often stored as little-endian values.
Not all 4-byte sequences correspond to a valid datetime. This implementation rejects these
timestamps and disallows their construction (hence the use of TryFrom rather than From).
However, all possible DOSDateTimes can be converted into a valid 4-byte sequence (hence the
use of Into).
Implementations§
Source§impl DOSDateTime
impl DOSDateTime
Sourcepub fn new(
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Result<Self, DateTimeError>
pub fn new( year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Result<Self, DateTimeError>
Attempts to construct a new instance of DOSDateTime. If any aspect of the datetime is
invalid, then the creation fails and an error is returned. The error returned is based on
the error returned by DOSDate::new or DOSTime::new.
use dostime::DOSDateTime;
use dostime::datetime::DateTimeError;
use dostime::date::DateError;
use dostime::time::TimeError;
// Construct valid datetimes.
let datetime1 = DOSDateTime::new(2017, 4, 6, 13, 24, 54).unwrap();
let datetime2 = DOSDateTime::new(1980, 1, 1, 0, 0, 0).unwrap();
// Invalid datetimes can't be constructed.
let bad_date = DOSDateTime::new(2011, 2, 29, 14, 0, 2).unwrap_err();
assert_eq!(bad_date, DateTimeError::DateError(DateError::InvalidDay));
let bad_time = DOSDateTime::new(1994, 9, 15, 14, 50, 60).unwrap_err();
assert_eq!(bad_time, DateTimeError::TimeError(TimeError::InvalidSecond));Sourcepub fn new_or_panic(
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Self
pub fn new_or_panic( year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Self
Creates a new instance of a DOSDateTime. If any aspect of the datetime is invalid, then
the function panics.
use dostime::DOSDateTime;
// Construct valid dates normally.
let date1 = DOSDateTime::new_or_panic(1980, 1, 1, 0, 0, 0);
let date2 = DOSDateTime::new_or_panic(2000, 3, 4, 15, 21, 19);use dostime::DOSDateTime;
// Invalid dates panic
DOSDateTime::new_or_panic(2000, 11, 31, 12, 13, 14);Trait Implementations§
Source§impl Clone for DOSDateTime
impl Clone for DOSDateTime
Source§fn clone(&self) -> DOSDateTime
fn clone(&self) -> DOSDateTime
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more