Skip to content

Commit

Permalink
Add mtime to ListEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
kovidgoyal committed Jan 21, 2025
1 parent 9ab99b3 commit fe4e0aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
6 changes: 4 additions & 2 deletions src/calibre/devices/mtp/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
__copyright__ = '2012, Kovid Goyal <kovid at kovidgoyal.net>'
__docformat__ = 'restructuredtext en'

import datetime
import importlib
import json
import os
Expand All @@ -19,7 +20,7 @@
from calibre.devices.errors import PathError
from calibre.devices.mtp.base import debug
from calibre.devices.mtp.defaults import DeviceDefaults
from calibre.devices.mtp.filesystem_cache import FileOrFolder
from calibre.devices.mtp.filesystem_cache import FileOrFolder, convert_timestamp
from calibre.ptempfile import PersistentTemporaryDirectory, SpooledTemporaryFile
from calibre.utils.filenames import shorten_components_to
from calibre.utils.icu import lower as icu_lower
Expand All @@ -41,6 +42,7 @@ class ListEntry(NamedTuple):
name: str
is_folder: bool
size: int
mtime: datetime.datetime


class MTP_DEVICE(BASE):
Expand Down Expand Up @@ -389,7 +391,7 @@ def get_file_by_name(self, outfile, parent, *names):

def list_folder_by_name(self, parent, *names):
' List the contents of the folder parent/ + "/".join(names). Works with folders not cached in FilesystemCache. '
return tuple(ListEntry(x['name'], x['is_folder'], x['size']) for x in self.list_mtp_folder_by_name(parent, *names))
return tuple(ListEntry(x['name'], x['is_folder'], x['size'], convert_timestamp(x['modified'])) for x in self.list_mtp_folder_by_name(parent, *names))

def prepare_addable_books(self, paths):
tdir = PersistentTemporaryDirectory('_prepare_mtp')
Expand Down
17 changes: 10 additions & 7 deletions src/calibre/devices/mtp/filesystem_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@

bexts = frozenset(BOOK_EXTENSIONS) - {'mbp', 'tan', 'rar', 'zip', 'xml'}

def convert_timestamp(md):
try:
if isinstance(md, tuple):
return datetime(*(list(md)+[local_tz]))
else:
return datetime.fromtimestamp(md, local_tz)
except Exception:
return datetime.fromtimestamp(0, local_tz)


class ListEntry:

Expand Down Expand Up @@ -55,13 +64,7 @@ def __init__(self, entry, fs_cache: 'FilesystemCache', is_storage: bool = False)
self.name = force_unicode(n, 'utf-8')
self.size = entry.get('size', 0)
md = entry.get('modified', 0)
try:
if isinstance(md, tuple):
self.last_modified = datetime(*(list(md)+[local_tz]))
else:
self.last_modified = datetime.fromtimestamp(md, local_tz)
except Exception:
self.last_modified = datetime.fromtimestamp(0, local_tz)
self.last_modified = convert_timestamp(md)
self.last_mod_string = self.last_modified.strftime('%Y/%m/%d %H:%M')
self.last_modified = as_utc(self.last_modified)

Expand Down

0 comments on commit fe4e0aa

Please sign in to comment.