#date-time #date #time #dos

no-std dostime

A Rust crate that converts to and from MS DOS dates and times

3 releases (1 stable)

new 1.0.0 Jan 1, 2025
0.2.0 Dec 31, 2024
0.1.0 Dec 30, 2024

#89 in Date and time

Download history 83/week @ 2024-12-24 325/week @ 2024-12-31

408 downloads per month

GPL-3.0-or-later

97KB
2K SLoC

dostime

This crate converts MS DOS times to and from various formats, including integers, byte arrays, and types in external crates (see the features for more information).

An explanation of the date and time formats can be found here, though the documentation for DOSTime and DOSDate also includes explantions for the format.

use dostime::{DOSDate, DOSTime, DOSDateTime};
use dostime::date::DateError;
use dostime::time::TimeError;
use dostime::datetime::DateTimeError;

// Valid dates and times can be constructed.
let date = DOSDate::new(2017, 4, 6).unwrap();           // 2017-04-06
let time = DOSTime::new(13, 24, 54).unwrap();           // 13:24:54
let datetime = DOSDateTime::new(2006, 4, 15, 1, 5, 34); // 2006-04-15 01:05:34

// Invalid dates and times can't be constructed and information about what is invalid is
// returned.
let invalid_month = DOSDate::new(1996, 13, 3).unwrap_err();
assert_eq!(invalid_month, DateError::InvalidMonth);

let invalid_second = DOSTime::new(1, 2, 60).unwrap_err();
assert_eq!(invalid_second, TimeError::InvalidSecond);

let invalid_day = DOSDateTime::new(2001, 4, 31, 6, 13, 12).unwrap_err();
assert_eq!(invalid_day, DateTimeError::DateError(DateError::InvalidDay));

// Dates and times can be converted to and from integers and arrays of bytes.
let int: u16 = date.into();
assert_eq!(int, 0x4A86);
assert_eq!(date, DOSDate::try_from(0x4A86).unwrap());

let bytes: [u8; 2] = time.into();
assert_eq!(bytes, [0x1B, 0x6B]);
assert_eq!(time, DOSTime::try_from([0x1B, 0x6B]).unwrap());

Features

  • std (default) - enables features that rely on the standard library
  • serde-1 - enables (de)serialization with Serde
  • time-1 - enables conversion to/from types in the time crate (Date, Time, and PrimitiveDateTime)
  • chrono-1 - enables conversion to/from types in the chrono crate (NaiveDate, NaiveTime, and NaiveDateTime)

Future Work

If anyone thinks of something else that this crate should have or spots any bugs, let me know! I'll see if I can add it. Alternatively, feel free to submit a pull request (or just fork it). I'll review and approve it as soon as I can.

Dependencies

~0–610KB
~11K SLoC