Skip to content

Commit

Permalink
Fix #309
Browse files Browse the repository at this point in the history
This simple change fixes all reported invalid values.
  • Loading branch information
jhpratt committed Jan 25, 2021
1 parent b71597c commit 0540d19
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,12 @@ Versioning].

---

## 0.2.25 [2021-01-24]

### Fixed

- Fix #309, which can cause panics in certain situations.

## 0.2.24 [2021-01-08]

### Fixed
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "time"
version = "0.2.24"
version = "0.2.25"
authors = ["Jacob Pratt <the.z.cuber@gmail.com>"]
edition = "2018"
repository = "https://github.com/time-rs/time"
Expand Down
8 changes: 4 additions & 4 deletions src/date.rs
Expand Up @@ -542,11 +542,11 @@ impl Date {
};

let raw_weekday =
(day as i32 + (13 * (month as i32 + 1)) / 5 + adjusted_year + adjusted_year / 4
((day as i32 + (13 * (month as i32 + 1)) / 5 + adjusted_year + adjusted_year / 4
- adjusted_year / 100
+ adjusted_year / 400)
% 7
- 2;
- 2)
% 7;

if raw_weekday < 0 {
(raw_weekday + 7) as u8
Expand Down Expand Up @@ -586,7 +586,7 @@ impl Date {
6 => Weekday::Sunday,
// FIXME The compiler isn't able to optimize this away. See
// rust-lang/rust#66993.
_ => unreachable!("A value mod 7 is always in the range 0..7"),
n => unreachable!("A value mod 7 is always in the range 0..7 (was {})", n),
}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/date.rs
Expand Up @@ -1230,3 +1230,14 @@ fn previous_day_panics() {
fn julian_day_panics() {
Date::from_julian_day(i64::MAX);
}

#[test]
fn issue_309() {
assert_eq!(date!(-36 - 11 - 01).weekday(), Weekday::Sunday);
assert_eq!(date!(-26 - 96).weekday(), Weekday::Sunday);
assert_eq!(date!(-31 - 137).weekday(), Weekday::Sunday);
assert_eq!(date!(-31 - 137).week(), 20);
assert_eq!(date!(-60 - 63).iso_year_week(), (-60, 9));
assert_eq!(date!(-208 - 99).week(), 14);
assert_eq!(util::weeks_in_year(-102), 52);
}

0 comments on commit 0540d19

Please sign in to comment.