@contextmanager<EOL>def set_timezone(tz):
import os<EOL>import time<EOL>def setTZ(tz):<EOL><INDENT>if tz is None:<EOL><INDENT>try:<EOL><INDENT>del os.environ['<STR_LIT>']<EOL><DEDENT>except KeyError:<EOL><INDENT>pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>os.environ['<STR_LIT>'] = tz<EOL>time.tzset()<EOL><DEDENT><DEDENT>orig_tz = os.environ.get('<STR_LIT>')<EOL>setTZ(tz)<EOL>try:<EOL><INDENT>yield<EOL><DEDENT>finally:<EOL><INDENT>setTZ(orig_tz)<EOL><DEDENT>
Context manager for temporarily setting a timezone.

    Parameters
    ----------
    tz : str
        A string representing a valid timezone.

    Examples
    --------

    >>> from datetime import datetime
    >>> from dateutil.tz import tzlocal
    >>> tzlocal().tzname(datetime.now())
    'IST'

    >>> with set_timezone('US/Eastern'):
    ...     tzlocal().tzname(datetime.now())
    ...
    'EDT'