From 0cc37216b4b17678541dd5ea157b9f7dc5ed6cec Mon Sep 17 00:00:00 2001 From: Charles Haley <> Date: Thu, 14 Apr 2011 12:18:41 +0100 Subject: [PATCH] Add a new date format code 'iso'. Permits formatting dates to see the complete time. --- src/calibre/library/field_metadata.py | 4 ++-- src/calibre/manual/template_lang.rst | 19 ++++++++++--------- src/calibre/utils/date.py | 4 ++++ src/calibre/utils/formatter_functions.py | 3 ++- 4 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/calibre/library/field_metadata.py b/src/calibre/library/field_metadata.py index ae91283523..33929ac2e4 100644 --- a/src/calibre/library/field_metadata.py +++ b/src/calibre/library/field_metadata.py @@ -364,11 +364,11 @@ class FieldMetadata(dict): self._tb_cats[k]['display'] = {} self._tb_cats[k]['is_editable'] = True self._add_search_terms_to_map(k, v['search_terms']) - for x in ('timestamp', 'last_modified'): - self._tb_cats[x]['display'] = { + self._tb_cats['timestamp']['display'] = { 'date_format': tweaks['gui_timestamp_display_format']} self._tb_cats['pubdate']['display'] = { 'date_format': tweaks['gui_pubdate_display_format']} + self._tb_cats['last_modified']['display'] = {'date_format': 'iso'} self.custom_field_prefix = '#' self.get = self._tb_cats.get diff --git a/src/calibre/manual/template_lang.rst b/src/calibre/manual/template_lang.rst index c6e29e3915..cdb8df2e2b 100644 --- a/src/calibre/manual/template_lang.rst +++ b/src/calibre/manual/template_lang.rst @@ -236,15 +236,16 @@ The following functions are available in addition to those described in single-f * ``format_date(x, date_format)`` -- format_date(val, format_string) -- format the value, which must be a date field, using the format_string, returning a string. The formatting codes are:: d : the day as number without a leading zero (1 to 31) - dd : the day as number with a leading zero (01 to 31) ' - ddd : the abbreviated localized day name (e.g. "Mon" to "Sun"). ' - dddd : the long localized day name (e.g. "Monday" to "Sunday"). ' - M : the month as number without a leading zero (1 to 12). ' - MM : the month as number with a leading zero (01 to 12) ' - MMM : the abbreviated localized month name (e.g. "Jan" to "Dec"). ' - MMMM : the long localized month name (e.g. "January" to "December"). ' - yy : the year as two digit number (00 to 99). ' - yyyy : the year as four digit number.' + dd : the day as number with a leading zero (01 to 31) + ddd : the abbreviated localized day name (e.g. "Mon" to "Sun"). + dddd : the long localized day name (e.g. "Monday" to "Sunday"). + M : the month as number without a leading zero (1 to 12). + MM : the month as number with a leading zero (01 to 12) + MMM : the abbreviated localized month name (e.g. "Jan" to "Dec"). + MMMM : the long localized month name (e.g. "January" to "December"). + yy : the year as two digit number (00 to 99). + yyyy : the year as four digit number. + iso : the date with time and timezone. Must be the only format present. * ``eval(string)`` -- evaluates the string as a program, passing the local variables (those ``assign`` ed to). This permits using the template processor to construct complex results from local variables. * ``multiply(x, y)`` -- returns x * y. Throws an exception if either x or y are not numbers. diff --git a/src/calibre/utils/date.py b/src/calibre/utils/date.py index 9b76a5a71a..c35e8ee2ab 100644 --- a/src/calibre/utils/date.py +++ b/src/calibre/utils/date.py @@ -142,6 +142,10 @@ def format_date(dt, format, assume_utc=False, as_utc=False): dt = dt.replace(tzinfo=_utc_tz if assume_utc else _local_tz) dt = dt.astimezone(_utc_tz if as_utc else _local_tz) + + if format == 'iso': + return isoformat(dt, assume_utc=assume_utc, as_utc=as_utc) + strf = partial(strftime, t=dt.timetuple()) def format_day(mo): diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index 015a639af1..7957bd0749 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -504,7 +504,8 @@ class BuiltinFormat_date(BuiltinFormatterFunction): 'MMM : the abbreviated localized month name (e.g. "Jan" to "Dec"). ' 'MMMM : the long localized month name (e.g. "January" to "December"). ' 'yy : the year as two digit number (00 to 99). ' - 'yyyy : the year as four digit number.') + 'yyyy : the year as four digit number. ' + 'iso : the date with time and timezone. Must be the only format present') def evaluate(self, formatter, kwargs, mi, locals, val, format_string): if not val: