From de4143ed812fb24b5ce966f7ba37593a1a52a036 Mon Sep 17 00:00:00 2001 From: Wynd Date: Fri, 16 Aug 2024 02:27:50 +0300 Subject: [PATCH] Transformed all UTCs to Local and fixed the 1 day offset issue --- src/heatmap.rs | 22 +++++---- src/main.rs | 132 ++++++++++++++++++++++++++----------------------- 2 files changed, 82 insertions(+), 72 deletions(-) diff --git a/src/heatmap.rs b/src/heatmap.rs index e7c7c75..dd65e86 100644 --- a/src/heatmap.rs +++ b/src/heatmap.rs @@ -1,20 +1,24 @@ use std::collections::BTreeMap; -use chrono::{DateTime, Datelike, Duration, Utc}; +use chrono::{DateTime, Datelike, Duration, Local, Utc}; use clap::ValueEnum; use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET}; pub struct Heatmap { data: [Vec; 7], - start_date: DateTime, - end_date: DateTime, + start_date: DateTime, + end_date: DateTime, commits: Vec, months: Vec<(usize, String)>, } impl Heatmap { - pub fn new(start_date: DateTime, end_date: DateTime, commits: Vec) -> Self { + pub fn new( + start_date: DateTime, + end_date: DateTime, + commits: Vec, + ) -> Self { let mut heatmap = Self { data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]], start_date, @@ -26,13 +30,13 @@ impl Heatmap { let mut grouped_commits = BTreeMap::new(); for commit in &heatmap.commits { - let commit_day = commit.time.ordinal0(); + let commit_day = commit.time.date_naive(); let record = grouped_commits.entry(commit_day).or_insert(0); *record += 1; } let mut current_day = start_date; - let mut day_of_week = (current_day.weekday().num_days_from_monday() + 1) % 7; + let mut day_of_week = (current_day.weekday().num_days_from_monday()) % 7; if day_of_week != 0 { for i in 0..day_of_week { @@ -52,14 +56,14 @@ impl Heatmap { .push((heatmap.data[day_of_week as usize].len(), month_name)); } - let value = grouped_commits.get(¤t_day.ordinal0()); + let value = grouped_commits.get(¤t_day.date_naive()); match value { Some(val) => heatmap.data[day_of_week as usize].push(*val), None => heatmap.data[day_of_week as usize].push(0), } + // println!("{} {value:?}", current_day.date_naive()); current_day += Duration::days(1); - day_of_week = (current_day.weekday().num_days_from_monday() + 1) % 7; - // println!("{} - {current_day} | {value:?}", current_day.ordinal0()); + day_of_week = current_day.weekday().num_days_from_monday() % 7; } heatmap diff --git a/src/main.rs b/src/main.rs index 7e54591..b869ad5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,7 +5,7 @@ use std::{cmp::Reverse, sync::OnceLock}; use anyhow::{Context, Result}; -use chrono::{DateTime, Datelike, Duration, Utc}; +use chrono::{Date, DateTime, Datelike, Duration, Local, NaiveDate, Offset, TimeZone, Utc}; use clap::Parser; use gix::{bstr::ByteSlice, ObjectId, Repository}; use heatmap::HeatmapColors; @@ -43,7 +43,7 @@ struct Commit { id: ObjectId, title: String, author: String, - time: DateTime, + time: DateTime, } fn main() -> Result<()> { @@ -64,7 +64,7 @@ fn main() -> Result<()> { let repo = gix::open(&args.input).unwrap(); - let end_date = Utc::now() - Duration::days(1); + let end_date = Local::now(); let start_date = end_date - Duration::days(365); let commits = @@ -106,7 +106,11 @@ fn get_color_map() -> Vec { .to_vec() } -fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime) -> Result> { +fn get_commits( + repo: Repository, + args: CliArgs, + start_date: DateTime, +) -> Result> { let mut commits: Vec = repo .head()? .into_peeled_id()? @@ -130,7 +134,9 @@ fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime) -> Re } let time = c.time().ok()?; - let time = DateTime::from_timestamp_millis(time.seconds * 1000)?; + // let offset = Local.timestamp_opt(0, 0).unwrap().offset().fix(); + // let time = Local::now() - Duration::seconds(time.seconds); + let time = DateTime::from_timestamp_millis(time.seconds * 1000)?.with_timezone(&Local); if time <= start_date + Duration::days(1) { return None; } @@ -149,61 +155,61 @@ fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime) -> Re Ok(commits) } -fn print_streak(commits: Vec, args: CliArgs) { - let mut commits_pushed = 0; - let mut days_streak = 0; - let mut last_date: Option> = None; - let mut start_date: Option> = None; - let mut end_date: Option> = None; - - for commit in commits { - match last_date { - Some(date) => { - let day = date.ordinal0(); - let commit_day = commit.time.ordinal0(); - let time_diff = date - commit.time; - - if commit_day != day { - days_streak += 1; - } - - // if time_diff.num_hours() >= args.split.into() { - // println!( - // "Failing Commit\n{} - {} {} hours difference", - // commit.id, - // commit.time.to_rfc3339(), - // time_diff.num_hours() - // ); - // break; - // } - - commits_pushed += 1; - last_date = Some(commit.time); - end_date = Some(commit.time); - - println!( - "{} - {} {} hours difference", - commit.id, - commit.time.to_rfc3339(), - time_diff.num_hours() - ); - } - None => { - last_date = Some(commit.time); - start_date = Some(commit.time); - println!("First Commit\n{} - {}", commit.id, commit.time.to_rfc3339()); - continue; - } - } - } - - println!("{commits_pushed} commits pushed during a {days_streak} days streak"); - if let Some(start_date) = start_date - && let Some(end_date) = end_date - { - let start_date = start_date.format("%d-%b-%Y").to_string(); - let end_date = end_date.format("%d-%b-%Y").to_string(); - - println!("Between: {start_date} {end_date}"); - } -} +// fn print_streak(commits: Vec) { +// let mut commits_pushed = 0; +// let mut days_streak = 0; +// let mut last_date: Option> = None; +// let mut start_date: Option> = None; +// let mut end_date: Option> = None; +// +// for commit in commits { +// match last_date { +// Some(date) => { +// let day = date.ordinal0(); +// let commit_day = commit.time.ordinal0(); +// let time_diff = date - commit.time; +// +// if commit_day != day { +// days_streak += 1; +// } +// +// if time_diff.num_hours() >= 24 { +// // println!( +// // "Failing Commit\n{} - {} {} hours difference", +// // commit.id, +// // commit.time.to_rfc3339(), +// // time_diff.num_hours() +// // ); +// break; +// } +// +// commits_pushed += 1; +// last_date = Some(commit.time); +// end_date = Some(commit.time); +// +// // println!( +// // "{} - {} {} hours difference", +// // commit.id, +// // commit.time.to_rfc3339(), +// // time_diff.num_hours() +// // ); +// } +// None => { +// last_date = Some(commit.time); +// start_date = Some(commit.time); +// // println!("First Commit\n{} - {}", commit.id, commit.time.to_rfc3339()); +// continue; +// } +// } +// } +// +// println!("{commits_pushed} commits pushed during a {days_streak} days streak"); +// if let Some(start_date) = start_date +// && let Some(end_date) = end_date +// { +// let start_date = start_date.format("%d-%b-%Y").to_string(); +// let end_date = end_date.format("%d-%b-%Y").to_string(); +// +// println!("Between: {start_date} {end_date}"); +// } +// }