From 75b513834a8d889ba96c81edbf1e04689ad7763d Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 5 Apr 2025 10:13:54 +0530 Subject: [PATCH] E-book viewer: Fix a regression in 8.0 that caused the clock to display an extra p after the time. Fixes #2106280 [extraneous letter 'p' in the clock](https://bugs.launchpad.net/calibre/+bug/2106280) Apparently Qt has now started using Ap and aP as AM/PM formatters. Sigh. --- src/pyj/date.pyj | 24 ++++++++++++++++++++++-- src/pyj/test_date.pyj | 6 ++++-- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/pyj/date.pyj b/src/pyj/date.pyj index bf2fe51bca..298bc5369a 100644 --- a/src/pyj/date.pyj +++ b/src/pyj/date.pyj @@ -33,7 +33,7 @@ except: def fmt_date_pat(): ans = fmt_date_pat.ans if not ans?: - ans = fmt_date_pat.ans = /(s{1,2})|(m{1,2})|(h{1,2})|(H{1,2})|(zzz)|(z)|(ap)|(AP)|(a)|(A)|(t)|(d{1,4}|M{1,4}|(?:yyyy|yy))/g + ans = fmt_date_pat.ans = /(s{1,2})|(m{1,2})|(h{1,2})|(H{1,2})|(zzz)|(z)|(ap)|(AP)|(Ap)|(aP)|(a)|(A)|(t)|(d{1,4}|M{1,4}|(?:yyyy|yy))/g return ans @@ -73,9 +73,25 @@ def fd_format_timezone(dt, ampm, t, as_utc): return window.Intl.DateTimeFormat().resolvedOptions().timeZone +def get_ampm_text(which): + ans = get_ampm_text[which] + if not ans: + d = Date() + if which is 'am': + d.setHours(1, 0, 0, 0) + else: + d.setHours(13, 0, 0, 0) + x = window.Intl.DateTimeFormat('default', { 'hour': 'numeric', 'hour12': True }).format(d) + ans = get_ampm_text[which] = x.replace(/\d+/g, '').trim() + return ans + + def fd_format_ampm(dt, ampm, ap, as_utc): h = dt.getUTCHours() if as_utc else dt.getHours() ans = 'am' if h < 12 else 'pm' + ans = get_ampm_text(ans) + if ans is 'Ap' or ans is 'aP': + return ans return ans if (ap is 'ap' or ap is 'a') else ans.toUpperCase() @@ -122,6 +138,10 @@ fd_function_index = { 's': fd_format_second, 'a': fd_format_ampm, 'A': fd_format_ampm, + 'ap': fd_format_ampm, + 'AP': fd_format_ampm, + 'Ap': fd_format_ampm, + 'aP': fd_format_ampm, 'z': fd_format_ms, 't': fd_format_timezone, } @@ -130,7 +150,7 @@ fd_function_index = { def am_pm_pat(): ans = am_pm_pat.ans if not ans?: - ans = am_pm_pat.ans = /(ap)|(a)|(AP)|(A)/ + ans = am_pm_pat.ans = /(ap)|(AP)|(Ap)|(aP)|(a)|(A)/ return ans diff --git a/src/pyj/test_date.pyj b/src/pyj/test_date.pyj index 40f22a99a8..453fbaa536 100644 --- a/src/pyj/test_date.pyj +++ b/src/pyj/test_date.pyj @@ -2,8 +2,8 @@ # License: GPL v3 Copyright: 2020, Kovid Goyal from __python__ import bound_methods, hash_literals -from date import format_date -from testing import test, assert_equal +from date import format_date, get_ampm_text +from testing import test, assert_equal, assert_true def test_fd(date, fmt, ans): @@ -12,6 +12,8 @@ def test_fd(date, fmt, ans): @test def date_formatting(): + assert_true(get_ampm_text('am')) + assert_true(get_ampm_text('pm')) test_fd('1101-01-01T09:00:00+00:00', 'hh h', '09 9') test_fd('1101-01-01T12:15:00+00:00', 'h:m ap', '12:15 pm') test_fd('1101-01-01T00:15:00+00:00', 'h:m ap', '12:15 am')