Mercurial > libervia-backend
comparison sat/tools/common/date_utils.py @ 3028:ab2696e34d29
Python 3 port:
/!\ this is a huge commit
/!\ starting from this commit, SàT is needs Python 3.6+
/!\ SàT maybe be instable or some feature may not work anymore, this will improve with time
This patch port backend, bridge and frontends to Python 3.
Roughly this has been done this way:
- 2to3 tools has been applied (with python 3.7)
- all references to python2 have been replaced with python3 (notably shebangs)
- fixed files not handled by 2to3 (notably the shell script)
- several manual fixes
- fixed issues reported by Python 3 that where not handled in Python 2
- replaced "async" with "async_" when needed (it's a reserved word from Python 3.7)
- replaced zope's "implements" with @implementer decorator
- temporary hack to handle data pickled in database, as str or bytes may be returned,
to be checked later
- fixed hash comparison for password
- removed some code which is not needed anymore with Python 3
- deactivated some code which needs to be checked (notably certificate validation)
- tested with jp, fixed reported issues until some basic commands worked
- ported Primitivus (after porting dependencies like urwid satext)
- more manual fixes
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 13 Aug 2019 19:08:41 +0200 |
parents | 1b6547fb80da |
children | 9d0df638c8b4 |
comparison
equal
deleted
inserted
replaced
3027:ff5bcb12ae60 | 3028:ab2696e34d29 |
---|---|
1 #!/usr/bin/env python2 | 1 #!/usr/bin/env python3 |
2 # -*- coding: utf-8 -*- | 2 # -*- coding: utf-8 -*- |
3 | 3 |
4 # SAT: a jabber client | 4 # SAT: a jabber client |
5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) | 5 # Copyright (C) 2009-2019 Jérôme Poisson (goffi@goffi.org) |
6 | 6 |
28 from babel import dates | 28 from babel import dates |
29 import calendar | 29 import calendar |
30 import time | 30 import time |
31 import re | 31 import re |
32 | 32 |
33 RELATIVE_RE = re.compile(ur"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *" | 33 RELATIVE_RE = re.compile(r"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *" |
34 ur"(?P<unit>(second|minute|hour|day|week|month|year))s?" | 34 r"(?P<unit>(second|minute|hour|day|week|month|year))s?" |
35 ur"(?P<ago> +ago)?", re.I) | 35 r"(?P<ago> +ago)?", re.I) |
36 YEAR_FIRST_RE = re.compile(ur"\d{4}[^\d]+") | 36 YEAR_FIRST_RE = re.compile(r"\d{4}[^\d]+") |
37 TZ_UTC = tz.tzutc() | 37 TZ_UTC = tz.tzutc() |
38 TZ_LOCAL = tz.gettz() | 38 TZ_LOCAL = tz.gettz() |
39 # used to replace values when something is missing | 39 # used to replace values when something is missing |
40 DEFAULT_DATETIME = datetime.datetime(2000, 01, 01) | 40 DEFAULT_DATETIME = datetime.datetime(2000, 0o1, 0o1) |
41 | 41 |
42 | 42 |
43 def date_parse(value, default_tz=TZ_UTC): | 43 def date_parse(value, default_tz=TZ_UTC): |
44 """Parse a date and return corresponding unix timestamp | 44 """Parse a date and return corresponding unix timestamp |
45 | 45 |
46 @param value(unicode): date to parse, in any format supported by parser | 46 @param value(unicode): date to parse, in any format supported by parser |
47 @param default_tz(datetime.tzinfo): default timezone | 47 @param default_tz(datetime.tzinfo): default timezone |
48 @return (int): timestamp | 48 @return (int): timestamp |
49 """ | 49 """ |
50 value = unicode(value).strip() | 50 value = str(value).strip() |
51 dayfirst = False if YEAR_FIRST_RE.match(value) else True | 51 dayfirst = False if YEAR_FIRST_RE.match(value) else True |
52 | 52 |
53 dt = default_tzinfo( | 53 dt = default_tzinfo( |
54 parser.parse(value, default=DEFAULT_DATETIME, dayfirst=dayfirst), | 54 parser.parse(value, default=DEFAULT_DATETIME, dayfirst=dayfirst), |
55 default_tz) | 55 default_tz) |
69 """ | 69 """ |
70 m = RELATIVE_RE.match(value) | 70 m = RELATIVE_RE.match(value) |
71 if m is None: | 71 if m is None: |
72 return date_parse(value, default_tz=default_tz) | 72 return date_parse(value, default_tz=default_tz) |
73 | 73 |
74 if m.group(u"direction") and m.group(u"ago"): | 74 if m.group("direction") and m.group("ago"): |
75 raise ValueError( | 75 raise ValueError( |
76 _(u"You can't use a direction (+ or -) and \"ago\" at the same time")) | 76 _("You can't use a direction (+ or -) and \"ago\" at the same time")) |
77 | 77 |
78 if m.group(u"direction") == u'-' or m.group(u"ago"): | 78 if m.group("direction") == '-' or m.group("ago"): |
79 direction = -1 | 79 direction = -1 |
80 else: | 80 else: |
81 direction = 1 | 81 direction = 1 |
82 | 82 |
83 date = m.group(u"date").strip().lower() | 83 date = m.group("date").strip().lower() |
84 if not date or date == u"now": | 84 if not date or date == "now": |
85 dt = datetime.datetime.now(tz.tzutc()) | 85 dt = datetime.datetime.now(tz.tzutc()) |
86 else: | 86 else: |
87 dt = default_tzinfo(parser.parse(date, dayfirst=True)) | 87 dt = default_tzinfo(parser.parse(date, dayfirst=True)) |
88 | 88 |
89 quantity = int(m.group(u"quantity")) | 89 quantity = int(m.group("quantity")) |
90 key = m.group(u"unit").lower() + u"s" | 90 key = m.group("unit").lower() + "s" |
91 delta_kw = {key: direction * quantity} | 91 delta_kw = {key: direction * quantity} |
92 dt = dt + relativedelta(**delta_kw) | 92 dt = dt + relativedelta(**delta_kw) |
93 return calendar.timegm(dt.utctimetuple()) | 93 return calendar.timegm(dt.utctimetuple()) |
94 | 94 |
95 | 95 |