mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Fix #5589
This commit is contained in:
parent
db67130999
commit
fefe3ca015
@ -8,10 +8,13 @@ __docformat__ = 'restructuredtext en'
|
|||||||
|
|
||||||
import re
|
import re
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from functools import partial
|
||||||
|
|
||||||
from dateutil.parser import parse
|
from dateutil.parser import parse
|
||||||
from dateutil.tz import tzlocal, tzutc
|
from dateutil.tz import tzlocal, tzutc
|
||||||
|
|
||||||
|
from calibre import strftime
|
||||||
|
|
||||||
class SafeLocalTimeZone(tzlocal):
|
class SafeLocalTimeZone(tzlocal):
|
||||||
'''
|
'''
|
||||||
Assume DST was not in effect for historical dates, if DST
|
Assume DST was not in effect for historical dates, if DST
|
||||||
@ -115,21 +118,27 @@ def utcnow():
|
|||||||
def utcfromtimestamp(stamp):
|
def utcfromtimestamp(stamp):
|
||||||
return datetime.utcfromtimestamp(stamp).replace(tzinfo=_utc_tz)
|
return datetime.utcfromtimestamp(stamp).replace(tzinfo=_utc_tz)
|
||||||
|
|
||||||
def format_date(dt, format):
|
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 dt.tzinfo is None:
|
||||||
|
dt = dt.replace(tzinfo=_utc_tz if assume_utc else
|
||||||
|
_local_tz)
|
||||||
|
dt = dt.astimezone(_utc_tz if as_utc else _local_tz)
|
||||||
|
strf = partial(strftime, t=dt.timetuple())
|
||||||
|
|
||||||
def format_day(mo):
|
def format_day(mo):
|
||||||
l = len(mo.group(0))
|
l = len(mo.group(0))
|
||||||
if l == 1: return '%d'%dt.day
|
if l == 1: return '%d'%dt.day
|
||||||
if l == 2: return '%02d'%dt.day
|
if l == 2: return '%02d'%dt.day
|
||||||
if l == 3: return dt.strftime('%a')
|
if l == 3: return strf('%a')
|
||||||
return dt.strftime('%A')
|
return strf('%A')
|
||||||
|
|
||||||
def format_month(mo):
|
def format_month(mo):
|
||||||
l = len(mo.group(0))
|
l = len(mo.group(0))
|
||||||
if l == 1: return '%d'%dt.month
|
if l == 1: return '%d'%dt.month
|
||||||
if l == 2: return '%02d'%dt.month
|
if l == 2: return '%02d'%dt.month
|
||||||
if l == 3: return dt.strftime('%b')
|
if l == 3: return strf('%b')
|
||||||
return dt.strftime('%B')
|
return strf('%B')
|
||||||
|
|
||||||
def format_year(mo):
|
def format_year(mo):
|
||||||
if len(mo.group(0)) == 2: return '%02d'%(dt.year % 100)
|
if len(mo.group(0)) == 2: return '%02d'%(dt.year % 100)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user