diff options
author | [ Kyle ] [ Hausmann ] <khausmann@squarespace.com> | 2019-01-07 09:33:05 -0500 |
---|---|---|
committer | [ Kyle ] [ Hausmann ] <kyle.hausmann@gmail.com> | 2019-01-07 11:44:17 -0500 |
commit | 99a887cc2474b7e75c154c306cfbb23b0afea447 (patch) | |
tree | fe01bb5fafb668ee7d0b73732fdace89a66c42b0 | |
parent | b980edb6c3a79a37f311bc6199d7b183bc6eef99 (diff) | |
download | pytz-git-99a887cc2474b7e75c154c306cfbb23b0afea447.tar.gz |
timezone constructor arg is case-insensitive
Use the case-insensitive match, by using the
_all_timezones_lower_to_standard map, if possible.
Signed-off-by: [ Kyle ] [ Hausmann ] <khausmann@squarespace.com>
-rw-r--r-- | src/pytz/__init__.py | 7 | ||||
-rw-r--r-- | src/pytz/tests/test_tzinfo.py | 10 |
2 files changed, 16 insertions, 1 deletions
diff --git a/src/pytz/__init__.py b/src/pytz/__init__.py index 6208a1e..73cb991 100644 --- a/src/pytz/__init__.py +++ b/src/pytz/__init__.py @@ -166,7 +166,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) @@ -185,6 +185,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..643f08a 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 '{}' and arg '{}' produce different timezone objects".format(from_lower, from_passed)) + + class BaseTzInfoTestCase: '''Ensure UTC, StaticTzInfo and DstTzInfo work consistently. |