This commit is contained in:
Kovid Goyal 2010-05-22 09:39:10 -06:00
parent db67130999
commit fefe3ca015

View File

@ -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)