diff 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
line wrap: on
line diff
--- a/sat/tools/common/date_utils.py	Wed Jul 31 11:31:22 2019 +0200
+++ b/sat/tools/common/date_utils.py	Tue Aug 13 19:08:41 2019 +0200
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python3
 # -*- coding: utf-8 -*-
 
 # SAT: a jabber client
@@ -30,14 +30,14 @@
 import time
 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]+")
+RELATIVE_RE = re.compile(r"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *"
+                         r"(?P<unit>(second|minute|hour|day|week|month|year))s?"
+                         r"(?P<ago> +ago)?", re.I)
+YEAR_FIRST_RE = re.compile(r"\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)
+DEFAULT_DATETIME = datetime.datetime(2000, 0o1, 0o1)
 
 
 def date_parse(value, default_tz=TZ_UTC):
@@ -47,7 +47,7 @@
     @param default_tz(datetime.tzinfo): default timezone
     @return (int): timestamp
     """
-    value = unicode(value).strip()
+    value = str(value).strip()
     dayfirst = False if YEAR_FIRST_RE.match(value) else True
 
     dt = default_tzinfo(
@@ -71,23 +71,23 @@
     if m is None:
         return date_parse(value, default_tz=default_tz)
 
-    if m.group(u"direction") and m.group(u"ago"):
+    if m.group("direction") and m.group("ago"):
         raise ValueError(
-            _(u"You can't use a direction (+ or -) and \"ago\" at the same time"))
+            _("You can't use a direction (+ or -) and \"ago\" at the same time"))
 
-    if m.group(u"direction") == u'-' or m.group(u"ago"):
+    if m.group("direction") == '-' or m.group("ago"):
         direction = -1
     else:
         direction = 1
 
-    date = m.group(u"date").strip().lower()
-    if not date or date == u"now":
+    date = m.group("date").strip().lower()
+    if not date or date == "now":
         dt = datetime.datetime.now(tz.tzutc())
     else:
         dt = default_tzinfo(parser.parse(date, dayfirst=True))
 
-    quantity = int(m.group(u"quantity"))
-    key = m.group(u"unit").lower() + u"s"
+    quantity = int(m.group("quantity"))
+    key = m.group("unit").lower() + "s"
     delta_kw = {key: direction * quantity}
     dt = dt + relativedelta(**delta_kw)
     return calendar.timegm(dt.utctimetuple())