I need to use the time zone of an exchange to create a tzinfo object so I can convert a time zone naive time into time zone aware. Its use is in a comparison with a supplied datetime object in an expiry time consolidator.

I can get the exchange time zone, but I don't know how to get the GMT offset for use in datetime.timezone(offset).

Any suggestions?

    def Custom(self, dt):
        '''
        Returns a CalendarInfo object for a custom daily consolidator.
        For Forex the end of day is 17:00 NY time.
        CFD end of day varies with CFD/Exchange.
        '''
        period = timedelta(days=1)
        
        if self.security.Type == SecurityType.Forex:
            start_time = dt.replace(hour=17)
        else:
            # Get the next market close time after midday today (time zone naive).
            reference_time = dt.replace(hour=12, minute=0)
            close_time = self.security.Exchange.Hours.GetNextMarketClose(reference_time, False)
            
            # Make the close time, time zone aware, so it can be compared with dt.
            exchange_tz = self.security.Exchange.Hours.TimeZone
            exchange_tx_offset = ???     # *** How to get the offset from exchange_tz?
            exchange_tzinfo = datetime.timezone(exchange_tx_offset)
            start_time = close_time.replace(tzinfo=exchange_tzinfo)
        
            self.Debug("Custom(): close_time = " + str(close_time) + ", start_time = " + str(start_time))
        
        # This comparison needs time zone aware times.
        if start_time > dt:
            start_time = start_time - period
        
        return CalendarInfo(start_time, period)

 

Author