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

9.10. parseFixedZone(): Parse fixed zone suffix

If the argument s is a fixed time zone offset of the form “±hhmm”, this function returns an instance of class FixedZone representing that offset.

sidereal.py
# - - -   p a r s e F i x e d Z o n e

HHMM_PAT  =  re.compile (
    r'\d{4}'    # Matches exactly four digits
    r'$' )        # Be sure everything is matched

def parseFixedZone ( s ):
    """Convert a +hhmm or -hhmm zone suffix.

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

The first character must be “+” or “-”; we set sign to +1 or -1, respectively.

sidereal.py
    #-- 1 --
    if  s.startswith('+'):    sign  =  1
    elif  s.startswith('-'):  sign  =  -1
    else:
        raise SyntaxError, ( "Expecting zone modifier as %shhmm: "
                             "'%s'" % (s[0], s) )

Use a regular expression to check the length and content of the hours and minutes. Then return an instance of the FixedZone class representing that number of hours and minutes.

sidereal.py
    #-- 2 --
    # [ if s[1:] matches HHMM_PAT ->
    #     hours  :=  the HH part as an int
    #     minutes  :=  the MM part as an int
    #   else -> raise SyntaxError ]
    rawHHMM  =  s[1:]
    m  =  HHMM_PAT.match ( rawHHMM )
    if  m is None:
        raise SyntaxError, ( "Expecting zone modifier as %sHHMM: "
                             "'%s'" % (s[0], s) )
    else:
        hours  =  int ( rawHHMM[:2] )
        minutes  =  int ( rawHHMM[2:] )

    #-- 3 --
    return  FixedZone ( sign*hours, sign*minutes, s )