tools(common/date_utils): fixed order on year first + set default date for date_parse
This commit is contained in:
parent
b2ce9a8725
commit
309e5eebb9
1 changed files with 9 additions and 2 deletions
|
|
@ -33,9 +33,11 @@ import re
|
|||
RELATIVE_RE = re.compile(ur"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *"
|
||||
ur"(?P<unit>(second|minute|hour|day|week|month|year))s?"
|
||||
ur"(?P<ago> +ago)?", re.I)
|
||||
|
||||
YEAR_FIRST_RE = re.compile(ur"\d{4}[^\d]+")
|
||||
TZ_UTC = tz.tzutc()
|
||||
TZ_LOCAL = tz.gettz()
|
||||
# used to replace values when something is missing
|
||||
DEFAULT_DATETIME = datetime.datetime(2000, 01, 01)
|
||||
|
||||
|
||||
def date_parse(value, default_tz=TZ_UTC):
|
||||
|
|
@ -45,7 +47,12 @@ def date_parse(value, default_tz=TZ_UTC):
|
|||
@param default_tz(datetime.tzinfo): default timezone
|
||||
@return (int): timestamp
|
||||
"""
|
||||
dt = default_tzinfo(parser.parse(unicode(value), dayfirst=True), default_tz)
|
||||
value = unicode(value).strip()
|
||||
dayfirst = False if YEAR_FIRST_RE.match(value) else True
|
||||
|
||||
dt = default_tzinfo(
|
||||
parser.parse(value, default=DEFAULT_DATETIME, dayfirst=dayfirst),
|
||||
default_tz)
|
||||
return calendar.timegm(dt.utctimetuple())
|
||||
|
||||
def date_parse_ext(value, default_tz=TZ_UTC):
|
||||
|
|
|
|||
Loading…
Reference in a new issue