diff --git a/beets/library.py b/beets/library.py index 399048612..9721d5e6b 100644 --- a/beets/library.py +++ b/beets/library.py @@ -37,6 +37,10 @@ import beets MAX_FILENAME_LENGTH = 200 +# This is the default format when printing the import time +# of an object. This needs to be a format accepted by time.strftime() +ITIME_FORMAT = '%Y-%m-%d %H:%M:%S' + # Fields in the "items" database table; all the metadata available for # items in the library. These are used directly in SQL; they are # vulnerable to injection if accessible to the user. @@ -398,6 +402,9 @@ class Item(object): if not sanitize: mapping['path'] = displayable_path(self.path) + # Convert the import time to human readable + mapping['itime'] = time.strftime(ITIME_FORMAT, time.localtime(getattr(self, 'itime'))) + # Use the album artist if the track artist is not set and # vice-versa. if not mapping['artist']: @@ -1741,6 +1748,9 @@ class Album(BaseAlbum): mapping['artpath'] = displayable_path(mapping['artpath']) mapping['path'] = displayable_path(self.item_dir()) + # Convert the import time to human readable format + mapping['itime'] = time.strftime(ITIME_FORMAT, time.localtime(mapping['itime'])) + # Get template functions. funcs = DefaultTemplateFunctions().functions() funcs.update(plugins.template_funcs()) @@ -1829,6 +1839,12 @@ class DefaultTemplateFunctions(object): """ return unidecode(s) + @staticmethod + def tmpl_format(s, format): + """Format the import time to any format according to time.strfime() + """ + return time.strftime(format, time.strptime(s, ITIME_FORMAT)) + def tmpl_aunique(self, keys=None, disam=None): """Generate a string that is guaranteed to be unique among all albums in the library who share the same set of keys. A fields diff --git a/docs/reference/pathformat.rst b/docs/reference/pathformat.rst index 13661d9d8..2474c97d5 100644 --- a/docs/reference/pathformat.rst +++ b/docs/reference/pathformat.rst @@ -56,7 +56,7 @@ track's artists. These functions are built in to beets: -* ``%lower{text}``: Convert ``text`` to lowercase. +* ``%lower{text}``: Convert ``text`` to lowercase. * ``%upper{text}``: Convert ``text`` to UPPERCASE. * ``%title{text}``: Convert ``text`` to Title Case. * ``%left{text,n}``: Return the first ``n`` characters of ``text``. @@ -70,8 +70,12 @@ These functions are built in to beets: `unidecode module`_. * ``%aunique{identifiers,disambiguators}``: Provides a unique string to disambiguate similar albums in the database. See :ref:`aunique`, below. +* ``%format{date_time,format}``: Return the date and time in any format accepted + by the `time.strfime() method`_. Should probably be used together with the + ``itime`` field (import time). .. _unidecode module: http://pypi.python.org/pypi/Unidecode +.. _time.strftime() method: http://docs.python.org/2/library/time.html#time.strftime Plugins can extend beets with more template functions (see :ref:`writing-plugins`).