Prépare l'utilisation de generalized time format pour recuPaiement
* Et de datetimes timezone aware pour gérer le bousin
This commit is contained in:
parent
87382fbf6f
commit
ca77b3defa
2 changed files with 65 additions and 1 deletions
|
@ -41,7 +41,7 @@ from lock import make_lock, remove_lock
|
||||||
from ldap_crans import crans_ldap, blacklist_items, droits_possibles, droits_critiques, smtpserv, script_utilisateur
|
from ldap_crans import crans_ldap, blacklist_items, droits_possibles, droits_critiques, smtpserv, script_utilisateur
|
||||||
from ldap_crans import Adherent, AssociationCrans, Club, Facture
|
from ldap_crans import Adherent, AssociationCrans, Club, Facture
|
||||||
from ldap_crans import Machine, MachineFixe, MachineWifi, MachineCrans, BorneWifi
|
from ldap_crans import Machine, MachineFixe, MachineWifi, MachineCrans, BorneWifi
|
||||||
from ldap_crans import tz, generalizedTimeFormat, fromGeneralizedTimeFormat
|
from ldap_crans import tz, generalizedTimeFormat, fromGeneralizedTimeFormat, datetimeFromGTF, datetimeToGTF, localizedDatetime
|
||||||
import user_tests
|
import user_tests
|
||||||
|
|
||||||
isadm = user_tests.isadm()
|
isadm = user_tests.isadm()
|
||||||
|
|
|
@ -47,6 +47,16 @@ import ridtools
|
||||||
from user_tests import isadm
|
from user_tests import isadm
|
||||||
import getpass
|
import getpass
|
||||||
|
|
||||||
|
try:
|
||||||
|
import pytz
|
||||||
|
except:
|
||||||
|
pytz = None
|
||||||
|
|
||||||
|
try:
|
||||||
|
import dateutil.tz
|
||||||
|
except:
|
||||||
|
dateutil = None
|
||||||
|
|
||||||
cur_user = os.getenv("SUDO_USER") or os.getenv("USER") or os.getenv("LOGNAME") or getpass.getuser()
|
cur_user = os.getenv("SUDO_USER") or os.getenv("USER") or os.getenv("LOGNAME") or getpass.getuser()
|
||||||
|
|
||||||
date_format = '%d/%m/%Y %H:%M'
|
date_format = '%d/%m/%Y %H:%M'
|
||||||
|
@ -163,6 +173,60 @@ def fromGeneralizedTimeFormat(gtf):
|
||||||
"""
|
"""
|
||||||
return time.mktime(time.strptime(gtf.split("-", 1)[0].split("+", 1)[0].split('Z', 1)[0], "%Y%m%d%H%M%S"))
|
return time.mktime(time.strptime(gtf.split("-", 1)[0].split("+", 1)[0].split('Z', 1)[0], "%Y%m%d%H%M%S"))
|
||||||
|
|
||||||
|
def datetimeFromGTF(gtf):
|
||||||
|
"""Returns a datetime from generalized time format
|
||||||
|
|
||||||
|
"""
|
||||||
|
if '-' in gtf or '+' in gtf:
|
||||||
|
date, tz = gtf[0:14], gtf[14:]
|
||||||
|
else:
|
||||||
|
date = gtf.replace("Z", '')
|
||||||
|
tz = '+0000'
|
||||||
|
return localizedDatetime(date, tz)
|
||||||
|
|
||||||
|
def datetimeToGTF(datetime_obj):
|
||||||
|
"""Transforms a datetime to a GTF"""
|
||||||
|
to_append = ""
|
||||||
|
if datetime_obj.utcoffset() is None:
|
||||||
|
if pytz is not None:
|
||||||
|
datetime_obj = pytz.utc.localize(datetime_obj)
|
||||||
|
else:
|
||||||
|
to_append = "Z"
|
||||||
|
mostly_gtf = datetime.datetime.strftime(datetime_obj, "%Y%m%d%H%M%S%z")
|
||||||
|
return mostly_gtf.replace('+0000', "Z") + to_append
|
||||||
|
|
||||||
|
def localizedDatetime(date=None, tz=None):
|
||||||
|
"""Génère un datetime localisé à partir d'une chaîne de la forme
|
||||||
|
%Y%m%d%H%M%S, et d'une chaîne tz de la forme +0200"""
|
||||||
|
|
||||||
|
_notz = (tz is None)
|
||||||
|
|
||||||
|
if date is not None:
|
||||||
|
the_date = datetime.datetime.strptime(date, "%Y%m%d%H%M%S")
|
||||||
|
else:
|
||||||
|
the_date = datetime.datetime.now()
|
||||||
|
|
||||||
|
# No timezone means we try to get from the system
|
||||||
|
# if we have dateutil, else, UTC.
|
||||||
|
if tz is None:
|
||||||
|
if dateutil is not None:
|
||||||
|
tz = datetime.datetime.now(dateutil.tz.tzlocal()).strftime("%z")
|
||||||
|
else:
|
||||||
|
tz = "+0000"
|
||||||
|
|
||||||
|
# No pytz means no timezoned datetime
|
||||||
|
if pytz is not None:
|
||||||
|
the_timezone = pytz.FixedOffset(int(tz[0:-2])*60 + int(tz[-2:]))
|
||||||
|
the_date = the_timezone.localize(the_date)
|
||||||
|
the_date = the_timezone.normalize(the_date)
|
||||||
|
else:
|
||||||
|
# Maybe we can do something
|
||||||
|
if dateutil is not None:
|
||||||
|
if _notz:
|
||||||
|
the_date.replace(tzinfo=dateutil.tz.tzlocal())
|
||||||
|
|
||||||
|
return the_date
|
||||||
|
|
||||||
def strip_accents(a, sois_un_porc_avec_les_espaces = True):
|
def strip_accents(a, sois_un_porc_avec_les_espaces = True):
|
||||||
""" Supression des accents de la chaîne fournie """
|
""" Supression des accents de la chaîne fournie """
|
||||||
res = normalize('NFKD', decode(a)).encode('ASCII', 'ignore')
|
res = normalize('NFKD', decode(a)).encode('ASCII', 'ignore')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue