Add doc-string about datetime helpers
This commit is contained in:
parent
46ecdc760a
commit
86b99b69a7
1 changed files with 34 additions and 0 deletions
|
@ -154,6 +154,15 @@ class LdapServerException(BaseException):
|
|||
# Helpers
|
||||
#
|
||||
def parse_datetime(value, to_timezone=None, default_timezone=None):
|
||||
"""
|
||||
Convert LDAP date string to datetime.datetime object
|
||||
|
||||
:param value: The LDAP date string to convert
|
||||
:param to_timezone: If specified, the return datetime will be converted to this
|
||||
specific timezone (optional, default : timezone of the LDAP date string)
|
||||
:param default_timezone: The timezone used if LDAP date string does not specified
|
||||
the timezone (optional, default : server local timezone)
|
||||
"""
|
||||
assert to_timezone is None or isinstance(to_timezone, datetime.tzinfo) or isinstance(to_timezone, str), 'to_timezone must be None, a datetime.tzinfo object or a string (not %s)' % type(to_timezone)
|
||||
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(default_timezone)
|
||||
date = dateutil.parser.parse(value, dayfirst=False)
|
||||
|
@ -175,9 +184,26 @@ def parse_datetime(value, to_timezone=None, default_timezone=None):
|
|||
return date
|
||||
|
||||
def parse_date(value, to_timezone=None, default_timezone=None):
|
||||
"""
|
||||
Convert LDAP date string to datetime.date object
|
||||
|
||||
:param value: The LDAP date string to convert
|
||||
:param to_timezone: If specified, the return datetime will be converted to this
|
||||
specific timezone (optional, default : timezone of the LDAP date string)
|
||||
:param default_timezone: The timezone used if LDAP date string does not specified
|
||||
the timezone (optional, default : server local timezone)
|
||||
"""
|
||||
return parse_datetime(value, to_timezone, default_timezone).date()
|
||||
|
||||
def format_datetime(value, from_timezone=None, to_timezone=None):
|
||||
"""
|
||||
Convert datetime.datetime object to LDAP date string
|
||||
|
||||
:param value: The datetime.datetime object to convert
|
||||
:param from_timezone: The timezone used if datetime.datetime object is naive (no tzinfo)
|
||||
(optional, default : server local timezone)
|
||||
:param to_timezone: The timezone used in LDAP (optional, default : UTC)
|
||||
"""
|
||||
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) or isinstance(to_timezone, str), 'to_timezone must be None, a datetime.tzinfo object or a string (not %s)' % type(to_timezone)
|
||||
|
@ -205,6 +231,14 @@ def format_datetime(value, from_timezone=None, to_timezone=None):
|
|||
return datestring
|
||||
|
||||
def format_date(value, from_timezone=None, to_timezone=None):
|
||||
"""
|
||||
Convert datetime.date object to LDAP date string
|
||||
|
||||
:param value: The datetime.date object to convert
|
||||
:param from_timezone: The timezone used if datetime.datetime object is naive (no tzinfo)
|
||||
(optional, default : server local timezone)
|
||||
:param to_timezone: The timezone used in LDAP (optional, default : UTC)
|
||||
"""
|
||||
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)
|
||||
|
||||
|
|
Loading…
Reference in a new issue