Skip to content

Commit cf3b798

Browse files
committed
refactor: port chrono to jiff
jiff API is much more cleaner.
1 parent c166b77 commit cf3b798

File tree

9 files changed

+69
-251
lines changed

9 files changed

+69
-251
lines changed

Cargo.lock

-161
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ lto = true
1818
panic = 'abort'
1919

2020
[dependencies]
21-
chrono = "0.4.38"
21+
jiff = "0.1.4"
2222
owo-colors = "4"
2323

2424
# cli
2525
clap = { version = "4.5.13", features = ["suggestions", "color", "cargo", "derive"] }
2626

2727
# error
2828
anyhow = "1.0"
29-
jiff = "0.1.4"
3029
thiserror = "1.0.63"
3130

3231
[dev-dependencies]

src/error.rs

+6
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,9 @@ pub enum Error {
88
#[error("No such time")]
99
InvalidTime,
1010
}
11+
12+
impl std::convert::From<jiff::Error> for Error {
13+
fn from(err: jiff::Error) -> Self {
14+
Error::InvalidArgument(err.to_string())
15+
}
16+
}

src/lib.rs

-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,3 @@ pub mod output;
44
pub mod progress;
55

66
pub use error::Error;
7-
8-
// Use internal type. Chrono API changes very often
9-
pub type Date = chrono::NaiveDate;
10-
pub type DateTime = chrono::NaiveDateTime;

src/progress/mod.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,28 @@ pub use month::month;
55
pub mod week;
66
pub use week::week;
77

8-
use chrono::{Local, NaiveDate};
8+
use jiff::civil;
9+
use jiff::Unit;
10+
use jiff::Zoned;
911

10-
use crate::{error::Error, Date};
12+
use crate::error::Error;
1113

1214
// Calculate the ratio of time progress
13-
fn compute(current: Date, start: Date, end: Date) -> Result<f64, Error> {
15+
fn compute(current: civil::Date, start: civil::Date, end: civil::Date) -> Result<f64, Error> {
1416
let whole_diff = end - start;
1517
// 86_400 is total second in a day (24 * 3600)
16-
let total_seconds_in_day = 84_000;
17-
let whole_diff_in_seconds =
18-
whole_diff.num_days() * total_seconds_in_day + whole_diff.num_seconds();
18+
let total_seconds_in_day = 84_000.0;
19+
let whole_diff_in_seconds = whole_diff.total(Unit::Day)? * total_seconds_in_day
20+
+ whole_diff.total(Unit::Millisecond)?;
1921

2022
let current_diff = current - start;
21-
let current_diff_in_seconds =
22-
current_diff.num_days() * total_seconds_in_day + current_diff.num_seconds();
23+
let current_diff_in_seconds = current_diff.total(Unit::Day)? * total_seconds_in_day
24+
+ current_diff.total(Unit::Millisecond)?;
2325
// progress_ratio
24-
let ratio = current_diff_in_seconds as f64 / whole_diff_in_seconds as f64;
26+
let ratio = current_diff_in_seconds / whole_diff_in_seconds;
2527
Ok(ratio)
2628
}
2729

28-
pub fn today() -> Date {
29-
Local::now().date_naive()
30-
}
31-
32-
pub fn date(year: i32, month: u32, day: u32) -> Result<Date, crate::Error> {
33-
NaiveDate::from_ymd_opt(year, month, day).ok_or(crate::Error::InvalidTime)
30+
pub fn today() -> civil::Date {
31+
Zoned::now().date()
3432
}

0 commit comments

Comments
 (0)