3 releases
0.1.2 | Jul 8, 2024 |
---|---|
0.1.1 | Jul 8, 2024 |
0.1.0 | Jul 8, 2024 |
#148 in Date and time
Used in rapid_solve
40KB
851 lines
RapidTime
This crate defines two types: DateTime
and
Duration
, which are useful to model times in
combinatorial optimization problems.
- The smallest unit is a second.
- In addtion to actual times,
DateTime::Earliest
andDateTime::Latest
represents plus and minus infinity, respectively. - Besides finite durations,
Duration::Infinity
represents an infinite duration. Durations
can be added to or subtracted fromDateTimes
.Durations
can be added to or subtracted from each other and implementSum
.DateTimes
can be subtracted from each other to produce aDuration
.Durations
andDateTimes
are total ordered. (They implementOrd
.)- No negative durations are allowed.
- Both types are
Copy
andClone
.
Usage
- Basic Usage
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);
- More
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
- More
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