mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Add support for aP and Ap to python format_date as well
This commit is contained in:
parent
75b513834a
commit
a7c02a1fdf
@ -289,11 +289,8 @@ def timestampfromdt(dt, assume_utc=True):
|
|||||||
# Format date functions {{{
|
# Format date functions {{{
|
||||||
|
|
||||||
def fd_format_hour(dt, ampm, hr):
|
def fd_format_hour(dt, ampm, hr):
|
||||||
l = len(hr)
|
h = strftime('%I' if ampm else '%H', t=dt.timetuple())
|
||||||
h = dt.hour
|
if len(hr) == 1:
|
||||||
if ampm:
|
|
||||||
h = h%12
|
|
||||||
if l == 1:
|
|
||||||
return f'{h}'
|
return f'{h}'
|
||||||
return f'{h:02}'
|
return f'{h:02}'
|
||||||
|
|
||||||
@ -314,9 +311,9 @@ def fd_format_second(dt, ampm, sec):
|
|||||||
|
|
||||||
def fd_format_ampm(dt, ampm, ap):
|
def fd_format_ampm(dt, ampm, ap):
|
||||||
res = strftime('%p', t=dt.timetuple())
|
res = strftime('%p', t=dt.timetuple())
|
||||||
if ap == 'AP':
|
if ap in ('aP', 'Ap'):
|
||||||
return res
|
return res
|
||||||
return res.lower()
|
return res.upper() if ap in ('A', 'AP') else res.lower()
|
||||||
|
|
||||||
|
|
||||||
def fd_format_day(dt, ampm, dy):
|
def fd_format_day(dt, ampm, dy):
|
||||||
@ -364,16 +361,14 @@ def fd_repl_func(dt, ampm, mo):
|
|||||||
|
|
||||||
def format_date(dt, format, assume_utc=False, as_utc=False):
|
def format_date(dt, format, assume_utc=False, as_utc=False):
|
||||||
''' Return a date formatted as a string using a subset of Qt's formatting codes '''
|
''' Return a date formatted as a string using a subset of Qt's formatting codes '''
|
||||||
if not format:
|
format = format or 'dd MMM yyyy'
|
||||||
format = 'dd MMM yyyy'
|
|
||||||
|
|
||||||
if not isinstance(dt, datetime):
|
if not isinstance(dt, datetime):
|
||||||
dt = datetime.combine(dt, dtime())
|
dt = datetime.combine(dt, dtime())
|
||||||
|
|
||||||
if hasattr(dt, 'tzinfo'):
|
if hasattr(dt, 'tzinfo'):
|
||||||
if dt.tzinfo is None:
|
if dt.tzinfo is None:
|
||||||
dt = dt.replace(tzinfo=_utc_tz if assume_utc else
|
dt = dt.replace(tzinfo=_utc_tz if assume_utc else _local_tz)
|
||||||
_local_tz)
|
|
||||||
dt = dt.astimezone(_utc_tz if as_utc else _local_tz)
|
dt = dt.astimezone(_utc_tz if as_utc else _local_tz)
|
||||||
|
|
||||||
if format == 'iso':
|
if format == 'iso':
|
||||||
@ -384,7 +379,7 @@ def format_date(dt, format, assume_utc=False, as_utc=False):
|
|||||||
|
|
||||||
repl_func = partial(fd_repl_func, dt, 'ap' in format.lower())
|
repl_func = partial(fd_repl_func, dt, 'ap' in format.lower())
|
||||||
return re.sub(
|
return re.sub(
|
||||||
r'(s{1,2})|(m{1,2})|(h{1,2})|(ap)|(AP)|(d{1,4}|M{1,4}|(?:yyyy|yy))',
|
r'(s{1,2})|(m{1,2})|(h{1,2})|(ap)|(AP)|(aP)|(Ap)|(d{1,4}|M{1,4}|(?:yyyy|yy))',
|
||||||
repl_func, format)
|
repl_func, format)
|
||||||
|
|
||||||
# }}}
|
# }}}
|
||||||
|
@ -350,18 +350,18 @@ def atomic_write(base, name, content):
|
|||||||
|
|
||||||
|
|
||||||
def run_rapydscript_tests():
|
def run_rapydscript_tests():
|
||||||
|
from calibre.gui2 import must_use_qt
|
||||||
|
must_use_qt()
|
||||||
|
from calibre.utils.webengine import create_script, insert_scripts, secure_webengine, setup_default_profile, setup_fake_protocol, setup_profile
|
||||||
|
setup_fake_protocol()
|
||||||
|
setup_default_profile()
|
||||||
from urllib.parse import parse_qs
|
from urllib.parse import parse_qs
|
||||||
|
|
||||||
from qt.core import QApplication, QByteArray, QEventLoop, QUrl
|
from qt.core import QApplication, QByteArray, QEventLoop, QUrl
|
||||||
from qt.webengine import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineUrlRequestJob, QWebEngineUrlSchemeHandler
|
from qt.webengine import QWebEnginePage, QWebEngineProfile, QWebEngineScript, QWebEngineUrlRequestJob, QWebEngineUrlSchemeHandler
|
||||||
|
|
||||||
from calibre.constants import FAKE_HOST, FAKE_PROTOCOL
|
from calibre.constants import FAKE_HOST, FAKE_PROTOCOL
|
||||||
from calibre.gui2 import must_use_qt
|
|
||||||
from calibre.gui2.viewer.web_view import send_reply
|
from calibre.gui2.viewer.web_view import send_reply
|
||||||
from calibre.utils.webengine import create_script, insert_scripts, secure_webengine, setup_default_profile, setup_fake_protocol, setup_profile
|
|
||||||
must_use_qt()
|
|
||||||
setup_fake_protocol()
|
|
||||||
setup_default_profile()
|
|
||||||
|
|
||||||
base = base_dir()
|
base = base_dir()
|
||||||
rapydscript_dir = os.path.join(base, 'src', 'pyj')
|
rapydscript_dir = os.path.join(base, 'src', 'pyj')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user