Add datetime helpers

This commit is contained in:
Benjamin Renard 2019-06-13 19:15:13 +02:00 committed by root
parent 0a127f0f8b
commit 74d8b4e913

View file

@ -1,8 +1,12 @@
#!/usr/bin/python #!/usr/bin/python
import copy
import datetime
import dateutil
import ldap import ldap
import ldap.modlist as modlist import ldap.modlist as modlist
import logging import logging
import pytz
class LdapServer(object): class LdapServer(object):
@ -144,3 +148,57 @@ class LdapServer(object):
class LdapServerException(BaseException): class LdapServerException(BaseException):
def __init__(self,msg): def __init__(self,msg):
BaseException.__init__(self, msg) BaseException.__init__(self, msg)
#
# Helpers
#
def parse_datetime(value, to_timezone=None, default_timezone=None):
assert to_timezone is None or isinstance(to_timezone, datetime.tzinfo), 'to_timezone must be None or datetime.tzinfo object (not %s)' % type(value)
assert default_timezone is None or isinstance(default_timezone, datetime.tzinfo) or isinstance(default_timezone, pytz.tzinfo.DstTzInfo) or isinstance(default_timezone, str), 'default_timezone parameter must be None, a string, a pytz.tzinfo.DstTzInfo or a datetime.tzinfo object (not %s)' % type(from_timezone)
date = dateutil.parser.parse(value, dayfirst=False)
if not date.tzinfo:
if not default_timezone:
default_timezone = pytz.utc
elif isinstance(default_timezone, str):
default_timezone = pytz.timezone(default_timezone)
if isinstance(default_timezone, pytz.tzinfo.DstTzInfo):
date = default_timezone.localize(date)
elif isinstance(from_timezone, datetime.tzinfo):
date = date.replace(tzinfo=default_timezone)
else:
raise Exception("It's not supposed to happen!")
if to_timezone:
return date.astimezone(totimezone)
return date
def parse_date(value, to_timezone=None):
return parse_datetime(value, to_timezone).date()
def format_datetime(value, from_timezone=None, to_timezone=None):
assert isinstance(value, datetime.datetime), 'First parameter must be an datetime.datetime object (not %s)' % type(value)
assert from_timezone is None or isinstance(from_timezone, datetime.tzinfo) or isinstance(from_timezone, pytz.tzinfo.DstTzInfo) or isinstance(from_timezone, str), 'from_timezone parameter must be None, a string, a pytz.tzinfo.DstTzInfo or a datetime.tzinfo object (not %s)' % type(from_timezone)
assert to_timezone is None or isinstance(to_timezone, datetime.tzinfo), 'to_timezone must be None or datetime.tzinfo object (not %s)' % type(value)
if not value.tzinfo:
if not from_timezone:
from_timezone = dateutil.tz.tzlocal()
elif isinstance(from_timezone, str):
from_timezone = pytz.timezone(from_timezone)
if isinstance(from_timezone, pytz.tzinfo.DstTzInfo):
from_value = from_timezone.localize(value)
elif isinstance(from_timezone, datetime.tzinfo):
from_value = value.replace(tzinfo=from_timezone)
else:
raise Exception("It's not supposed to happen!")
else:
from_value = copy.deepcopy(value)
if not to_timezone:
to_timezone = pytz.utc
to_value = from_value.astimezone(to_timezone)
datestring = to_value.strftime(value, '%Y%m%d%H%M%S%z')
if datestring.endswith('+0000'):
datestring = datestring.replace('+0000', 'Z')
return datestring
def format_date(value, from_timezone=None, to_timezone=None):
assert isinstance(value, datetime.date), 'First parameter must be an datetime.date object (not %s)' % type(value)
return format_datetime(datetime.datetime.combine(value, datetime.datetime.min.time()), from_timezone, to_timezone)