Fixed Time "crash" using NaiveDateTime instead of NaiveTime

This commit is contained in:
Neshura 2023-08-03 21:27:39 +02:00
parent 1c619f1b97
commit 17d375a5dc
Signed by: Neshura
GPG key ID: B6983AAA6B9A7A6C

View file

@ -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<PrevPost>,
community_ids: CommunitiesVector,
auth: Sensitive<String>,
start_time: DateTime<Utc>,
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<String>, 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();
}
},