From 504184e69b8e176da0499ed03e6c48f8060a12cf Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Fri, 1 Dec 2023 18:32:32 -0500 Subject: [PATCH] Refactor access of current time --- src/database.rs | 15 ++++++--------- src/main.rs | 9 ++++++--- src/sync.rs | 1 + src/util.rs | 12 +++++++++++- 4 files changed, 24 insertions(+), 13 deletions(-) diff --git a/src/database.rs b/src/database.rs index d5f3a46..3a073d9 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,10 +1,8 @@ -use crate::error; use crate::queries::*; use sqlite::*; use std::fs::{self, OpenOptions}; use std::io; use std::path::{Path, PathBuf}; -use std::time::SystemTime; pub struct DatasetMetadata { pub last_sync: Timestamp, @@ -239,18 +237,17 @@ pub fn get_metadata( .and_then(Result::ok)) } -pub fn update_last_sync(connection: &Connection, dataset: &str) -> sqlite::Result<()> { +pub fn update_last_sync( + connection: &Connection, + dataset: &str, + current_time: Timestamp, +) -> sqlite::Result<()> { let query = "UPDATE datasets SET last_sync = :sync WHERE name = :dataset"; - let current_time = SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH) - .unwrap_or_else(|_| error("System time is before the Unix epoch (1970)!", 2)) - .as_secs(); - connection .prepare(query)? .into_iter() - .bind((":sync", current_time as i64))? + .bind((":sync", current_time.0 as i64))? .bind((":dataset", dataset))? .try_for_each(|x| x.map(|_| ())) } diff --git a/src/main.rs b/src/main.rs index b32e5b9..c1e73b5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -170,7 +170,7 @@ fn dataset_list() { print!(" - \x1b[33mRun 'startrnr sync' to update!\x1b[0m"); } else { print!( - " - \x1b[33mRun 'startrnr sync \"{}\"' to update!\x1b[0m", + " - \x1b[33mRun 'startrnr sync {:?}' to update!\x1b[0m", name ); } @@ -529,15 +529,18 @@ fn sync(datasets: Vec, all: bool, auth_token: Option) { datasets }; + let current_time = current_time(); + for dataset in datasets { let dataset_config = get_metadata(&connection, &dataset) .expect("Error communicating with SQLite") .unwrap_or_else(|| error(&format!("Dataset {} does not exist!", dataset), 1)); - sync_dataset(&connection, &dataset, dataset_config, &auth) + sync_dataset(&connection, &dataset, dataset_config, current_time, &auth) .expect("Error communicating with SQLite"); // .unwrap_or_else(|_| error("Error communicating with SQLite", 2)); - update_last_sync(&connection, &dataset).expect("Error communicating with SQLite"); + update_last_sync(&connection, &dataset, current_time) + .expect("Error communicating with SQLite"); } } diff --git a/src/sync.rs b/src/sync.rs index e8a9398..283dcca 100644 --- a/src/sync.rs +++ b/src/sync.rs @@ -289,6 +289,7 @@ pub fn sync_dataset( connection: &Connection, dataset: &str, metadata: DatasetMetadata, + current_time: Timestamp, auth: &str, ) -> sqlite::Result<()> { let events = get_tournament_events(&metadata, auth) diff --git a/src/util.rs b/src/util.rs index 15d06e0..9729759 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,9 +1,10 @@ use sqlite::*; use std::io::{self, Write}; use std::process::exit; +use std::time::SystemTime; use crate::database::*; -use crate::queries::{PlayerData, PlayerId}; +use crate::queries::{PlayerData, PlayerId, Timestamp}; pub const SECS_IN_HR: u64 = 3600; pub const SECS_IN_DAY: u64 = SECS_IN_HR * 24; @@ -19,6 +20,15 @@ pub fn issue(msg: &str, code: i32) -> ! { exit(code) } +pub fn current_time() -> Timestamp { + Timestamp( + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH) + .unwrap_or_else(|_| error("System time is before the Unix epoch (1970)!", 2)) + .as_secs(), + ) +} + pub fn read_string() -> String { let mut line = String::new(); io::stdout()