diff options
author | stub42 <stuart@stuartbishop.net> | 2019-01-21 13:29:40 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-01-21 13:29:40 +0700 |
commit | 78d76e59e52761b30cdf413be23d7d5e57ed416c (patch) | |
tree | 8440eea573e3393520013109bbe0ca2895573e1f | |
parent | fd9b123d7493e93a040a241c6f249144eb463529 (diff) | |
parent | bec0a03d2322fc897676986ac21b75ba6e96d69c (diff) | |
download | pytz-git-78d76e59e52761b30cdf413be23d7d5e57ed416c.tar.gz |
Make timezone lookup case insensitive
-rw-r--r-- | gen_tzinfo.py | 1 | ||||
-rw-r--r-- | src/pytz/__init__.py | 7 | ||||
-rw-r--r-- | src/pytz/tests/test_tzinfo.py | 10 |
3 files changed, 17 insertions, 1 deletions
diff --git a/gen_tzinfo.py b/gen_tzinfo.py index 201a643..d06fa0e 100644 --- a/gen_tzinfo.py +++ b/gen_tzinfo.py @@ -129,6 +129,7 @@ def add_allzones(filename): tz for tz in all_timezones if resource_exists(tz)) ''' print >> outf, 'all_timezones_set = LazySet(all_timezones)' + print >> outf, '_all_timezones_lower_to_standard = dict((tz.lower(), tz) for tz in all_timezones)' print >> outf, 'common_timezones = \\' pprint(cz, outf) diff --git a/src/pytz/__init__.py b/src/pytz/__init__.py index ac6b455..36d38c3 100644 --- a/src/pytz/__init__.py +++ b/src/pytz/__init__.py @@ -169,7 +169,7 @@ def timezone(zone): # All valid timezones are ASCII raise UnknownTimeZoneError(zone) - zone = _unmunge_zone(zone) + zone = _case_insensitive_zone_lookup(_unmunge_zone(zone)) if zone not in _tzinfo_cache: if zone in all_timezones_set: fp = open_resource(zone) @@ -188,6 +188,11 @@ def _unmunge_zone(zone): return zone.replace('_plus_', '+').replace('_minus_', '-') +def _case_insensitive_zone_lookup(zone): + """Get case-insensitively matching timezone, if found, else return zone unchanged""" + return _all_timezones_lower_to_standard.get(zone.lower()) or zone + + ZERO = datetime.timedelta(0) HOUR = datetime.timedelta(hours=1) diff --git a/src/pytz/tests/test_tzinfo.py b/src/pytz/tests/test_tzinfo.py index 91104cd..6a852cc 100644 --- a/src/pytz/tests/test_tzinfo.py +++ b/src/pytz/tests/test_tzinfo.py @@ -758,6 +758,16 @@ class CommonTimezonesTestCase(unittest.TestCase): self.assertFalse('Europe/Belfast' in pytz.common_timezones_set) +class ZoneCaseInsensitivityTestCase(unittest.TestCase): + def test_lower_case_timezone_constructor_arg(self): + for tz in pytz.all_timezones_set: + from_lower = pytz.timezone(tz.lower()) + from_passed = pytz.timezone(tz) + self.assertEqual(from_lower, + from_passed, + "arg '%s' and arg '%s' produce different timezone objects" % (from_lower, from_passed)) + + class BaseTzInfoTestCase: '''Ensure UTC, StaticTzInfo and DstTzInfo work consistently. |