Skip to content

Commit

Permalink
Allow passing in strings instead of timezones, for convenience.
Browse files Browse the repository at this point in the history
  • Loading branch information
nvie committed Feb 1, 2012
1 parent 28c171a commit 6ea6b7b
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions times/__init__.py
Expand Up @@ -34,6 +34,8 @@ def to_local(dt, timezone):
"""
if dt.tzinfo is not None:
raise ValueError('First argument to to_local() should be a universal time.')
if isinstance(timezone, basestring):
timezone = pytz.timezone(timezone)
return pytz.utc.localize(dt).astimezone(timezone)

from_universal = to_local
Expand All @@ -46,5 +48,7 @@ def format(dt, timezone):
"""Formats the given universal time for display in the given time zone."""
if timezone is None:
raise ValueError('Please give an explicit timezone.')
if isinstance(timezone, basestring):
timezone = pytz.timezone(timezone)
return to_local(dt, timezone).strftime(fmt)

0 comments on commit 6ea6b7b

Please sign in to comment.