Skip to main content

DateTime

Struct DateTime 

Source
pub struct DateTime {
    pub year: u16,
    pub month: u8,
    pub day: u8,
    pub hour: u8,
    pub minute: u8,
    pub second: u8,
}
Expand description

Date and time structure for MTP/PTP.

Format: “YYYYMMDDThhmmss” (ISO 8601 subset)

§Validation

DateTime values must satisfy these constraints:

  • Year: 0-9999 (4-digit representation)
  • Month: 1-12
  • Day: 1-31
  • Hour: 0-23
  • Minute: 0-59
  • Second: 0-59

Use DateTime::new() to create validated instances, or DateTime::is_valid() to check existing instances.

Fields§

§year: u16

Year (0-9999)

§month: u8

Month (1-12)

§day: u8

Day (1-31)

§hour: u8

Hour (0-23)

§minute: u8

Minute (0-59)

§second: u8

Second (0-59)

Implementations§

Source§

impl DateTime

Source

pub fn new( year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Option<Self>

Create a new DateTime with validation.

Returns None if any value is out of range.

§Example
use mtp_rs::ptp::DateTime;

let dt = DateTime::new(2024, 3, 15, 14, 30, 22).unwrap();
assert_eq!(dt.year, 2024);

// Invalid values return None
assert!(DateTime::new(2024, 13, 1, 0, 0, 0).is_none()); // month > 12
assert!(DateTime::new(2024, 1, 1, 0, 60, 0).is_none()); // minute > 59
Source

pub fn is_valid(&self) -> bool

Check if this DateTime has valid values.

Returns true if all fields are within valid ranges:

  • Year: 0-9999
  • Month: 1-12
  • Day: 1-31
  • Hour: 0-23
  • Minute: 0-59
  • Second: 0-59

Note: This does not validate day-of-month against the specific month (e.g., Feb 31 would pass). MTP devices generally accept any 1-31 value.

Source

pub fn parse(s: &str) -> Option<Self>

Parse a datetime string in MTP format.

Format: “YYYYMMDDThhmmss” with optional timezone suffix (Z or +hhmm/-hhmm). The timezone suffix is parsed but ignored.

Returns None if the string is malformed or contains invalid values.

Source

pub fn format(&self) -> Option<String>

Format the datetime as an MTP string.

Returns Some("YYYYMMDDThhmmss") if the values are valid (exactly 15 characters), or None if any value is out of range.

§Example
use mtp_rs::ptp::DateTime;

let dt = DateTime::new(2024, 3, 15, 14, 30, 22).unwrap();
assert_eq!(dt.format(), Some("20240315T143022".to_string()));

// Invalid DateTime returns None
let invalid = DateTime { year: 2024, month: 13, day: 1, hour: 0, minute: 0, second: 0 };
assert_eq!(invalid.format(), None);

Trait Implementations§

Source§

impl Clone for DateTime

Source§

fn clone(&self) -> DateTime

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 DateTime

Source§

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

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

impl Default for DateTime

Source§

fn default() -> DateTime

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

impl PartialEq for DateTime

Source§

fn eq(&self, other: &DateTime) -> 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 Copy for DateTime

Source§

impl Eq for DateTime

Source§

impl StructuralPartialEq for DateTime

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.