Crates.io | rapid_time |
lib.rs | rapid_time |
version | 0.1.2 |
source | src |
created_at | 2024-07-08 20:04:20.219172 |
updated_at | 2024-07-08 21:53:14.265551 |
description | A library for datetimes and durations, which are useful to model times in combinatorial optimization problems. |
homepage | |
repository | https://github.com/LeonSering/rapid_time |
max_upload_size | |
id | 1296329 |
size | 39,195 |
This crate defines two types: DateTime
and
Duration
, which are useful to model times in
combinatorial optimization problems.
DateTime::Earliest
and DateTime::Latest
represents plus and minus infinity, respectively.Duration::Infinity
represents an infinite duration.Durations
can be added to or subtracted from DateTimes
.Durations
can be added to or subtracted from each other and implement
Sum
.DateTimes
can be subtracted from each other to produce a
Duration
.Durations
and DateTimes
are total ordered. (They implement
Ord
.)Copy
and Clone
.use rapid_time::{DateTime, Duration};
let tour_start = DateTime::new("2024-02-28T08:00:00");
let tour_length = Duration::new("100:00:00");
let tour_end = DateTime::new("2024-03-03T12:00:00");
assert_eq!(tour_start + tour_length, tour_end);
assert_eq!(tour_end - tour_start, tour_length);
assert_eq!(tour_end - tour_length, tour_start);
// Note that 2024 is a leap year.
# use rapid_time::{DateTime, Duration};
assert_eq!(DateTime::Earliest + Duration::new("10000:00:00"), DateTime::Earliest);
assert_eq!(DateTime::new("0000-01-01T00:00:00") + Duration::Infinity, DateTime::Latest);
assert_eq!(DateTime::Latest - DateTime::Earliest, Duration::Infinity);
assert_eq!(DateTime::Earliest + Duration::Infinity, DateTime::Latest);
Duration
# use rapid_time::{DateTime, Duration};
assert_eq!(Duration::new("1:00:00") + Duration::from_seconds(120), Duration::new("1:02:00"));
assert_eq!(Duration::new("100:00:00").in_sec().unwrap(), 100 * 3600);
assert_eq!(Duration::from_iso("P10DT2H00M59S").in_min().unwrap(), 10 * 24 * 60 + 2 * 60);
// Duration::from_seconds(10) - Duration::from_seconds(20); // panics
DateTime
# use rapid_time::{DateTime, Duration};
assert_eq!(DateTime::new("2024-02-28T08:30").as_iso(), "2024-02-28T08:30:00");
// DateTime::new("2024-01-01T08:00") - DateTime::new("2024-01-01T09:00"); // panics