Skip to content

Commit 8b860c5

Browse files
committed
refactor(lib): use jiff instead of chrono
1 parent 6d23e2f commit 8b860c5

File tree

6 files changed

+24
-88
lines changed

6 files changed

+24
-88
lines changed

Cargo.lock

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

cli/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ name = "github-streak-stats"
99
path = "src/main.rs"
1010

1111
[dependencies]
12+
# Error handling
1213
anyhow = "1.0.86"
14+
1315
# Command line arguments parser
1416
clap = { version = "4.5.15", features = ["derive", "env", "wrap_help"] }
1517

cli/src/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ fn main() -> Result<()> {
136136
TableCell::new("Longest and latest streak"),
137137
TableCell::new(format!(
138138
"{} days, from {} to {}",
139-
(longest_streak.end - longest_streak.start).num_days() + 1,
139+
(longest_streak.end - longest_streak.start).get_days() + 1,
140140
longest_streak.start,
141141
longest_streak.end,
142142
)),
@@ -146,7 +146,7 @@ fn main() -> Result<()> {
146146
TableCell::new("Current streak"),
147147
TableCell::new(format!(
148148
"{} days, from {} to {}",
149-
(current_streak.end - current_streak.start).num_days() + 1,
149+
(current_streak.end - current_streak.start).get_days() + 1,
150150
current_streak.start,
151151
current_streak.end,
152152
)),

lib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ description = "A library for fetching GitHub streak stats."
66

77
[dependencies]
88
anyhow = "1.0"
9-
chrono = "0.4"
9+
jiff = "0.1.5"
1010
graphql_client = { version = "0.14", features = ["reqwest-blocking", "reqwest-rustls"] }
1111
# pin the version to v0.11 until https://github.com/graphql-rust/graphql-client/pull/490 is merged
1212
reqwest = { version = "0.11", features = ["blocking"] }

lib/src/github_client.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use std::env;
22

33
use anyhow::{anyhow, Result};
4-
use chrono::NaiveDate;
54
use graphql_client::{reqwest::post_graphql_blocking, GraphQLQuery, Response};
5+
use jiff::{civil::Date, fmt::strtime::parse, Span};
66
use reqwest::{
77
blocking::Client,
88
header::{HeaderValue, AUTHORIZATION},
@@ -62,10 +62,10 @@ impl GitHubClient {
6262
pub fn calc_streak_from_contributions(&self, contributions: &[Contribution]) -> Result<Stats> {
6363
let mut longest_streak = 0;
6464
let mut current_streak = 0;
65-
let mut longest_streak_start = NaiveDate::MIN;
66-
let mut longest_streak_end = NaiveDate::MIN;
67-
let mut current_streak_start = NaiveDate::MIN;
68-
let mut current_streak_end = NaiveDate::MIN;
65+
let mut longest_streak_start = Date::MIN;
66+
let mut longest_streak_end = Date::MIN;
67+
let mut current_streak_start = Date::MIN;
68+
let mut current_streak_end = Date::MIN;
6969
let mut total_contributions = 0;
7070

7171
for c in contributions.iter() {
@@ -75,7 +75,8 @@ impl GitHubClient {
7575
if current_streak >= longest_streak {
7676
longest_streak = current_streak;
7777
longest_streak_end = c.date;
78-
longest_streak_start = c.date - chrono::Duration::days(current_streak - 1);
78+
longest_streak_start =
79+
c.date.checked_sub(Span::new().days(current_streak - 1))?;
7980
}
8081
if current_streak == 1 {
8182
current_streak_start = c.date;
@@ -117,7 +118,11 @@ impl GitHubClient {
117118
.into_iter()
118119
.flat_map(|week| week.contribution_days)
119120
.map(|day| Contribution {
120-
date: NaiveDate::parse_from_str(&day.date, "%Y-%m-%d").unwrap(),
121+
date: parse("%Y-%m-%d%z", format!("{}+0000", &day.date))
122+
.unwrap()
123+
.to_zoned()
124+
.unwrap()
125+
.date(),
121126
contribution_count: day.contribution_count,
122127
})
123128
.collect::<Vec<_>>();

lib/src/types.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::fmt::Display;
22

3-
use chrono::NaiveDate;
43
use graphql_client::GraphQLQuery;
54

65
// Have to define custom type for DateTime and Date as these are not standard type
@@ -43,7 +42,7 @@ pub struct UserQuery;
4342
#[derive(Debug)]
4443
pub struct Contribution {
4544
/// Date of the contribution
46-
pub date: NaiveDate,
45+
pub date: jiff::civil::Date,
4746
/// Number of contributions on that date
4847
pub contribution_count: i64,
4948
}
@@ -74,14 +73,14 @@ impl Display for User {
7473
/// Simple date range
7574
pub struct Streak {
7675
/// Start date
77-
pub start: NaiveDate,
76+
pub start: jiff::civil::Date,
7877
/// End date
79-
pub end: NaiveDate,
78+
pub end: jiff::civil::Date,
8079
}
8180

82-
/// Converts a tuple of (NaiveDate, NaiveDate) to Streak
83-
impl From<(NaiveDate, NaiveDate)> for Streak {
84-
fn from(value: (NaiveDate, NaiveDate)) -> Self {
81+
/// Converts a tuple of (Date, Date) to Streak
82+
impl From<(jiff::civil::Date, jiff::civil::Date)> for Streak {
83+
fn from(value: (jiff::civil::Date, jiff::civil::Date)) -> Self {
8584
Self { start: value.0, end: value.1 }
8685
}
8786
}

0 commit comments

Comments
 (0)