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