LdapServer / datetime helpers : support string for to_timezone parameter
This commit is contained in:
parent
f6d46e3870
commit
46ecdc760a
1 changed files with 10 additions and 2 deletions
|
@ -154,7 +154,7 @@ class LdapServerException(BaseException):
|
||||||
# Helpers
|
# Helpers
|
||||||
#
|
#
|
||||||
def parse_datetime(value, to_timezone=None, default_timezone=None):
|
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(to_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)
|
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)
|
date = dateutil.parser.parse(value, dayfirst=False)
|
||||||
if not date.tzinfo:
|
if not date.tzinfo:
|
||||||
|
@ -169,6 +169,8 @@ def parse_datetime(value, to_timezone=None, default_timezone=None):
|
||||||
else:
|
else:
|
||||||
raise Exception("It's not supposed to happen!")
|
raise Exception("It's not supposed to happen!")
|
||||||
if to_timezone:
|
if to_timezone:
|
||||||
|
if isinstance(to_timezone, str):
|
||||||
|
to_timezone = pytz.timezone(to_timezone)
|
||||||
return date.astimezone(to_timezone)
|
return date.astimezone(to_timezone)
|
||||||
return date
|
return date
|
||||||
|
|
||||||
|
@ -178,7 +180,7 @@ def parse_date(value, to_timezone=None, default_timezone=None):
|
||||||
def format_datetime(value, from_timezone=None, to_timezone=None):
|
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 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 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(to_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)
|
||||||
if not value.tzinfo:
|
if not value.tzinfo:
|
||||||
if not from_timezone:
|
if not from_timezone:
|
||||||
from_timezone = dateutil.tz.tzlocal()
|
from_timezone = dateutil.tz.tzlocal()
|
||||||
|
@ -194,6 +196,8 @@ def format_datetime(value, from_timezone=None, to_timezone=None):
|
||||||
from_value = copy.deepcopy(value)
|
from_value = copy.deepcopy(value)
|
||||||
if not to_timezone:
|
if not to_timezone:
|
||||||
to_timezone = pytz.utc
|
to_timezone = pytz.utc
|
||||||
|
elif isinstance(to_timezone, str):
|
||||||
|
to_timezone = pytz.timezone(to_timezone)
|
||||||
to_value = from_value.astimezone(to_timezone)
|
to_value = from_value.astimezone(to_timezone)
|
||||||
datestring = to_value.strftime('%Y%m%d%H%M%S%z')
|
datestring = to_value.strftime('%Y%m%d%H%M%S%z')
|
||||||
if datestring.endswith('+0000'):
|
if datestring.endswith('+0000'):
|
||||||
|
@ -218,6 +222,7 @@ if __name__ == '__main__':
|
||||||
print "format_datetime (from_timezone=Paris) : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='Europe/Paris')
|
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=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_datetime (to_timezone=local) : %s" % format_datetime(now, to_timezone=dateutil.tz.tzlocal())
|
||||||
|
print "format_datetime (to_timezone=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo')
|
||||||
|
|
||||||
print "format_date : %s" % format_date(now)
|
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=utc) : %s" % format_date(now.replace(tzinfo=None), from_timezone=pytz.utc)
|
||||||
|
@ -225,6 +230,7 @@ if __name__ == '__main__':
|
||||||
print "format_date (from_timezone=Paris) : %s" % format_date(now.replace(tzinfo=None), from_timezone='Europe/Paris')
|
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=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 "format_date (to_timezone=local) : %s" % format_date(now, to_timezone=dateutil.tz.tzlocal())
|
||||||
|
print "format_date (to_timezone=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo')
|
||||||
|
|
||||||
|
|
||||||
print "parse_datetime : %s" % parse_datetime(datestring_now)
|
print "parse_datetime : %s" % parse_datetime(datestring_now)
|
||||||
|
@ -233,6 +239,7 @@ if __name__ == '__main__':
|
||||||
print "parse_datetime (default_timezone=Paris) : %s" % parse_datetime(datestring_now[0:-1], default_timezone='Europe/Paris')
|
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=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_datetime (to_timezone=local) : %s" % parse_datetime(datestring_now, to_timezone=dateutil.tz.tzlocal())
|
||||||
|
print "parse_datetime (to_timezone=Tokyo) : %s" % parse_datetime(datestring_now, to_timezone='Asia/Tokyo')
|
||||||
|
|
||||||
print "parse_date : %s" % parse_date(datestring_now)
|
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=utc) : %s" % parse_date(datestring_now[0:-1], default_timezone=pytz.utc)
|
||||||
|
@ -240,3 +247,4 @@ if __name__ == '__main__':
|
||||||
print "parse_date (default_timezone=Paris) : %s" % parse_date(datestring_now[0:-1], default_timezone='Europe/Paris')
|
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=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())
|
print "parse_date (to_timezone=local) : %s" % parse_date(datestring_now, to_timezone=dateutil.tz.tzlocal())
|
||||||
|
print "parse_date (to_timezone=Tokyo) : %s" % parse_date(datestring_now, to_timezone='Asia/Tokyo')
|
||||||
|
|
Loading…
Reference in a new issue