diff --git a/LdapServer.py b/LdapServer.py index 3b898a9..4ff5396 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -2,7 +2,8 @@ import copy import datetime -import dateutil +import dateutil.parser +import dateutil.tz import ldap import ldap.modlist as modlist import logging @@ -163,16 +164,16 @@ def parse_datetime(value, to_timezone=None, default_timezone=None): default_timezone = pytz.timezone(default_timezone) if isinstance(default_timezone, pytz.tzinfo.DstTzInfo): date = default_timezone.localize(date) - elif isinstance(from_timezone, datetime.tzinfo): + elif isinstance(default_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.astimezone(to_timezone) return date -def parse_date(value, to_timezone=None): - return parse_datetime(value, to_timezone).date() +def parse_date(value, to_timezone=None, default_timezone=None): + return parse_datetime(value, to_timezone, default_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) @@ -194,7 +195,7 @@ def format_datetime(value, from_timezone=None, to_timezone=None): 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') + datestring = to_value.strftime('%Y%m%d%H%M%S%z') if datestring.endswith('+0000'): datestring = datestring.replace('+0000', 'Z') return datestring @@ -202,3 +203,40 @@ def format_datetime(value, from_timezone=None, to_timezone=None): 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) + +# +# Tests +# +if __name__ == '__main__': + now = datetime.datetime.now() + print "Now = %s" % now + + datestring_now = format_datetime(now) + print "format_datetime : %s" % datestring_now + print "format_datetime (from_timezone=utc) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=pytz.utc) + print "format_datetime (from_timezone=local) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()) + print "format_datetime (from_timezone=Paris) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='Europe/Paris') + print "format_datetime (to_timezone=utc) : %s" % format_datetime(now, to_timezone=pytz.utc) + print "format_datetime (to_timezone=local) : %s" % format_datetime(now, to_timezone=dateutil.tz.tzlocal()) + + print "format_date : %s" % format_date(now) + print "format_date (from_timezone=utc) : %s" % format_date(now.replace(tzinfo=None), from_timezone=pytz.utc) + print "format_date (from_timezone=local) : %s" % format_date(now.replace(tzinfo=None), from_timezone=dateutil.tz.tzlocal()) + print "format_date (from_timezone=Paris) : %s" % format_date(now.replace(tzinfo=None), from_timezone='Europe/Paris') + print "format_date (to_timezone=utc) : %s" % format_date(now, to_timezone=pytz.utc) + print "format_date (to_timezone=local) : %s" % format_date(now, to_timezone=dateutil.tz.tzlocal()) + + + print "parse_datetime : %s" % parse_datetime(datestring_now) + print "parse_datetime (default_timezone=utc) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=pytz.utc) + print "parse_datetime (default_timezone=local) : %s" % parse_datetime(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()) + print "parse_datetime (default_timezone=Paris) : %s" % parse_datetime(datestring_now[0:-1], default_timezone='Europe/Paris') + print "parse_datetime (to_timezone=utc) : %s" % parse_datetime(datestring_now, to_timezone=pytz.utc) + print "parse_datetime (to_timezone=local) : %s" % parse_datetime(datestring_now, to_timezone=dateutil.tz.tzlocal()) + + print "parse_date : %s" % parse_date(datestring_now) + print "parse_date (default_timezone=utc) : %s" % parse_date(datestring_now[0:-1], default_timezone=pytz.utc) + print "parse_date (default_timezone=local) : %s" % parse_date(datestring_now[0:-1], default_timezone=dateutil.tz.tzlocal()) + print "parse_date (default_timezone=Paris) : %s" % parse_date(datestring_now[0:-1], default_timezone='Europe/Paris') + print "parse_date (to_timezone=utc) : %s" % parse_date(datestring_now, to_timezone=pytz.utc) + print "parse_date (to_timezone=local) : %s" % parse_date(datestring_now, to_timezone=dateutil.tz.tzlocal())