changed format_time_nice to a trait that extends chrono::Duration
This commit is contained in:
parent
6c83683a4a
commit
e98c5e7a12
28
src/common/display_duration_ext.rs
Normal file
28
src/common/display_duration_ext.rs
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
pub trait DisplayDurationExt {
|
||||||
|
/// displays a duration in a human readable form
|
||||||
|
fn display(&self) -> String;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DisplayDurationExt for chrono::Duration {
|
||||||
|
fn display(&self) -> String {
|
||||||
|
if self.num_weeks() == 52 {
|
||||||
|
format!("{} Year", (self.num_weeks() / 52) as i32)
|
||||||
|
} else if self.num_weeks() > 103 {
|
||||||
|
format!("{} Years", (self.num_weeks() / 52) as i32)
|
||||||
|
} else if self.num_days() == 1 {
|
||||||
|
format!("{} Day", self.num_days())
|
||||||
|
} else if self.num_days() > 1 {
|
||||||
|
format!("{} Days", self.num_days())
|
||||||
|
} else if self.num_hours() == 1 {
|
||||||
|
format!("{} Hour", self.num_hours())
|
||||||
|
} else if self.num_hours() > 1 {
|
||||||
|
format!("{} Hours", self.num_hours())
|
||||||
|
} else if self.num_minutes() == 1 {
|
||||||
|
format!("{} Minute", self.num_minutes())
|
||||||
|
} else if self.num_minutes() > 1 {
|
||||||
|
format!("{} Minutes", self.num_minutes())
|
||||||
|
} else {
|
||||||
|
format!("{} Seconds", self.num_seconds())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/common/mod.rs
Normal file
1
src/common/mod.rs
Normal file
@ -0,0 +1 @@
|
|||||||
|
pub mod display_duration_ext;
|
@ -1,6 +1,7 @@
|
|||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
mod common;
|
||||||
mod repo;
|
mod repo;
|
||||||
mod repository;
|
mod repository;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
@ -5,6 +5,7 @@ use std::fmt;
|
|||||||
|
|
||||||
use chrono::DateTime;
|
use chrono::DateTime;
|
||||||
|
|
||||||
|
use crate::common::display_duration_ext::DisplayDurationExt;
|
||||||
use crate::repo;
|
use crate::repo;
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
@ -54,7 +55,7 @@ impl Tag {
|
|||||||
let now = chrono::Utc::now();
|
let now = chrono::Utc::now();
|
||||||
let rfc3339 = DateTime::parse_from_rfc3339(last_updated).unwrap();
|
let rfc3339 = DateTime::parse_from_rfc3339(last_updated).unwrap();
|
||||||
let dif = now - rfc3339.with_timezone(&chrono::Utc);
|
let dif = now - rfc3339.with_timezone(&chrono::Utc);
|
||||||
format!(", {} old", format_time_nice(dif))
|
format!(", {} old", dif.display())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -109,29 +110,6 @@ impl Repo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// converts a given duration to a readable string
|
|
||||||
fn format_time_nice(time: chrono::Duration) -> String {
|
|
||||||
if time.num_weeks() == 52 {
|
|
||||||
format!("{} Year", (time.num_weeks() / 52) as i32)
|
|
||||||
} else if time.num_weeks() > 103 {
|
|
||||||
format!("{} Years", (time.num_weeks() / 52) as i32)
|
|
||||||
} else if time.num_days() == 1 {
|
|
||||||
format!("{} Day", time.num_days())
|
|
||||||
} else if time.num_days() > 1 {
|
|
||||||
format!("{} Days", time.num_days())
|
|
||||||
} else if time.num_hours() == 1 {
|
|
||||||
format!("{} Hour", time.num_hours())
|
|
||||||
} else if time.num_hours() > 1 {
|
|
||||||
format!("{} Hours", time.num_hours())
|
|
||||||
} else if time.num_minutes() == 1 {
|
|
||||||
format!("{} Minute", time.num_minutes())
|
|
||||||
} else if time.num_minutes() > 1 {
|
|
||||||
format!("{} Minutes", time.num_minutes())
|
|
||||||
} else {
|
|
||||||
format!("{} Seconds", time.num_seconds())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// checks the repo name and may add a prefix for official images
|
/// checks the repo name and may add a prefix for official images
|
||||||
pub fn check_repo(name: &str) -> Result<String, Error> {
|
pub fn check_repo(name: &str) -> Result<String, Error> {
|
||||||
let repo = match repo::split_tag_from_repo(name) {
|
let repo = match repo::split_tag_from_repo(name) {
|
||||||
|
Loading…
Reference in New Issue
Block a user