From 5228e61a0ef00e4731c0d0de9e7f468c5da9405f Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 13 Aug 2022 09:05:53 +0530 Subject: [PATCH] Content server: Fix formatting of dates for custom columns using ISO format. Fixes #1986412 [Content Server: iso date format break in book details page](https://bugs.launchpad.net/calibre/+bug/1986412) --- src/pyj/date.pyj | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/pyj/date.pyj b/src/pyj/date.pyj index c5d0730173..26cfe8b408 100644 --- a/src/pyj/date.pyj +++ b/src/pyj/date.pyj @@ -132,6 +132,20 @@ def am_pm_pat(): return ans +def as_iso(date): + tzo = -date.getTimezoneOffset() + dif = '+' if tzo >= 0 else '-' + + def pad(num): + return ('0' if num < 10 else '') + num + + return ( + date.getFullYear() + '-' + pad(date.getMonth() + 1) + '-' + pad(date.getDate()) + ' ' + pad(date.getHours()) + + ':' + pad(date.getMinutes()) + ':' + pad(date.getSeconds()) + dif + pad(Math.floor(Math.abs(tzo) / 60)) + + ':' + pad(Math.abs(tzo) % 60) + ) + + def format_date(date=None, fmt='dd MMM yyyy', as_utc=False): fmt = fmt or 'dd MMM yyyy' ampm = am_pm_pat().test(fmt) @@ -140,6 +154,8 @@ def format_date(date=None, fmt='dd MMM yyyy', as_utc=False): date = date or Date() if is_date_undefined(date): return '' + if fmt is 'iso': + return as_iso(date) return fmt.replace(fmt_date_pat(), def(match): if not match: return ''