Skip to content

Commit 0540d19

Browse files
committed
Fix #309
This simple change fixes all reported invalid values.
1 parent b71597c commit 0540d19

File tree

4 files changed

+22
-5
lines changed

4 files changed

+22
-5
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ Versioning].
77

88
---
99

10+
## 0.2.25 [2021-01-24]
11+
12+
### Fixed
13+
14+
- Fix #309, which can cause panics in certain situations.
15+
1016
## 0.2.24 [2021-01-08]
1117

1218
### Fixed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "time"
3-
version = "0.2.24"
3+
version = "0.2.25"
44
authors = ["Jacob Pratt <[email protected]>"]
55
edition = "2018"
66
repository = "https://github.com/time-rs/time"

src/date.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -542,11 +542,11 @@ impl Date {
542542
};
543543

544544
let raw_weekday =
545-
(day as i32 + (13 * (month as i32 + 1)) / 5 + adjusted_year + adjusted_year / 4
545+
((day as i32 + (13 * (month as i32 + 1)) / 5 + adjusted_year + adjusted_year / 4
546546
- adjusted_year / 100
547547
+ adjusted_year / 400)
548-
% 7
549-
- 2;
548+
- 2)
549+
% 7;
550550

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

tests/date.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1230,3 +1230,14 @@ fn previous_day_panics() {
12301230
fn julian_day_panics() {
12311231
Date::from_julian_day(i64::MAX);
12321232
}
1233+
1234+
#[test]
1235+
fn issue_309() {
1236+
assert_eq!(date!(-36 - 11 - 01).weekday(), Weekday::Sunday);
1237+
assert_eq!(date!(-26 - 96).weekday(), Weekday::Sunday);
1238+
assert_eq!(date!(-31 - 137).weekday(), Weekday::Sunday);
1239+
assert_eq!(date!(-31 - 137).week(), 20);
1240+
assert_eq!(date!(-60 - 63).iso_year_week(), (-60, 9));
1241+
assert_eq!(date!(-208 - 99).week(), 14);
1242+
assert_eq!(util::weeks_in_year(-102), 52);
1243+
}

0 commit comments

Comments
 (0)