From 74d8b4e91393edd412b22d66b1f4da3dd96efa04 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Thu, 13 Jun 2019 19:15:13 +0200 Subject: [PATCH] Add datetime helpers --- LdapServer.py | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/LdapServer.py b/LdapServer.py index fcbce87..3b898a9 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -1,8 +1,12 @@ #!/usr/bin/python +import copy +import datetime +import dateutil import ldap import ldap.modlist as modlist import logging +import pytz class LdapServer(object): @@ -144,3 +148,57 @@ class LdapServer(object): class LdapServerException(BaseException): def __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)