comparison sat/tools/common/date_utils.py @ 2797:7968c8b7b5e1

tools (common): fixed dateutil.parser import
author Goffi <goffi@goffi.org>
date Sat, 02 Feb 2019 01:12:54 +0100
parents 003b8b4b56a7
children 1b6547fb80da
comparison
equal deleted inserted replaced
2796:fdc53c8a5439 2797:7968c8b7b5e1
20 """tools to help manipulating time and dates""" 20 """tools to help manipulating time and dates"""
21 21
22 from sat.core.constants import Const as C 22 from sat.core.constants import Const as C
23 from sat.core.i18n import _ 23 from sat.core.i18n import _
24 import datetime 24 import datetime
25 import dateutil 25 from dateutil import tz, parser
26 from dateutil import tz
27 from dateutil.relativedelta import relativedelta 26 from dateutil.relativedelta import relativedelta
28 from dateutil.utils import default_tzinfo 27 from dateutil.utils import default_tzinfo
29 from babel import dates 28 from babel import dates
30 import calendar 29 import calendar
31 import time 30 import time
40 39
41 40
42 def date_parse(value, default_tz=TZ_UTC): 41 def date_parse(value, default_tz=TZ_UTC):
43 """Parse a date and return corresponding unix timestamp 42 """Parse a date and return corresponding unix timestamp
44 43
45 @param value(unicode): date to parse, in any format supported by dateutil.parser 44 @param value(unicode): date to parse, in any format supported by parser
46 @param default_tz(datetime.tzinfo): default timezone 45 @param default_tz(datetime.tzinfo): default timezone
47 @return (int): timestamp 46 @return (int): timestamp
48 """ 47 """
49 dt = default_tzinfo(dateutil.parser.parse(unicode(value), dayfirst=True), default_tz) 48 dt = default_tzinfo(parser.parse(unicode(value), dayfirst=True), default_tz)
50 return calendar.timegm(dt.utctimetuple()) 49 return calendar.timegm(dt.utctimetuple())
51 50
52 def date_parse_ext(value, default_tz=TZ_UTC): 51 def date_parse_ext(value, default_tz=TZ_UTC):
53 """Extended date parse which accept relative date 52 """Extended date parse which accept relative date
54 53
55 @param value(unicode): date to parse, in any format supported by dateutil.parser 54 @param value(unicode): date to parse, in any format supported by parser
56 and with the hability to specify X days/weeks/months/years in the past or future. 55 and with the hability to specify X days/weeks/months/years in the past or future.
57 Relative date are specified either with something like `[main_date] +1 week` 56 Relative date are specified either with something like `[main_date] +1 week`
58 or with something like `3 days ago`, and it is case insensitive. [main_date] is 57 or with something like `3 days ago`, and it is case insensitive. [main_date] is
59 a date parsable by dateutil.parser, or empty to specify current date/time. 58 a date parsable by parser, or empty to specify current date/time.
60 "now" can also be used to specify current date/time. 59 "now" can also be used to specify current date/time.
61 @param default_tz(datetime.tzinfo): same as for date_parse 60 @param default_tz(datetime.tzinfo): same as for date_parse
62 @return (int): timestamp 61 @return (int): timestamp
63 """ 62 """
64 m = RELATIVE_RE.match(value) 63 m = RELATIVE_RE.match(value)
76 75
77 date = m.group(u"date").strip().lower() 76 date = m.group(u"date").strip().lower()
78 if not date or date == u"now": 77 if not date or date == u"now":
79 dt = datetime.datetime.now(tz.tzutc()) 78 dt = datetime.datetime.now(tz.tzutc())
80 else: 79 else:
81 dt = default_tzinfo(dateutil.parser.parse(date, dayfirst=True)) 80 dt = default_tzinfo(parser.parse(date, dayfirst=True))
82 81
83 quantity = int(m.group(u"quantity")) 82 quantity = int(m.group(u"quantity"))
84 key = m.group(u"unit").lower() + u"s" 83 key = m.group(u"unit").lower() + u"s"
85 delta_kw = {key: direction * quantity} 84 delta_kw = {key: direction * quantity}
86 dt = dt + relativedelta(**delta_kw) 85 dt = dt + relativedelta(**delta_kw)