[postprocessor:mtime] fix timestamps from datetime objects (#2307)

'datetime.timestamp()', which got used to convert datetime objects to
POSIX timestamps, assumes naive datetimes represent LOCAL time, while
datetimes in 'date' metadata fields represent UTC time.

Ref: https://docs.python.org/3/library/datetime.html#datetime.datetime.timestamp
> Naive datetime instances are assumed to represent local time
> you can obtain the POSIX timestamp by … calculating the timestamp directly
This commit is contained in:
Mike Fährmann
2022-03-23 23:05:14 +01:00
parent 29db716a63
commit e7b30866d0
2 changed files with 10 additions and 6 deletions

View File

@@ -9,7 +9,8 @@
"""Use metadata as file modification time""" """Use metadata as file modification time"""
from .common import PostProcessor from .common import PostProcessor
from ..text import parse_int from .. import text, util
from datetime import datetime
class MtimePP(PostProcessor): class MtimePP(PostProcessor):
@@ -27,8 +28,11 @@ class MtimePP(PostProcessor):
def run(self, pathfmt): def run(self, pathfmt):
mtime = pathfmt.kwdict.get(self.key) mtime = pathfmt.kwdict.get(self.key)
ts = getattr(mtime, "timestamp", None) pathfmt.kwdict["_mtime"] = (
pathfmt.kwdict["_mtime"] = ts() if ts else parse_int(mtime) util.datetime_to_timestamp(mtime)
if isinstance(mtime, datetime) else
text.parse_int(mtime)
)
__postprocessor__ = MtimePP __postprocessor__ = MtimePP

View File

@@ -1,7 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# Copyright 2019-2021 Mike Fährmann # Copyright 2019-2022 Mike Fährmann
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as # it under the terms of the GNU General Public License version 2 as
@@ -16,7 +16,7 @@ import logging
import zipfile import zipfile
import tempfile import tempfile
import collections import collections
from datetime import datetime, timezone as tz from datetime import datetime
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from gallery_dl import extractor, output, path # noqa E402 from gallery_dl import extractor, output, path # noqa E402
@@ -345,7 +345,7 @@ class MtimeTest(BasePostprocessorTest):
self.assertEqual(pp.key, "date") self.assertEqual(pp.key, "date")
def test_mtime_datetime(self): def test_mtime_datetime(self):
self._create(None, {"date": datetime(1980, 1, 1, tzinfo=tz.utc)}) self._create(None, {"date": datetime(1980, 1, 1)})
self._trigger() self._trigger()
self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800) self.assertEqual(self.pathfmt.kwdict["_mtime"], 315532800)