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: u16Year (0-9999)
month: u8Month (1-12)
day: u8Day (1-31)
hour: u8Hour (0-23)
minute: u8Minute (0-59)
second: u8Second (0-59)
Implementations§
Source§impl DateTime
impl DateTime
Sourcepub fn new(
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Option<Self>
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 > 59Sourcepub fn is_valid(&self) -> bool
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.
Sourcepub fn parse(s: &str) -> Option<Self>
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.
Sourcepub fn format(&self) -> Option<String>
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);