mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
QDateTime->datetime faster and more robust
Fix editing of book metadata failing when its timestamp is out of range for the system. Fixes #1191599 [Cannot edit simple ebook metadatas](https://bugs.launchpad.net/calibre/+bug/1191599)
This commit is contained in:
parent
340fda187c
commit
d62006bc11
@ -7,7 +7,7 @@ __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
|
|||||||
__docformat__ = 'restructuredtext en'
|
__docformat__ = 'restructuredtext en'
|
||||||
|
|
||||||
import re, time
|
import re, time
|
||||||
from datetime import datetime, time as dtime, timedelta
|
from datetime import datetime, time as dtime, timedelta, MINYEAR, MAXYEAR
|
||||||
from functools import partial
|
from functools import partial
|
||||||
|
|
||||||
from dateutil.tz import tzlocal, tzutc, EPOCHORDINAL
|
from dateutil.tz import tzlocal, tzutc, EPOCHORDINAL
|
||||||
@ -141,14 +141,26 @@ def dt_factory(time_t, assume_utc=False, as_utc=True):
|
|||||||
dt = dt.replace(tzinfo=_utc_tz if assume_utc else _local_tz)
|
dt = dt.replace(tzinfo=_utc_tz if assume_utc else _local_tz)
|
||||||
return dt.astimezone(_utc_tz if as_utc else _local_tz)
|
return dt.astimezone(_utc_tz if as_utc else _local_tz)
|
||||||
|
|
||||||
|
safeyear = lambda x: min(max(x, MINYEAR), MAXYEAR)
|
||||||
|
|
||||||
def qt_to_dt(qdate_or_qdatetime, as_utc=True):
|
def qt_to_dt(qdate_or_qdatetime, as_utc=True):
|
||||||
from PyQt4.Qt import Qt
|
|
||||||
o = qdate_or_qdatetime
|
o = qdate_or_qdatetime
|
||||||
if hasattr(o, 'toUTC'):
|
if hasattr(o, 'toUTC'):
|
||||||
# QDateTime
|
# QDateTime
|
||||||
o = unicode(o.toUTC().toString(Qt.ISODate))
|
o = o.toUTC()
|
||||||
return parse_date(o, assume_utc=True, as_utc=as_utc)
|
d, t = o.date(), o.time()
|
||||||
dt = datetime(o.year(), o.month(), o.day()).replace(tzinfo=_local_tz)
|
try:
|
||||||
|
ans = datetime(safeyear(d.year()), d.month(), d.day(), t.hour(), t.minute(), t.second(), t.msec()*1000, utc_tz)
|
||||||
|
except ValueError:
|
||||||
|
ans = datetime(safeyear(d.year()), d.month(), 1, t.hour(), t.minute(), t.second(), t.msec()*1000, utc_tz)
|
||||||
|
if not as_utc:
|
||||||
|
ans = ans.astimezone(local_tz)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
try:
|
||||||
|
dt = datetime(safeyear(o.year()), o.month(), o.day()).replace(tzinfo=_local_tz)
|
||||||
|
except ValueError:
|
||||||
|
dt = datetime(safeyear(o.year()), o.month(), 1).replace(tzinfo=_local_tz)
|
||||||
return dt.astimezone(_utc_tz if as_utc else _local_tz)
|
return dt.astimezone(_utc_tz if as_utc else _local_tz)
|
||||||
|
|
||||||
def fromtimestamp(ctime, as_utc=True):
|
def fromtimestamp(ctime, as_utc=True):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user