Переглянути джерело

refactor: get archive timestamps via archive_ts_now()

Thomas Waldmann 2 роки тому
батько
коміт
1f859c9f17

+ 5 - 5
src/borg/archive.py

@@ -30,7 +30,7 @@ from .helpers import HardLinkManager
 from .helpers import ChunkIteratorFileWrapper, open_item
 from .helpers import Error, IntegrityError, set_ec
 from .platform import uid2user, user2uid, gid2group, group2gid
-from .helpers import parse_timestamp
+from .helpers import parse_timestamp, archive_ts_now
 from .helpers import OutputTimestamp, format_timedelta, format_file_size, file_status, FileSize
 from .helpers import safe_encode, make_path_safe, remove_surrogates
 from .helpers import StableDict
@@ -510,13 +510,13 @@ class Archive:
             start_monotonic is None
         ), "Logic error: if start is given, start_monotonic must be given as well and vice versa."
         if start is None:
-            start = datetime.now().astimezone()  # local time with local timezone
+            start = archive_ts_now()
             start_monotonic = time.monotonic()
         self.chunker_params = chunker_params
         self.start = start
         self.start_monotonic = start_monotonic
         if end is None:
-            end = datetime.now().astimezone()  # local time with local timezone
+            end = archive_ts_now()
         self.end = end
         self.consider_part_files = consider_part_files
         self.pipeline = DownloadPipeline(self.repository, self.repo_objs)
@@ -663,7 +663,7 @@ Duration: {0.duration}
         )
         duration = timedelta(seconds=time.monotonic() - self.start_monotonic)
         if timestamp is None:
-            end = datetime.now().astimezone()  # local time with local timezone
+            end = archive_ts_now()
             start = end - duration
         else:
             start = timestamp
@@ -2364,7 +2364,7 @@ class ArchiveRecreater:
             target.rename(archive.name)
         if self.stats:
             target.start = _start
-            target.end = datetime.now().astimezone()  # local time with local timezone
+            target.end = archive_ts_now()
             log_multi(str(target), str(target.stats))
 
     def matcher_add_tagged_dirs(self, archive):

+ 2 - 3
src/borg/archiver/create_cmd.py

@@ -5,7 +5,6 @@ import os
 import stat
 import subprocess
 import time
-from datetime import datetime
 from io import TextIOWrapper
 
 from ._common import with_repository, Highlander
@@ -19,7 +18,7 @@ from ..compress import CompressionSpec
 from ..helpers import ChunkerParams
 from ..helpers import NameSpec, FilesCacheMode
 from ..helpers import eval_escapes
-from ..helpers import timestamp
+from ..helpers import timestamp, archive_ts_now
 from ..helpers import get_cache_dir, os_stat
 from ..helpers import dir_is_tagged
 from ..helpers import log_multi
@@ -209,7 +208,7 @@ class CreateMixIn:
         self.noxattrs = args.noxattrs
         self.exclude_nodump = args.exclude_nodump
         dry_run = args.dry_run
-        t0 = datetime.now().astimezone()  # local time with local timezone
+        t0 = archive_ts_now()
         t0_monotonic = time.monotonic()
         logger.info('Creating archive at "%s"' % args.location.processed)
         if not dry_run:

+ 2 - 3
src/borg/archiver/tar_cmds.py

@@ -1,6 +1,5 @@
 import argparse
 import base64
-from datetime import datetime
 import logging
 import os
 import stat
@@ -19,7 +18,7 @@ from ..helpers import ChunkIteratorFileWrapper
 from ..helpers import ChunkerParams
 from ..helpers import NameSpec
 from ..helpers import remove_surrogates
-from ..helpers import timestamp
+from ..helpers import timestamp, archive_ts_now
 from ..helpers import basic_json_data, json_print
 from ..helpers import log_multi
 from ..manifest import Manifest
@@ -255,7 +254,7 @@ class TarMixIn:
         return self.exit_code
 
     def _import_tar(self, args, repository, manifest, key, cache, tarstream):
-        t0 = datetime.now().astimezone()  # local time with local timezone
+        t0 = archive_ts_now()
         t0_monotonic = time.monotonic()
 
         archive = Archive(

+ 1 - 1
src/borg/helpers/__init__.py

@@ -35,7 +35,7 @@ from .process import signal_handler, raising_signal_handler, sig_int, ignore_sig
 from .process import popen_with_error_handling, is_terminal, prepare_subprocess_env, create_filter_process
 from .progress import ProgressIndicatorPercent, ProgressIndicatorEndless, ProgressIndicatorMessage
 from .time import parse_timestamp, timestamp, safe_timestamp, safe_s, safe_ns, MAX_S, SUPPORT_32BIT_PLATFORMS
-from .time import format_time, format_timedelta, OutputTimestamp
+from .time import format_time, format_timedelta, OutputTimestamp, archive_ts_now
 from .yes_no import yes, TRUISH, FALSISH, DEFAULTISH
 
 from .msgpack import is_slow_msgpack, is_supported_msgpack, get_limited_unpacker

+ 5 - 0
src/borg/helpers/time.py

@@ -123,3 +123,8 @@ class OutputTimestamp:
         return self.ts.isoformat(timespec="microseconds")
 
     to_json = isoformat
+
+
+def archive_ts_now():
+    """return tz-aware datetime obj for current time for usage as archive timestamp"""
+    return datetime.now().astimezone()  # local time / local timezone