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

51.2. class FixedTimeZone

In order to represent aware datetime objects (see the datetime documentation for a discussion of aware vs. naive times), we'll need a class derived from datetime.tzinfo to represent the timestamps from the log files.

We don't have to worry about Daylight Saving Time, because the time zone in the log record is encoded as “-0700” or “-0600” depending on the time of year. So for our uses, a time zone object with a fixed offset will be fine.

The code below was cribbed pretty much directly from the example class FixedOffset in the datetime documentation, so we'll present it here with minimal comments.

pageget.py
# - - - - -   c l a s s   F i x e d T i m e Z o n e

class FixedTimeZone(datetime.tzinfo):
    '''Represents a time zone that's always the same offset from UTC.

      Exports:
        FixedTimeZone ( mmEast, name ):
          [ (mmEast is a zone offset represented by the number of
            minutes east of UTC, or negative for west) and
            (name is a time zone name as a string) ->
              return a new FixedTimeZone instance with those
              values ]
        .utcoffset(dt):
          [ dt is a datetime.datetime instance ->
              return self's offset as a datetime.timedelta
              instance ]
        .tzname(dt):
          [ return self's name ]
        .dst(dt):
          [ return a datetime.timedelta of zero ]

      State/Invariants:
        .__offset:  [ self's offset as a datetime.timedelta ]
        .__name:    [ self's name, as passed to constructor ]
        .ZERO:      [ a zero datetime.timedelta ]
    '''
    ZERO  =  datetime.timedelta ( 0 )

    def __init__ ( self, mmEast, name ):
        '''Constructor
        '''
        self.__offset  =  datetime.timedelta ( minutes=mmEast )
        self.__name  =  name

    def utcoffset ( self, dt ):
        return self.__offset

    def tzname ( self, dt ):
        return self.__name

    def dst ( self, dt ):
        return self.ZERO