diff --git a/LdapServer.py b/LdapServer.py index eb99b03..2e66f58 100644 --- a/LdapServer.py +++ b/LdapServer.py @@ -172,6 +172,8 @@ def parse_datetime(value, to_timezone=None, default_timezone=None, naive=None): return date if not default_timezone: default_timezone = pytz.utc + elif default_timezone == 'local': + default_timezone = dateutil.tz.tzlocal() elif isinstance(default_timezone, str): default_timezone = pytz.timezone(default_timezone) if isinstance(default_timezone, pytz.tzinfo.DstTzInfo): @@ -183,7 +185,9 @@ def parse_datetime(value, to_timezone=None, default_timezone=None, naive=None): elif naive: return date.replace(tzinfo=None) if to_timezone: - if isinstance(to_timezone, str): + if to_timezone == 'local': + to_timezone = dateutil.tz.tzlocal() + elif isinstance(to_timezone, str): to_timezone = pytz.timezone(to_timezone) return date.astimezone(to_timezone) return date @@ -215,7 +219,7 @@ def format_datetime(value, from_timezone=None, to_timezone=None, naive=None): 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) if not value.tzinfo and not naive: - if not from_timezone: + if not from_timezone or from_timezone == 'local': from_timezone = dateutil.tz.tzlocal() elif isinstance(from_timezone, str): from_timezone = pytz.timezone(from_timezone) @@ -231,6 +235,8 @@ def format_datetime(value, from_timezone=None, to_timezone=None, naive=None): from_value = copy.deepcopy(value) if not to_timezone: to_timezone = pytz.utc + elif to_timezone == 'local': + to_timezone = dateutil.tz.tzlocal() elif isinstance(to_timezone, str): to_timezone = pytz.timezone(to_timezone) to_value = from_value.astimezone(to_timezone) @@ -264,18 +270,22 @@ if __name__ == '__main__': 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='local') : %s" % format_datetime(now.replace(tzinfo=None), from_timezone='local') 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_datetime (to_timezone='local') : %s" % format_datetime(now, to_timezone='local') print "format_datetime (to_timezone=Tokyo) : %s" % format_datetime(now, to_timezone='Asia/Tokyo') print "format_datetime (naive=True) : %s" % format_datetime(now, naive=True) 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='local') : %s" % format_date(now.replace(tzinfo=None), from_timezone='local') 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 "format_date (to_timezone='local') : %s" % format_date(now, to_timezone='local') print "format_date (to_timezone=Tokyo) : %s" % format_date(now, to_timezone='Asia/Tokyo') print "format_date (naive=True) : %s" % format_date(now, naive=True) @@ -283,17 +293,21 @@ if __name__ == '__main__': 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='local') : %s" % parse_datetime(datestring_now[0:-1], default_timezone='local') 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_datetime (to_timezone='local') : %s" % parse_datetime(datestring_now, to_timezone='local') print "parse_datetime (to_timezone=Tokyo) : %s" % parse_datetime(datestring_now, to_timezone='Asia/Tokyo') print "parse_datetime (naive=True) : %s" % parse_datetime(datestring_now, naive=True) 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='local') : %s" % parse_date(datestring_now[0:-1], default_timezone='local') 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()) + print "parse_date (to_timezone='local') : %s" % parse_date(datestring_now, to_timezone='local') print "parse_date (to_timezone=Tokyo) : %s" % parse_date(datestring_now, to_timezone='Asia/Tokyo') print "parse_date (naive=True) : %s" % parse_date(datestring_now, naive=True)