Skip to content

Using time crate instead of chrono #533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ anyhow = "1.0"
askalono = "0.4.3"
byte-unit = "4.0.12"
bytecount = "0.6.2"
chrono = "0.4"
chrono-humanize = "0.2.1"
time-humanize = { version="0.1.2", features = ["time"] }
time = { version = "0.3.5", features = ["formatting"] }
clap = {version = "2.33.3", features = ["wrap_help"]}
color_quant = "1.1.0"
colored = "2.0.0"
Expand Down
69 changes: 56 additions & 13 deletions src/info/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use crate::info::author::Author;
use crate::info::head_refs::HeadRefs;
use anyhow::{Context, Result};
use byte_unit::Byte;
use chrono::{FixedOffset, TimeZone};
use chrono_humanize::HumanTime;
use git2::Time;
use git2::{
BranchType, Commit, Repository, RepositoryOpenFlags, Signature, Status, StatusOptions,
Expand All @@ -12,6 +10,10 @@ use git2::{
use regex::Regex;
use std::collections::HashMap;
use std::path::Path;
use time::format_description::well_known::Rfc3339;
use time::OffsetDateTime;

use time_humanize::HumanTime;

pub struct Repo<'a> {
repo: &'a Repository,
Expand Down Expand Up @@ -337,24 +339,65 @@ fn bytes_to_human_readable(bytes: u128) -> String {
}

fn git_time_to_formatted_time(time: &Time, iso_time: bool) -> String {
let (offset, _) = match time.offset_minutes() {
n if n < 0 => (-n, '-'),
n => (n, '+'),
};

let offset = FixedOffset::west(offset);
let dt_with_tz = offset.timestamp(time.seconds(), 0);
if iso_time {
dt_with_tz
.with_timezone(&chrono::Utc)
.to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
to_rfc3339(HumanTime::from(time.seconds()))
} else {
let ht = HumanTime::from(dt_with_tz);
let ht = HumanTime::from_duration_since_timestamp(time.seconds().unsigned_abs());
format!("{}", ht)
}
}

fn to_rfc3339<T>(dt: T) -> String
Comment on lines 341 to +350
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be cool if we added some tests for these functions. Unfortunately we didn't have any prior tests for duration formatting to confirm consistent behavior.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, can do that

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@spenserblack You think those tests are sufficient?

where
T: Into<OffsetDateTime>,
{
dt.into().format(&Rfc3339).unwrap()
}

pub fn is_valid(repo_path: &str) -> Result<bool> {
let repo = Repository::open_ext(repo_path, RepositoryOpenFlags::empty(), Vec::<&Path>::new());
Ok(repo.is_ok() && !repo?.is_bare())
}

#[cfg(test)]
mod tests {

use super::*;
use git2::Time;
use std::time::SystemTime;

#[test]
fn display_time_as_human_time_current_time_now() {
let current_time = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.unwrap();

let time = Time::new(current_time.as_secs() as i64, 0);
let result = git_time_to_formatted_time(&time, false);
assert_eq!(result, "now");
}

#[test]
fn display_time_as_human_time_current_time_arbitrary() {
let time = 1600000000;
let time = Time::new(time, 0);
let result = git_time_to_formatted_time(&time, false);
assert_eq!(result, "a year ago");
}

#[test]
fn display_time_as_iso_time_some_time() {
// Set "current" time to 11/18/2021 11:02:22
let time_sample = 1637233282;
let time = Time::new(time_sample, 0);
let result = git_time_to_formatted_time(&time, true);
assert_eq!(result, "2021-11-18T11:01:22Z");
}
#[test]
fn display_time_as_iso_time_current_epoch() {
let time_sample = 0;
let time = Time::new(time_sample, 0);
let result = git_time_to_formatted_time(&time, true);
assert_eq!(result, "1970-01-01T00:00:00Z");
}
}