Next / Previous / Contents / TCC Help System / NM Tech homepage

9.9. parseZone(): Process a time zone suffix

For the various forms allowed as time zone suffixes, refer to the specification.

sidereal.py
# - - -   p a r s e Z o n e

def parseZone ( s ):
    """Validate and convert a time zone suffix.

      [ s is a string ->
          if s is a valid time zone suffix ->
            return that zone's information as an instance of
            a class that inherits from datetime.tzinfo
          else -> raise SyntaxError ]
    """

The only supported zone suffixes with variable content are the “+hhmm” and “-hhmm” forms. The rest can be handled with a dictionary whose keys are the various time zone codes (uppercased, so we can be case-insensitive), and each corresponding value is the appropriate tzinfo instance. For this dictionary, see Section 9.13, “zoneCodeMap: Dictionary of time zones”.

sidereal.py
    #-- 1 --
    # [ if s starts with "+" or "-" and is a valid fixed-offset
    #   time zone suffix ->
    #     return that zone's information as a datetime.tzinfo instance
    #   else if is starts with "+" or "-" but is not a valid
    #   fixed-offset time zone suffix ->
    #     raise SyntaxError
    #   else -> I ]
    if  s.startswith("+") or s.startswith("-"):
        return  parseFixedZone ( s )

    #-- 2 --
    # [ if s.upper() is a key in zoneCodeMap ->
    #     return the corresponding value
    #   else -> raise SyntaxError ]
    try:
        tz  =  zoneCodeMap[s.upper()]
        return tz
    except KeyError:
        raise SyntaxError, ( "Unknown time zone code: '%s'" % s )