summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-06-13 06:44:20 +0200
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2024-06-13 06:44:20 +0200
commitd148d3fdc076a9c41229e6e216a2a045e227cc08 (patch)
tree6b8dda002a0ee99f73a0a667f6d4dd612c51f397
parentfix(new::section): Avoid overriding already existing section file (diff)
downloadlpm-d148d3fdc076a9c41229e6e216a2a045e227cc08.tar.gz
lpm-d148d3fdc076a9c41229e6e216a2a045e227cc08.zip
fix(new::section): Generate a precise date
-rw-r--r--Cargo.toml1
-rw-r--r--src/new/section.rs50
2 files changed, 41 insertions, 10 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 7264004..1f73201 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -16,6 +16,7 @@ anyhow = "1.0.86"
 chrono = { version = "0.4.38", features = ["alloc"] }
 clap = { version = "4.5.7", features = ["derive"] }
 convert_case = "0.6.0"
+deunicode = "1.6.0"
 log = "0.4.21"
 serde = { version = "1.0.203", features = ["derive"] }
 serde_derive = "1.0.203"
diff --git a/src/new/section.rs b/src/new/section.rs
index bfa2b2c..5e9f08c 100644
--- a/src/new/section.rs
+++ b/src/new/section.rs
@@ -5,8 +5,7 @@ use std::{
 };
 
 use anyhow::Context;
-use chrono::{DateTime, Local};
-use convert_case::{Case, Casing};
+use chrono::{Local, TimeDelta, TimeZone};
 use log::debug;
 
 use crate::{
@@ -15,6 +14,44 @@ use crate::{
     new::MangledName,
 };
 
+fn get_current_date() -> String {
+    let start = SystemTime::now();
+    let seconds_since_epoch: TimeDelta = TimeDelta::from_std(
+        start
+            .duration_since(UNIX_EPOCH)
+            .expect("Time went backwards"),
+    )
+    .expect("Time does not go backwards");
+
+    debug!(
+        "Adding a date with timestamp: {}",
+        seconds_since_epoch.num_seconds()
+    );
+
+    let our_date = format!(
+        "{}",
+        Local
+            .timestamp_opt(seconds_since_epoch.num_seconds(), 0)
+            // only has unwrap, no expect. But should always work
+            .unwrap()
+            .format("%Y-%m-%d %H:%M:%S%.f %:z")
+    );
+
+    // let date = Command::new("date")
+    //     .args(["-d", &format!("@{}", seconds_since_epoch.num_seconds())])
+    //     .output()
+    //     .unwrap()
+    //     .stdout;
+
+    // info!(
+    //     "This is dated: '{}' vs. our date: '{}'",
+    //     String::from_utf8(date).unwrap().strip_suffix('\n').unwrap(),
+    //     our_date
+    // );
+
+    our_date
+}
+
 pub fn generate_new_section(
     config: &Config,
     name: String,
@@ -30,14 +67,7 @@ pub fn generate_new_section(
         .templates
         .section
         .replace("REPLACMENT_SECTION_TITLE", &name)
-        .replace(
-            "DATE",
-            &format!(
-                "{}",
-                // FIXME: The time is not really precise enough to display the time.  <2024-03-31>
-                DateTime::<Local>::from(SystemTime::now()).format("%Y-%m-%d (%_H:%_S)")
-            ),
-        );
+        .replace("DATE", &get_current_date());
 
     file_tree.add_file(GeneratedFile::new_clobber(
         chapter_root