Skip to content

Commit e72ddcb

Browse files
committed
Default to the next puzzle in cargo x wait
1 parent 77edc50 commit e72ddcb

File tree

2 files changed

+49
-7
lines changed

2 files changed

+49
-7
lines changed

crates/utils/src/date.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,43 @@ impl Date {
3434
pub fn release_time(&self) -> SystemTime {
3535
SystemTime::UNIX_EPOCH + Duration::from_secs(self.release_timestamp())
3636
}
37+
38+
/// The [`Date`] of the next puzzle.
39+
#[must_use]
40+
#[expect(clippy::cast_possible_truncation)]
41+
pub fn next_puzzle() -> Option<Date> {
42+
let now = SystemTime::now()
43+
.duration_since(SystemTime::UNIX_EPOCH)
44+
.unwrap()
45+
.as_secs();
46+
47+
let mut date = Date {
48+
year: Year(2015),
49+
day: Day(1),
50+
};
51+
52+
// Skip ahead whole years
53+
if now > Self::FIRST_RELEASE_TIMESTAMP {
54+
let year = 2015 + ((now - Self::FIRST_RELEASE_TIMESTAMP) / 60 / 60 / 24 / 366);
55+
if year > 9999 {
56+
return None;
57+
}
58+
date.year = Year(year as u16);
59+
}
60+
61+
while date.release_timestamp() < now {
62+
if date.day.0 < 25 {
63+
date.day.0 += 1;
64+
} else if date.year.0 < 9999 {
65+
date.year.0 += 1;
66+
date.day.0 = 1;
67+
} else {
68+
return None;
69+
}
70+
}
71+
72+
Some(date)
73+
}
3774
}
3875

3976
impl Display for Date {

crates/xtask/src/cmd/wait.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ use utils::date::Date;
77
const ANSI_LINE_START: &str = "\x1B[0G";
88
const ANSI_CLEAR_LINE: &str = "\x1B[2K";
99

10-
pub fn main(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
11-
let year = crate::year_arg(&mut args)?;
12-
let day = crate::day_arg(&mut args)?;
13-
crate::ensure_no_args(args)?;
14-
15-
let date = Date { year, day };
10+
pub fn main(args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>> {
11+
let mut args = args.peekable();
12+
13+
let date = if args.peek().is_none() {
14+
Date::next_puzzle().unwrap()
15+
} else {
16+
let year = crate::year_arg(&mut args)?;
17+
let day = crate::day_arg(&mut args)?;
18+
crate::ensure_no_args(args)?;
19+
Date { year, day }
20+
};
1621
let release = date.release_time();
1722

1823
let mut previous = (0, 0, 0, 61);
@@ -42,5 +47,5 @@ pub fn main(mut args: impl Iterator<Item = String>) -> Result<(), Box<dyn Error>
4247

4348
println!("{ANSI_LINE_START}{ANSI_CLEAR_LINE}{date} is released!");
4449

45-
super::input::download(year, day)
50+
super::input::download(date.year, date.day)
4651
}

0 commit comments

Comments
 (0)