From 17d375a5dcd369d550b861174b2164543e40d04e Mon Sep 17 00:00:00 2001 From: Neshura Date: Thu, 3 Aug 2023 21:27:39 +0200 Subject: [PATCH] Fixed Time "crash" using NaiveDateTime instead of NaiveTime --- src/main.rs | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8f21fa1..d64a7e6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use chrono::{Utc, DateTime, NaiveTime}; +use chrono::{Utc, DateTime, NaiveTime, NaiveDateTime, Timelike, NaiveDate}; use config::{Config, PrevPost, Secrets, CommunitiesVector}; use lemmy_api_common::{ person::{Login, LoginResponse}, @@ -31,7 +31,7 @@ struct Bot { post_history: Vec, community_ids: CommunitiesVector, auth: Sensitive, - start_time: DateTime, + start_time: NaiveDateTime, } impl Bot { @@ -42,7 +42,7 @@ impl Bot { post_history: PrevPost::load(), community_ids: CommunitiesVector::new(), auth: Sensitive::new("".to_string()), - start_time: Utc::now(), + start_time: Utc::now().naive_local(), } } @@ -98,13 +98,13 @@ impl Bot { } #[warn(unused_results)] - pub(crate) fn run_once(&mut self, prev_time: &mut NaiveTime) -> Result<(), reqwest::Error> { + pub(crate) fn run_once(&mut self, prev_time: &mut NaiveDateTime) -> Result<(), reqwest::Error> { println!("{:#<1$}", "", 30); - self.start_time = Utc::now(); + self.start_time = Utc::now().naive_local(); - if self.start_time.time() - *prev_time > chrono::Duration::seconds(6) { // Prod should use hours, add command line switch later and read duration from config + if self.start_time - *prev_time > chrono::Duration::seconds(6) { // Prod should use hours, add command line switch later and read duration from config println!("Reloading Config"); - *prev_time = self.start_time.time(); + *prev_time = self.start_time; self.config.load(); match self.community_ids.load(&self.auth, &self.config.instance) { Ok(_) => {}, @@ -135,11 +135,11 @@ impl Bot { pub(crate) fn idle(&self) { let mut sleep_duration = chrono::Duration::seconds(30); - if Utc::now().time() - self.start_time.time() > sleep_duration { + if Utc::now().naive_local() - self.start_time > sleep_duration { sleep_duration = chrono::Duration::seconds(60); } - while Utc::now().time() - self.start_time.time() < sleep_duration { + while Utc::now().naive_local() - self.start_time < sleep_duration { sleep(time::Duration::from_secs(1)); } @@ -184,7 +184,7 @@ fn list_posts(auth: &Sensitive, base: String) -> GetPostsResponse { fn run_bot() { // Get all needed auth tokens at the start - let mut old = Utc::now().time(); + let mut old = Utc::now().naive_local(); let mut this = Bot::new(); match this.login() { Ok(_) => { @@ -192,10 +192,6 @@ fn run_bot() { // Enter a loop (not for debugging) loop { - println!("Start Time: {}", this.start_time.time()); - println!("Previous Time: {}", old); - println!("Difference Start - Old: {}", this.start_time.time() - old); - println!("Difference Now - Start: {}", Utc::now().time() - this.start_time.time()); this.idle(); // 3 retries in case of connection issues let mut loop_breaker: u8 = 0; @@ -208,10 +204,6 @@ fn run_bot() { loop_breaker += 1; }; this.print_info(); - println!("Start Time: {}", this.start_time.time()); - println!("Previous Time: {}", old); - println!("Difference Start - Old: {}", this.start_time.time() - old); - println!("Difference Now - Start: {}", Utc::now().time() - this.start_time.time()); this.idle(); } },