From 3e88dadeb142dfc5b86d35975e30e168bcf00833 Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Thu, 12 Oct 2023 16:27:48 +0200
Subject: [PATCH] fix date time handling

---
 check_journallog | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/check_journallog b/check_journallog
index b77503a..0f6f13e 100755
--- a/check_journallog
+++ b/check_journallog
@@ -85,15 +85,21 @@ lastLine=$( cat "$tmpfile" 2>/dev/null )
 if [ -z "$lastLine" ]; then
     ph.setStatus unknown
     ph.status "Initializing ... storing last line of jorunallog ..."
-    journalctl -n 1 > "$tmpfile"
+    journalctl -n 1 | tail -1 > "$tmpfile"
 else
-    ts=$( cut -f 1-3 -d " " <<< "$lastLine" )
+
+    # mydate is "Okt 12 16:23:36"
+    # mydate=$( cut -f 1-3 -d " " <<< "$lastLine" )
+    
+    # what works:
+    # date -d "12 sec ago" +"%Y-%m-%d %H:%I:%S"
+    # date -d "Oct 12 14:24:04" +"%Y-%m-%d %H:%M:%S"
+
     typeset -i iAge; iAge=$( ph.getFileAge "$tmpfile" )
+    # ts=$( date -d "${mydate}" +"%Y-%m-%d %H:%M:%S" )
+    ts=$( date -d "${iAge} sec ago" +"%Y-%m-%d %H:%M:%S" )
     data=$( sudo journalctl --since "$ts" )
 
-    # for next run: insert last handled line
-    tail -1 <<< "$data" > "$tmpfile"
-
     # detect last seen position and count lines from there
     typeset -i iTotal; iTotal=$( wc -l <<< "$data" )
     typeset -i iFrom;  iFrom=$( grep -Fn "$lastLine" <<< "$data" | head -1 | cut -f 1 -d ':' )
@@ -121,6 +127,9 @@ else
     echo "Limits: $sLimits"
     echo "Raw data: Found $iLines new line(s) in the last $iAge sec"
 
+    # for next run: insert last handled line
+    test $iLines -gt 0 && tail -1 <<< "$data" > "$tmpfile"
+
 fi
 
 # cat "$tmpfile"
-- 
GitLab