Transformed all UTCs to Local and fixed the 1 day offset issue

master
Wynd 2024-08-16 02:27:50 +03:00
parent 1bb310a4a3
commit de4143ed81
2 changed files with 82 additions and 72 deletions

View File

@ -1,20 +1,24 @@
use std::collections::BTreeMap; use std::collections::BTreeMap;
use chrono::{DateTime, Datelike, Duration, Utc}; use chrono::{DateTime, Datelike, Duration, Local, Utc};
use clap::ValueEnum; use clap::ValueEnum;
use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET}; use crate::{get_char, get_color, get_color_map, Commit, DAYS, RESET};
pub struct Heatmap { pub struct Heatmap {
data: [Vec<u32>; 7], data: [Vec<u32>; 7],
start_date: DateTime<Utc>, start_date: DateTime<Local>,
end_date: DateTime<Utc>, end_date: DateTime<Local>,
commits: Vec<Commit>, commits: Vec<Commit>,
months: Vec<(usize, String)>, months: Vec<(usize, String)>,
} }
impl Heatmap { impl Heatmap {
pub fn new(start_date: DateTime<Utc>, end_date: DateTime<Utc>, commits: Vec<Commit>) -> Self { pub fn new(
start_date: DateTime<Local>,
end_date: DateTime<Local>,
commits: Vec<Commit>,
) -> Self {
let mut heatmap = Self { let mut heatmap = Self {
data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]], data: [vec![], vec![], vec![], vec![], vec![], vec![], vec![]],
start_date, start_date,
@ -26,13 +30,13 @@ impl Heatmap {
let mut grouped_commits = BTreeMap::new(); let mut grouped_commits = BTreeMap::new();
for commit in &heatmap.commits { 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); let record = grouped_commits.entry(commit_day).or_insert(0);
*record += 1; *record += 1;
} }
let mut current_day = start_date; 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 { if day_of_week != 0 {
for i in 0..day_of_week { for i in 0..day_of_week {
@ -52,14 +56,14 @@ impl Heatmap {
.push((heatmap.data[day_of_week as usize].len(), month_name)); .push((heatmap.data[day_of_week as usize].len(), month_name));
} }
let value = grouped_commits.get(&current_day.ordinal0()); let value = grouped_commits.get(&current_day.date_naive());
match value { match value {
Some(val) => heatmap.data[day_of_week as usize].push(*val), Some(val) => heatmap.data[day_of_week as usize].push(*val),
None => heatmap.data[day_of_week as usize].push(0), None => heatmap.data[day_of_week as usize].push(0),
} }
// println!("{} {value:?}", current_day.date_naive());
current_day += Duration::days(1); current_day += Duration::days(1);
day_of_week = (current_day.weekday().num_days_from_monday() + 1) % 7; day_of_week = current_day.weekday().num_days_from_monday() % 7;
// println!("{} - {current_day} | {value:?}", current_day.ordinal0());
} }
heatmap heatmap

View File

@ -5,7 +5,7 @@
use std::{cmp::Reverse, sync::OnceLock}; use std::{cmp::Reverse, sync::OnceLock};
use anyhow::{Context, Result}; 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 clap::Parser;
use gix::{bstr::ByteSlice, ObjectId, Repository}; use gix::{bstr::ByteSlice, ObjectId, Repository};
use heatmap::HeatmapColors; use heatmap::HeatmapColors;
@ -43,7 +43,7 @@ struct Commit {
id: ObjectId, id: ObjectId,
title: String, title: String,
author: String, author: String,
time: DateTime<Utc>, time: DateTime<Local>,
} }
fn main() -> Result<()> { fn main() -> Result<()> {
@ -64,7 +64,7 @@ fn main() -> Result<()> {
let repo = gix::open(&args.input).unwrap(); 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 start_date = end_date - Duration::days(365);
let commits = let commits =
@ -106,7 +106,11 @@ fn get_color_map() -> Vec<String> {
.to_vec() .to_vec()
} }
fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime<Utc>) -> Result<Vec<Commit>> { fn get_commits(
repo: Repository,
args: CliArgs,
start_date: DateTime<Local>,
) -> Result<Vec<Commit>> {
let mut commits: Vec<Commit> = repo let mut commits: Vec<Commit> = repo
.head()? .head()?
.into_peeled_id()? .into_peeled_id()?
@ -130,7 +134,9 @@ fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime<Utc>) -> Re
} }
let time = c.time().ok()?; 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) { if time <= start_date + Duration::days(1) {
return None; return None;
} }
@ -149,61 +155,61 @@ fn get_commits(repo: Repository, args: CliArgs, start_date: DateTime<Utc>) -> Re
Ok(commits) Ok(commits)
} }
fn print_streak(commits: Vec<Commit>, args: CliArgs) { // fn print_streak(commits: Vec<Commit>) {
let mut commits_pushed = 0; // let mut commits_pushed = 0;
let mut days_streak = 0; // let mut days_streak = 0;
let mut last_date: Option<DateTime<Utc>> = None; // let mut last_date: Option<DateTime<Utc>> = None;
let mut start_date: Option<DateTime<Utc>> = None; // let mut start_date: Option<DateTime<Utc>> = None;
let mut end_date: Option<DateTime<Utc>> = None; // let mut end_date: Option<DateTime<Utc>> = None;
//
for commit in commits { // for commit in commits {
match last_date { // match last_date {
Some(date) => { // Some(date) => {
let day = date.ordinal0(); // let day = date.ordinal0();
let commit_day = commit.time.ordinal0(); // let commit_day = commit.time.ordinal0();
let time_diff = date - commit.time; // let time_diff = date - commit.time;
//
if commit_day != day { // if commit_day != day {
days_streak += 1; // days_streak += 1;
} // }
//
// if time_diff.num_hours() >= args.split.into() { // if time_diff.num_hours() >= 24 {
// println!( // // println!(
// "Failing Commit\n{} - {} {} hours difference", // // "Failing Commit\n{} - {} {} hours difference",
// commit.id, // // commit.id,
// commit.time.to_rfc3339(), // // commit.time.to_rfc3339(),
// time_diff.num_hours() // // time_diff.num_hours()
// ); // // );
// break; // break;
// } // }
//
commits_pushed += 1; // commits_pushed += 1;
last_date = Some(commit.time); // last_date = Some(commit.time);
end_date = Some(commit.time); // end_date = Some(commit.time);
//
println!( // // println!(
"{} - {} {} hours difference", // // "{} - {} {} hours difference",
commit.id, // // commit.id,
commit.time.to_rfc3339(), // // commit.time.to_rfc3339(),
time_diff.num_hours() // // time_diff.num_hours()
); // // );
} // }
None => { // None => {
last_date = Some(commit.time); // last_date = Some(commit.time);
start_date = Some(commit.time); // start_date = Some(commit.time);
println!("First Commit\n{} - {}", commit.id, commit.time.to_rfc3339()); // // println!("First Commit\n{} - {}", commit.id, commit.time.to_rfc3339());
continue; // continue;
} // }
} // }
} // }
//
println!("{commits_pushed} commits pushed during a {days_streak} days streak"); // println!("{commits_pushed} commits pushed during a {days_streak} days streak");
if let Some(start_date) = start_date // if let Some(start_date) = start_date
&& let Some(end_date) = end_date // && let Some(end_date) = end_date
{ // {
let start_date = start_date.format("%d-%b-%Y").to_string(); // let start_date = start_date.format("%d-%b-%Y").to_string();
let end_date = end_date.format("%d-%b-%Y").to_string(); // let end_date = end_date.format("%d-%b-%Y").to_string();
//
println!("Between: {start_date} {end_date}"); // println!("Between: {start_date} {end_date}");
} // }
} // }