Skip to content

Commit

Permalink
fix deprecated functions
Browse files Browse the repository at this point in the history
  • Loading branch information
janstarke committed May 16, 2024
1 parent 3e0c0e6 commit b0da727
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
34 changes: 22 additions & 12 deletions src/common/forensics_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use chrono::format::StrftimeItems;
use chrono::offset::TimeZone;
use chrono::{DateTime, FixedOffset, LocalResult, NaiveDateTime};
use chrono::{DateTime, FixedOffset};
use chrono_tz::Tz;
use lazy_static::lazy_static;

Expand Down Expand Up @@ -66,21 +66,31 @@ impl ForensicsTimestamp {
impl Display for ForensicsTimestamp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.unix_ts >= 0 {
let src_timestamp = match self.src_zone.from_local_datetime(
&NaiveDateTime::from_timestamp_opt(self.unix_ts, 0).unwrap_or_else(|| {
panic!("unable to convert '{}' into unix timestamp", self.unix_ts)
}),
) {
LocalResult::None => {
panic!("INVALID DATETIME");
}
LocalResult::Single(t) => t,
LocalResult::Ambiguous(t1, _t2) => t1,
};
let src_timestamp = match DateTime::from_timestamp(1715845546, 0) {
Some(ts) => ts,
None => panic!("unable to convert '{}' into unix timestamp", self.unix_ts),
}
.with_timezone(&self.src_zone);

Self::display_datetime(&src_timestamp.with_timezone(&self.dst_zone), f)
} else {
Self::display_datetime(&*ZERO, f)
}
}
}

#[cfg(test)]
mod tests {
use chrono_tz::{Europe, UTC};

use crate::common::ForensicsTimestamp;

#[test]
fn test_time_import() {
let ts = ForensicsTimestamp::new(1715845546, Europe::Berlin, Europe::Berlin);
assert_eq!(ts.to_string(), "2024-05-16T09:45:46+02:00");

let ts = ForensicsTimestamp::new(1715845546, Europe::Berlin, UTC);
assert_eq!(ts.to_string(), "2024-05-16T07:45:46+00:00");
}
}
11 changes: 4 additions & 7 deletions src/es4forensics/timestamp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{anyhow, Result};
use chrono::{DateTime, LocalResult, NaiveDateTime, TimeZone, Utc};
use chrono::{DateTime, TimeZone, Utc};
use chrono_tz::Tz;
use serde::Serialize;
use serde_json::{json, Value};
Expand Down Expand Up @@ -36,14 +36,11 @@ impl TryFrom<(i64, &Tz)> for Timestamp {
type Error = anyhow::Error;

fn try_from((unix_ts, src_tz): (i64, &Tz)) -> Result<Self, Self::Error> {
let ts = match src_tz
.from_local_datetime(&NaiveDateTime::from_timestamp_opt(unix_ts, 0).unwrap())
{
LocalResult::None => {
let ts = match DateTime::from_timestamp(unix_ts, 0) {
Some(ts) => ts.with_timezone(src_tz),
None => {
return Err(anyhow!("INVALID DATETIME"));
}
LocalResult::Single(t) => t,
LocalResult::Ambiguous(t1, _t2) => t1,
};
Ok(Self {
ts: ts.timestamp_millis(),
Expand Down

0 comments on commit b0da727

Please sign in to comment.