From f1844882e6f15ce8176dabd0d740c767941f9064 Mon Sep 17 00:00:00 2001 From: itiB Date: Thu, 10 Dec 2020 01:45:36 +0900 Subject: [PATCH] Refact: after_fact.rs emit time format --- src/afterfact.rs | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/src/afterfact.rs b/src/afterfact.rs index fd30f2e5..43b03c8a 100644 --- a/src/afterfact.rs +++ b/src/afterfact.rs @@ -1,6 +1,6 @@ use crate::detections::configs; use crate::detections::print; -use chrono::Local; +use chrono::{DateTime, Local, TimeZone, Utc}; use serde::Serialize; use std::error::Error; use std::fs::File; @@ -39,24 +39,9 @@ fn emit_csv(writer: &mut Box) -> Result<(), Box> { let messages = print::MESSAGES.lock().unwrap(); for (time, texts) in messages.iter() { - let formated_time = if configs::singleton().args.is_present("utc") { - if configs::singleton().args.is_present("rfc-2822") { - time.to_rfc2822() - } else { - time.to_rfc3339() - } - } else { - let time_local = time.with_timezone(&Local); - if configs::singleton().args.is_present("rfc-2822") { - time_local.to_rfc2822() - } else { - time_local.to_rfc3339() - } - }; - for text in texts { wtr.serialize(CsvFormat { - time: &formated_time, + time: &format_time(time), message: text, })?; } @@ -65,6 +50,25 @@ fn emit_csv(writer: &mut Box) -> Result<(), Box> { Ok(()) } +fn format_time(time: &DateTime) -> String { + if configs::singleton().args.is_present("utc") { + format_rfc(time) + } else { + format_rfc(&time.with_timezone(&Local)) + } +} + +fn format_rfc(time: &DateTime) -> String +where + Tz::Offset: std::fmt::Display, +{ + if configs::singleton().args.is_present("rfc-2822") { + return time.to_rfc2822(); + } else { + return time.to_rfc3339(); + } +} + #[test] fn test_emit_csv() { use serde_json::Value;