Browse Source

archive listing: use iso8601 timestamp format with --json-lines

like yyyy-mm-ddThh:mm:ss - no tz yet, this likely needs more refactoring
to tz aware and utc datetime objects everywhere, currently there are
naive datetime objects and also localtime at quite some places.

(cherry picked from commit 043d794b91a6c3b3ce18fef18cdbd256df398d04)
Thomas Waldmann 7 years ago
parent
commit
d17553add6
1 changed files with 19 additions and 7 deletions
  1. 19 7
      src/borg/helpers.py

+ 19 - 7
src/borg/helpers.py

@@ -717,11 +717,17 @@ def safe_timestamp(item_timestamp_ns):
 
 
 def format_time(t):
-    """use ISO-8601 date and time format
+    """use ISO-8601-like date and time format (human readable, with wkday and blank date/time separator)
     """
     return t.strftime('%a, %Y-%m-%d %H:%M:%S')
 
 
+def isoformat_time(t):
+    """use ISO-8601 date and time format (machine readable, no wkday, no microseconds either)
+    """
+    return t.strftime('%Y-%m-%dT%H:%M:%S')  # note: first make all datetime objects tz aware before adding %z here.
+
+
 def format_timedelta(td):
     """Format timedelta in a human friendly format
     """
@@ -1762,6 +1768,12 @@ class ItemFormatter(BaseFormatter):
             'archiveid': archive.fpr,
         }
         static_keys.update(self.FIXED_KEYS)
+        if self.json_lines:
+            self.item_data = {}
+            self.format_item = self.format_item_json
+            self.format_time = self.format_time_json
+        else:
+            self.item_data = static_keys
         self.format = partial_format(format, static_keys)
         self.format_keys = {f[1] for f in Formatter().parse(format)}
         self.call_keys = {
@@ -1781,11 +1793,6 @@ class ItemFormatter(BaseFormatter):
         for hash_function in hashlib.algorithms_guaranteed:
             self.add_key(hash_function, partial(self.hash_item, hash_function))
         self.used_call_keys = set(self.call_keys) & self.format_keys
-        if self.json_lines:
-            self.item_data = {}
-            self.format_item = self.format_item_json
-        else:
-            self.item_data = static_keys
 
     def format_item_json(self, item):
         return json.dumps(self.get_item_data(item)) + '\n'
@@ -1863,7 +1870,12 @@ class ItemFormatter(BaseFormatter):
         return hash.hexdigest()
 
     def format_time(self, key, item):
-        return format_time(safe_timestamp(item.get(key) or item.mtime))
+        t = self.time(key, item)
+        return format_time(t)
+
+    def format_time_json(self, key, item):
+        t = self.time(key, item)
+        return isoformat_time(t)
 
     def time(self, key, item):
         return safe_timestamp(item.get(key) or item.mtime)