DateTime#

A naive local date-time at minute precision, with no timezone and no DST. Every date the API accepts or returns is a DateTime, for example the dates in TaskData and SchedulingConfig. Build one with DateTime::from_ymd_hm or convert it from a chrono::NaiveDateTime.

Declaration#

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DateTime(/* private */);

impl Display for DateTime
impl From<chrono::NaiveDateTime> for DateTime
impl From<DateTime> for chrono::NaiveDateTime

The inner value is private. DateTime doesn't implement Default or FromStr. With the serde feature it also implements Serialize and Deserialize; see Serialization.

Values compare by time: an earlier date-time is less than a later one.

Associated functions#

from_ymd_hm#

pub fn from_ymd_hm(y: i32, m: u32, d: u32, h: u32, min: u32) -> Option<DateTime>

Builds a date-time from calendar fields.

Parameters

Name Type Description
y i32 Year.
m u32 Month, 1..=12.
d u32 Day of the month, checked against the month and leap years.
h u32 Hour, 0..=23.
min u32 Minute, 0..=59.

Returns - Some(DateTime), or None when the fields don't form a valid date and time (for example February 30 or hour 24).

Methods#

minutes#

pub fn minutes(self) -> i64

Minutes since 1970-01-01 00:00, counted as naive time. Dates before 1970 give negative values. The difference of two minutes() values is the elapsed wall-clock time between them; no working calendar is applied.

Returns - the minute count.

Conversions#

From<chrono::NaiveDateTime>#

impl From<chrono::NaiveDateTime> for DateTime

Truncates seconds and sub-seconds: 2026-03-02 08:15:59 becomes 2026-03-02 08:15. plen-core doesn't re-export chrono, so add it to your own dependencies (cargo add chrono) to use this conversion.

From<DateTime> for chrono::NaiveDateTime#

impl From<DateTime> for chrono::NaiveDateTime

Returns the same date and time with seconds set to 0.

Display#

Display writes YYYY-MM-DD HH:MM, for example 2026-03-02 08:00.

Serialization#

Requires the serde feature:

cargo add plen-core --features serde
  • Serialize writes the Display string, "YYYY-MM-DD HH:MM".
  • Deserialize reads a string, trims surrounding whitespace and accepts YYYY-MM-DD HH:MM, YYYY-MM-DD HH:MM:SS, the same two forms with T instead of the space, and a bare date YYYY-MM-DD (read as 00:00). Seconds are truncated. Any other string fails with the error message invalid date.

Example#

use plen_core::DateTime;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let start = DateTime::from_ymd_hm(2026, 3, 2, 8, 0).ok_or("invalid date")?;
    let end = DateTime::from_ymd_hm(2026, 3, 4, 17, 0).ok_or("invalid date")?;

    assert!(start < end);
    println!("{start} -> {end}"); // 2026-03-02 08:00 -> 2026-03-04 17:00

    let elapsed_hours = (end.minutes() - start.minutes()) / 60;
    println!("{elapsed_hours} h"); // 57 h

    assert!(DateTime::from_ymd_hm(2026, 2, 30, 8, 0).is_none());
    Ok(())
}
  • TaskData - task dates and constraint dates are DateTime values
  • SchedulingConfig - project start and end are DateTime values
  • Mutation - returns computed task dates as DateTime
  • Quick start - builds dates with from_ymd_hm in a first schedule
  • Project data - how dates are read in an input snapshot