From 9f7f337efd90eb7ffecac9c2285ba9318aae6cb6 Mon Sep 17 00:00:00 2001 From: ThetaDev Date: Tue, 18 Oct 2022 23:06:19 +0200 Subject: [PATCH] fix: remove split seconds --- src/client/mod.rs | 2 +- src/report.rs | 4 ++-- src/timeago.rs | 2 +- src/util/date.rs | 13 ++++++++++++- src/util/mod.rs | 2 +- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/client/mod.rs b/src/client/mod.rs index d5f9f3e..e762253 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -269,7 +269,7 @@ impl CacheEntry { impl From for CacheEntry { fn from(f: T) -> Self { Self::Some { - last_update: OffsetDateTime::now_utc(), + last_update: util::now_sec(), data: f, } } diff --git a/src/report.rs b/src/report.rs index 979d13b..3497cee 100644 --- a/src/report.rs +++ b/src/report.rs @@ -11,8 +11,8 @@ use serde::{Deserialize, Serialize}; use time::macros::format_description; use time::OffsetDateTime; -use crate::deobfuscate::DeobfData; use crate::error::Error; +use crate::{deobfuscate::DeobfData, util}; const FILENAME_FORMAT: &[time::format_description::FormatItem] = format_description!("[year]-[month]-[day]_[hour]-[minute]-[second]"); @@ -81,7 +81,7 @@ impl Default for Info { Self { package: "rustypipe".to_owned(), version: "0.1.0".to_owned(), - date: OffsetDateTime::now_utc(), + date: util::now_sec(), } } } diff --git a/src/timeago.rs b/src/timeago.rs index 8ae4bec..53b0090 100644 --- a/src/timeago.rs +++ b/src/timeago.rs @@ -84,7 +84,7 @@ impl Mul for TimeAgo { impl From for OffsetDateTime { fn from(ta: TimeAgo) -> Self { - let ts = OffsetDateTime::now_utc(); + let ts = util::now_sec(); match ta.unit { TimeUnit::Second => ts - Duration::seconds(ta.n as i64), TimeUnit::Minute => ts - Duration::minutes(ta.n as i64), diff --git a/src/util/date.rs b/src/util/date.rs index a161565..742a9c6 100644 --- a/src/util/date.rs +++ b/src/util/date.rs @@ -1,4 +1,4 @@ -use time::{Date, Month}; +use time::{Date, Month, OffsetDateTime}; pub const fn month_from_n(n: u8) -> Option { match n { @@ -42,3 +42,14 @@ pub fn shift_months(date: Date, months: i32) -> Date { pub fn shift_years(date: Date, years: i32) -> Date { shift_months(date, years * 12) } + +/// Get the current datetime without milli/micro/nanoseconds +pub fn now_sec() -> OffsetDateTime { + OffsetDateTime::now_utc() + .replace_millisecond(0) + .unwrap() + .replace_microsecond(0) + .unwrap() + .replace_nanosecond(0) + .unwrap() +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 9c6ab8f..02a746f 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -3,7 +3,7 @@ mod protobuf; pub mod dictionary; -pub use date::{month_from_n, shift_months, shift_years}; +pub use date::{month_from_n, now_sec, shift_months, shift_years}; pub use protobuf::ProtoBuilder; use std::{