DOSDateTime

Struct DOSDateTime 

Source
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

Source

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));
Source

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);
Source

pub fn year(&self) -> u16

Returns the year for this DOSDate.

Source

pub fn month(&self) -> u8

Returns the month for this DOSDate.

Source

pub fn day(&self) -> u8

Returns the day for this DOSDate.

Source

pub fn hour(&self) -> u8

Returns the hour for this DOSTime.

Source

pub fn minute(&self) -> u8

Returns the minute for this DOSTime.

Source

pub fn second(&self) -> u8

Returns the second for this DOSTime.

Trait Implementations§

Source§

impl Clone for DOSDateTime

Source§

fn clone(&self) -> DOSDateTime

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DOSDateTime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DOSDateTime

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for DOSDateTime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<(DOSDate, DOSTime)> for DOSDateTime

Source§

fn from(value: (DOSDate, DOSTime)) -> Self

Converts to this type from the input type.
Source§

impl From<DOSDateTime> for [u8; 4]

Source§

fn from(value: DOSDateTime) -> Self

Converts to this type from the input type.
Source§

impl From<DOSDateTime> for u32

Source§

fn from(value: DOSDateTime) -> Self

Converts to this type from the input type.
Source§

impl FromBE<DOSDateTime> for [u8; 4]

Source§

fn from_be(value: DOSDateTime) -> Self

Converts the value into an instance of Self, where the value should be treated as big-endian.
Source§

impl FromLE<DOSDateTime> for [u8; 4]

Source§

fn from_le(value: DOSDateTime) -> Self

Converts the value into an instance of Self, where the value should be treated as little-endian.
Source§

impl PartialEq for DOSDateTime

Source§

fn eq(&self, other: &DOSDateTime) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<[u8; 4]> for DOSDateTime

Source§

type Error = DateTimeError

The type returned in the event of a conversion error.
Source§

fn try_from(value: [u8; 4]) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<u32> for DOSDateTime

Source§

type Error = DateTimeError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u32) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFromBE<[u8; 4]> for DOSDateTime

Source§

type Error = DateTimeError

The type returned in the event of a conversion error.
Source§

fn try_from_be(value: [u8; 4]) -> Result<Self, Self::Error>

Attempts to convert a value into an instance of Self, where the value should be treated as big-endian.
Source§

impl TryFromBE<u32> for DOSDateTime

Source§

type Error = DateTimeError

The type returned in the event of a conversion error.
Source§

fn try_from_be(value: u32) -> Result<Self, Self::Error>

Attempts to convert a value into an instance of Self, where the value should be treated as big-endian.
Source§

impl TryFromLE<[u8; 4]> for DOSDateTime

Source§

type Error = DateTimeError

The type returned in the event of a conversion error.
Source§

fn try_from_le(value: [u8; 4]) -> Result<Self, Self::Error>

Attempts to convert a value into an instance of Self, where the value should be treated as little-endian.
Source§

impl Copy for DOSDateTime

Source§

impl Eq for DOSDateTime

Source§

impl StructuralPartialEq for DOSDateTime

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> IntoBE<U> for T
where U: FromBE<T>,

Source§

fn into_be(self) -> U

Converts self into a value, where that value should be treated as big-endian.
Source§

impl<T, U> IntoLE<U> for T
where U: FromLE<T>,

Source§

fn into_le(self) -> U

Converts self into a value, where the value should be treated as little-endian.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T, U> TryIntoBE<U> for T
where U: TryFromBE<T>,

Source§

type Error = <U as TryFromBE<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into_be(self) -> Result<U, <T as TryIntoBE<U>>::Error>

Attempts to convert self into a value, where that value should be treated as big-endian.
Source§

impl<T, U> TryIntoLE<U> for T
where U: TryFromLE<T>,

Source§

type Error = <U as TryFromLE<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into_le(self) -> Result<U, <T as TryIntoLE<U>>::Error>

Attempts to convert self into a value, where that value should be treated as little-endian.