Bläddra i källkod

create/recreate/import-tar --timestamp: default to local timezone

if you want to give UTC, just append: +00:00
Thomas Waldmann 2 år sedan
förälder
incheckning
4ab07b6acb

+ 1 - 1
src/borg/archiver/create.py

@@ -822,7 +822,7 @@ class CreateMixIn:
             type=timestamp,
             default=None,
             help="manually specify the archive creation date/time (yyyy-mm-ddThh:mm:ss[(+|-)HH:MM] format, "
-            "(+|-)HH:MM is the UTC offset, default: +00:00). Alternatively, give a reference file/directory.",
+            "(+|-)HH:MM is the UTC offset, default: local time zone). Alternatively, give a reference file/directory.",
         )
         archive_group.add_argument(
             "-c",

+ 1 - 1
src/borg/archiver/recreate.py

@@ -178,7 +178,7 @@ class RecreateMixIn:
             type=timestamp,
             default=None,
             help="manually specify the archive creation date/time (yyyy-mm-ddThh:mm:ss[(+|-)HH:MM] format, "
-            "(+|-)HH:MM is the UTC offset, default: +00:00). Alternatively, give a reference file/directory.",
+            "(+|-)HH:MM is the UTC offset, default: local time zone). Alternatively, give a reference file/directory.",
         )
         archive_group.add_argument(
             "-C",

+ 1 - 1
src/borg/archiver/tar.py

@@ -486,7 +486,7 @@ class TarMixIn:
             default=None,
             metavar="TIMESTAMP",
             help="manually specify the archive creation date/time (yyyy-mm-ddThh:mm:ss[(+|-)HH:MM] format, "
-            "(+|-)HH:MM is the UTC offset, default: +00:00). Alternatively, give a reference file/directory.",
+            "(+|-)HH:MM is the UTC offset, default: local time zone). Alternatively, give a reference file/directory.",
         )
         archive_group.add_argument(
             "-c",

+ 18 - 3
src/borg/helpers/time.py

@@ -3,13 +3,28 @@ from datetime import datetime, timezone
 
 
 def parse_timestamp(timestamp, tzinfo=timezone.utc):
-    """Parse a ISO 8601 timestamp string"""
+    """Parse a ISO 8601 timestamp string.
+
+    For naive/unaware dt, assume it is in tzinfo timezone (default: UTC).
+    """
     dt = datetime.fromisoformat(timestamp)
     if dt.tzinfo is None:
         dt = dt.replace(tzinfo=tzinfo)
     return dt
 
 
+def parse_local_timestamp(timestamp, tzinfo=None):
+    """Parse a ISO 8601 timestamp string.
+
+    For naive/unaware dt, assume it is in local timezone.
+    Convert to tzinfo timezone (the default None means: local timezone).
+    """
+    dt = datetime.fromisoformat(timestamp)
+    if dt.tzinfo is None:
+        dt = dt.astimezone(tz=tzinfo)
+    return dt
+
+
 def timestamp(s):
     """Convert a --timestamp=s argument to a datetime object"""
     try:
@@ -17,8 +32,8 @@ def timestamp(s):
         ts = safe_s(os.stat(s).st_mtime)
         return datetime.fromtimestamp(ts, tz=timezone.utc)
     except OSError:
-        # didn't work, try parsing as a ISO timestamp. if no TZ is given, we assume UTC.
-        return parse_timestamp(s)
+        # didn't work, try parsing as a ISO timestamp. if no TZ is given, we assume local timezone.
+        return parse_local_timestamp(s)
 
 
 # Not too rarely, we get crappy timestamps from the fs, that overflow some computations.

+ 1 - 7
src/borg/testsuite/archiver.py

@@ -2300,18 +2300,12 @@ class ArchiverTestCase(ArchiverTestCaseBase):
         # the latest archive must be still there
         self.assert_in("test5", output)
 
-    # Given a date and time in local tz, create a UTC timestamp string suitable
-    # for create --timestamp command line option
-    def _to_utc_timestamp(self, year, month, day, hour, minute, second):
-        dtime = datetime(year, month, day, hour, minute, second, 0).astimezone()  # local time with local timezone
-        return dtime.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S")
-
     def _create_archive_ts(self, name, y, m, d, H=0, M=0, S=0):
         self.cmd(
             f"--repo={self.repository_location}",
             "create",
             "--timestamp",
-            self._to_utc_timestamp(y, m, d, H, M, S),
+            datetime(y, m, d, H, M, S, 0).strftime(ISO_FORMAT_NO_USECS),  # naive == local time / local tz
             name,
             src_dir,
         )