Skip to content
This repository has been archived by the owner on Feb 19, 2023. It is now read-only.

Commit

Permalink
Text equal and max between
Browse files Browse the repository at this point in the history
  • Loading branch information
samueldple committed Oct 18, 2019
1 parent 9467968 commit 38ef6b5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/time_tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ impl Duration {
/// with a minute value of 90 would add an hour to the resulting tuple
/// and set the minutes to 30, for example.
pub fn new(h: u32, m: u32, s: u32) -> Duration {
let total_seconds = s + 60 * m + 3600 * h;
Duration::from_seconds(u64::from(total_seconds))
let total_seconds: u64 = u64::from(s) + 60 * u64::from(m) + 3600 * u64::from(h);
Duration::from_seconds(total_seconds)
}

/// Same as `Duration::new()` but takes the total number of seconds
Expand Down Expand Up @@ -271,7 +271,7 @@ impl Duration {
smaller = dt2;
greater = dt1;
}
let days_between = greater.get_date().to_days() - greater.get_date().to_days();
let days_between = greater.get_date().to_days() - smaller.get_date().to_days();
let time_between = if days_between == 0 {
Duration::from(greater.get_time()) - Duration::from(smaller.get_time())
} else {
Expand Down
25 changes: 25 additions & 0 deletions tests/date_time_tuple_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ extern crate date_time;

use date_time::date_time_tuple::DateTimeTuple;
use date_time::date_tuple::DateTuple;
use date_time::time_tuple::Duration;
use date_time::time_tuple::TimeTuple;

#[test]
Expand Down Expand Up @@ -67,3 +68,27 @@ fn test_from_string() {
assert!(str::parse::<DateTimeTuple>("2000-15-10@08:30:00").is_err());
assert!(str::parse::<DateTimeTuple>("2-a11111@05:a:04").is_err());
}

#[test]
fn test_between_equal() {
assert_eq!(
Duration::new(0, 0, 0),
Duration::between(
DateTimeTuple::new(DateTuple::new(1, 2, 3).unwrap(), TimeTuple::new(4, 5, 6)),
DateTimeTuple::new(DateTuple::new(1, 2, 3).unwrap(), TimeTuple::new(4, 5, 6))
)
);
}

#[test]
fn test_between_max_cant_overflow() {
// Must not panic
Duration::between(
DateTimeTuple::new(DateTuple::new(0, 1, 1).unwrap(), TimeTuple::new(0, 0, 0)),
DateTimeTuple::new(
DateTuple::new(9999, 12, 31).unwrap(),
TimeTuple::new(23, 59, 59),
),
)
.to_seconds();
}

0 comments on commit 38ef6b5

Please sign in to comment.