Skip to content

Commit

Permalink
sysinfo: fix installtime offset (dnf returns UTC, not local time)
Browse files Browse the repository at this point in the history
  • Loading branch information
decathorpe committed Apr 8, 2020
1 parent 118512c commit e2c5f03
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

6 changes: 3 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::io::{stdin, stdout, Write};

use bodhi::{Karma, Update};
use chrono::{DateTime, Local};
use chrono::{DateTime, Utc};

use super::print_update;

Expand Down Expand Up @@ -55,11 +55,11 @@ pub fn str_to_karma(string: &str) -> Option<Karma> {
/// automatically; two empty lines or EOF (`Ctrl-D`) ends comment input)
///
/// If enabled at compile time, it also asks for bug and testcase feedback.
pub fn ask_feedback<'a>(
pub fn ask_feedback<'a, S: std::hash::BuildHasher>(
rl: &mut rustyline::Editor<()>,
update: &'a Update,
builds: &[&str],
install_times: &HashMap<String, DateTime<Local>>,
install_times: &HashMap<String, DateTime<Utc>, S>,
) -> Result<Feedback<'a>, String> {
print_update(update, builds, install_times);

Expand Down
16 changes: 10 additions & 6 deletions src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::io::{stdout, Write};

use bodhi::{Comment, Update};
use chrono::{DateTime, Duration, Local};
use chrono::{DateTime, Duration, Utc};

/// This function draws a pretty progress bar with this format:
///
Expand Down Expand Up @@ -33,8 +33,8 @@ pub fn progress_bar(prefix: &str, p: u32, ps: u32) {
}

/// This helper function returns the duration from a datetime that lies in the past until now.
fn duration_until_now(datetime: &DateTime<Local>) -> Duration {
let result = Local::now() - datetime.to_owned();
fn duration_until_now(datetime: &DateTime<Utc>) -> Duration {
let result = Utc::now() - datetime.to_owned();

if result <= Duration::seconds(0) {
Duration::seconds(0)
Expand Down Expand Up @@ -68,14 +68,18 @@ fn pretty_duration(duration: Duration) -> String {
)
} else if duration >= Duration::minutes(1) {
let minutes = duration.num_minutes();
format!("{}", proper_plural(minutes, "minute"))
proper_plural(minutes, "minute")
} else {
format!("less than a minute")
String::from("less than a minute")
}
}

/// This helper function pretty-prints an update.
pub fn print_update(update: &Update, builds: &[&str], install_times: &HashMap<String, DateTime<Local>>) {
pub fn print_update<S: std::hash::BuildHasher>(
update: &Update,
builds: &[&str],
install_times: &HashMap<String, DateTime<Utc>, S>,
) {
let submitted_date = match &update.date_submitted {
Some(date) => date.to_string(),
None => "(None)".to_string(),
Expand Down
10 changes: 5 additions & 5 deletions src/sysinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;
use std::process::Command;

use bodhi::FedoraRelease;
use chrono::{DateTime, Local, TimeZone};
use chrono::{DateTime, TimeZone, Utc};

use super::{parse_filename, NVR};

Expand Down Expand Up @@ -144,7 +144,7 @@ pub fn get_src_bin_map() -> Result<HashMap<String, Vec<String>>, String> {
}

/// This helper function returns a map from binary packages to their installation times.
pub fn get_installation_times() -> Result<HashMap<String, DateTime<Local>>, String> {
pub fn get_installation_times() -> Result<HashMap<String, DateTime<Utc>>, String> {
// query dnf for installed binary packages and their corresponding installation dates
let output = match Command::new("dnf")
.arg("--quiet")
Expand Down Expand Up @@ -172,7 +172,7 @@ pub fn get_installation_times() -> Result<HashMap<String, DateTime<Local>>, Stri

let lines: Vec<&str> = results.trim().split('\n').collect();

let mut pkg_map: HashMap<String, DateTime<Local>> = HashMap::new();
let mut pkg_map: HashMap<String, DateTime<Utc>> = HashMap::new();

for line in lines {
let parts: Vec<&str> = line.split('\t').collect();
Expand All @@ -184,12 +184,12 @@ pub fn get_installation_times() -> Result<HashMap<String, DateTime<Local>>, Stri
let binary = parts.get(0).unwrap();
let installtime = parts.get(1).unwrap();

let local_time = match Local.datetime_from_str(installtime, "%Y-%m-%d %H:%M") {
let datetime = match Utc.datetime_from_str(installtime, "%Y-%m-%d %H:%M") {
Ok(datetime) => datetime,
Err(error) => return Err(format!("Failed to parse dnf output: {}", error)),
};

pkg_map.entry((*binary).to_string()).or_insert(local_time);
pkg_map.entry((*binary).to_string()).or_insert(datetime);
}

Ok(pkg_map)
Expand Down

0 comments on commit e2c5f03

Please sign in to comment.