mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
utility function to generate datetime from date strings such that the year and month are always correct
This commit is contained in:
parent
4a2de2c905
commit
d49d82305f
@ -18,7 +18,7 @@ from calibre.ebooks.metadata import check_isbn
|
|||||||
from calibre.ebooks.metadata.sources.base import (Source, Option, fixcase,
|
from calibre.ebooks.metadata.sources.base import (Source, Option, fixcase,
|
||||||
fixauthors)
|
fixauthors)
|
||||||
from calibre.ebooks.metadata.book.base import Metadata
|
from calibre.ebooks.metadata.book.base import Metadata
|
||||||
from calibre.utils.date import parse_date
|
from calibre.utils.date import parse_only_date
|
||||||
from calibre.utils.localization import canonicalize_lang
|
from calibre.utils.localization import canonicalize_lang
|
||||||
|
|
||||||
class Worker(Thread): # Get details {{{
|
class Worker(Thread): # Get details {{{
|
||||||
@ -471,7 +471,7 @@ class Worker(Thread): # Get details {{{
|
|||||||
ans = x.tail
|
ans = x.tail
|
||||||
date = ans.rpartition('(')[-1].replace(')', '').strip()
|
date = ans.rpartition('(')[-1].replace(')', '').strip()
|
||||||
date = self.delocalize_datestr(date)
|
date = self.delocalize_datestr(date)
|
||||||
return parse_date(date, assume_utc=True)
|
return parse_only_date(date, assume_utc=True)
|
||||||
|
|
||||||
def parse_language(self, pd):
|
def parse_language(self, pd):
|
||||||
for x in reversed(pd.xpath(self.language_xpath)):
|
for x in reversed(pd.xpath(self.language_xpath)):
|
||||||
|
@ -82,6 +82,26 @@ def parse_date(date_string, assume_utc=False, as_utc=True, default=None):
|
|||||||
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)
|
||||||
|
|
||||||
|
def parse_only_date(raw, assume_utc=True):
|
||||||
|
'''
|
||||||
|
Parse a date string that contains no time information in a manner that
|
||||||
|
guarantees that the month and year are always correct in all timezones, and
|
||||||
|
the day is at most one day wrong.
|
||||||
|
'''
|
||||||
|
from calibre.utils.date import utcnow, now, parse_date
|
||||||
|
from datetime import timedelta
|
||||||
|
f = utcnow if assume_utc else now
|
||||||
|
default = f().replace(hour=0, minute=0, second=0, microsecond=0,
|
||||||
|
day=15)
|
||||||
|
ans = parse_date(raw, default=default, assume_utc=assume_utc)
|
||||||
|
n = ans + timedelta(days=1)
|
||||||
|
if n.month > ans.month:
|
||||||
|
ans = ans.replace(day=ans.day-1)
|
||||||
|
if ans.day == 1:
|
||||||
|
ans = ans.replace(day=2)
|
||||||
|
return ans
|
||||||
|
|
||||||
|
|
||||||
def strptime(val, fmt, assume_utc=False, as_utc=True):
|
def strptime(val, fmt, assume_utc=False, as_utc=True):
|
||||||
dt = datetime.strptime(val, fmt)
|
dt = datetime.strptime(val, fmt)
|
||||||
if dt.tzinfo is None:
|
if dt.tzinfo is None:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user