use local time / local timezone to output timestamps, fixes #7283

"ls" and also "tar" show timestamps as local time.
borg now does the same, but also shows the local tzoffset.

this also affects JSON output.
This commit is contained in:
Thomas Waldmann 2023-01-19 03:32:13 +01:00
parent be1048abfe
commit 9460da42d1
No known key found for this signature in database
GPG key ID: 243ACFA951F78E01
2 changed files with 11 additions and 9 deletions

View file

@ -114,13 +114,15 @@ class OutputTimestamp:
self.ts = ts
def __format__(self, format_spec):
return format_time(self.ts, format_spec=format_spec)
# we want to output a timestamp in the user's local timezone
return format_time(self.ts.astimezone(), format_spec=format_spec)
def __str__(self):
return f"{self}"
def isoformat(self):
return self.ts.isoformat(timespec="microseconds")
# we want to output a timestamp in the user's local timezone
return self.ts.astimezone().isoformat(timespec="microseconds")
to_json = isoformat

View file

@ -2,7 +2,6 @@ import json
import os
import stat
import tarfile
from datetime import timezone
import unittest
from ...constants import * # NOQA
@ -48,9 +47,10 @@ class ArchiverTestCase(ArchiverTestCaseBase):
repo12_tar = os.path.join(os.path.dirname(__file__), "repo12.tar.gz")
repo12_tzoffset = "+01:00" # timezone used to create the repo/archives/json dumps inside the tar file
def local_to_utc(local_naive, tzoffset, tzinfo):
def convert_tz(local_naive, tzoffset, tzinfo):
# local_naive was meant to be in tzoffset timezone (e.g. "+01:00"),
# but we want it non-naive in tzinfo time zone (e.g. timezone.utc).
# but we want it non-naive in tzinfo time zone (e.g. timezone.utc
# or None if local timezone is desired).
ts = parse_timestamp(local_naive + tzoffset)
return ts.astimezone(tzinfo).isoformat(timespec="microseconds")
@ -85,10 +85,10 @@ class ArchiverTestCase(ArchiverTestCaseBase):
del expected_archive["barchive"]
# timestamps:
# borg 1.2 transformed to local time and had microseconds = 0, no tzoffset
# borg 2 uses an utc timestamp, with microseconds and with tzoffset
# borg 2 uses local time, with microseconds and with tzoffset
for key in "start", "time":
# fix expectation: local time meant +01:00, so we convert that to utc +00:00
expected_archive[key] = local_to_utc(expected_archive[key], repo12_tzoffset, timezone.utc)
# fix expectation: local time meant +01:00, so we convert that to whatever local tz is here.
expected_archive[key] = convert_tz(expected_archive[key], repo12_tzoffset, None)
# set microseconds to 0, so we can compare got with expected.
got_ts = parse_timestamp(got_archive[key])
got_archive[key] = got_ts.replace(microsecond=0).isoformat(timespec="microseconds")
@ -125,7 +125,7 @@ class ArchiverTestCase(ArchiverTestCaseBase):
for key in "mtime", "ctime", "atime":
if key in e:
e[key] = local_to_utc(e[key], repo12_tzoffset, timezone.utc)
e[key] = convert_tz(e[key], repo12_tzoffset, None)
# borg 1 used hardlink slaves linking back to their hardlink masters.
# borg 2 uses symmetric approach: just normal items. if they are hardlinks,