def select_istg(self, samdb, mydsa, ro):
        """Determine if my DC should be an intersite topology
        generator.  If my DC is the istg and is both a writeable
        DC and the database is opened in write mode then we perform
        an originating update to set the interSiteTopologyGenerator
        attribute in the NTDS Site Settings object.  An RODC always
        acts as an ISTG for itself.
        """
        # The KCC on an RODC always acts as an ISTG for itself
        if mydsa.dsa_is_ro:
            mydsa.dsa_is_istg = True
            return True

        # Find configuration NC replica for my DSA
        for c_rep in mydsa.current_rep_table.values():
            if c_rep.is_config():
                break

        if c_rep is None:
            raise Exception("Unable to find config NC replica for (%s)" %
                            mydsa.dsa_dnstr)

        # Load repsFrom if not already loaded so we can get the current
        # state of the config replica and whether we are getting updates
        # from the istg
        c_rep.load_repsFrom(samdb)

        # From MS-Tech ISTG selection:
        #     First, the KCC on a writable DC determines whether it acts
        #     as an ISTG for its site
        #
        #     Let s be the object such that s!lDAPDisplayName = nTDSDSA
        #     and classSchema in s!objectClass.
        #
        #     Let D be the sequence of objects o in the site of the local
        #     DC such that o!objectCategory = s. D is sorted in ascending
        #     order by objectGUID.
        #
        # Which is a fancy way of saying "sort all the nTDSDSA objects
        # in the site by guid in ascending order".   Place sorted list
        # in D_sort[]
        D_sort = []
        d_dsa = None

        unixnow = int(time.time())     # seconds since 1970
        ntnow = unix2nttime(unixnow) # double word number of 100 nanosecond
                                       # intervals since 1600s

        for dsa in self.dsa_table.values():
            D_sort.append(dsa)

        D_sort.sort(sort_dsa_by_guid)

        # Let f be the duration o!interSiteTopologyFailover seconds, or 2 hours
        # if o!interSiteTopologyFailover is 0 or has no value.
        #
        # Note: lastSuccess and ntnow are in 100 nanosecond intervals
        #       so it appears we have to turn f into the same interval
        #
        #       interSiteTopologyFailover (if set) appears to be in minutes
        #       so we'll need to convert to senconds and then 100 nanosecond
        #       intervals
        #
        #       10,000,000 is number of 100 nanosecond intervals in a second
        if self.site_topo_failover == 0:
            f = 2 * 60 * 60 * 10000000
        else:
            f = self.site_topo_failover * 60 * 10000000

        # From MS-Tech ISTG selection:
        #     If o != NULL and o!interSiteTopologyGenerator is not the
        #     nTDSDSA object for the local DC and
        #     o!interSiteTopologyGenerator is an element dj of sequence D:
        #
        if self.site_topo_generator is not None and \
           self.site_topo_generator in self.dsa_table.keys():
            d_dsa = self.dsa_table[self.site_topo_generator]
            j_idx = D_sort.index(d_dsa)

        if d_dsa is not None and d_dsa is not mydsa:
           # From MS-Tech ISTG selection:
           #     Let c be the cursor in the replUpToDateVector variable
           #     associated with the NC replica of the config NC such
           #     that c.uuidDsa = dj!invocationId. If no such c exists
           #     (No evidence of replication from current ITSG):
           #         Let i = j.
           #         Let t = 0.
           #
           #     Else if the current time < c.timeLastSyncSuccess - f
           #     (Evidence of time sync problem on current ISTG):
           #         Let i = 0.
           #         Let t = 0.
           #
           #     Else (Evidence of replication from current ITSG):
           #         Let i = j.
           #         Let t = c.timeLastSyncSuccess.
           #
           # last_success appears to be a double word containing
           #     number of 100 nanosecond intervals since the 1600s
           if d_dsa.dsa_ivid != c_rep.source_dsa_invocation_id:
               i_idx = j_idx
               t_time = 0

           elif ntnow < (f - c_rep.last_success):
               i_idx = 0
               t_time = 0
           else:
               i_idx = j_idx
               t_time = c_rep.last_success

        # Otherwise (Nominate local DC as ISTG):
        #     Let i be the integer such that di is the nTDSDSA
        #         object for the local DC.
        #     Let t = the current time.
        else:
            i_idx = D_sort.index(mydsa)
            t_time = ntnow

        # Compute a function that maintains the current ISTG if
        # it is alive, cycles through other candidates if not.
        #
        # Let k be the integer (i + ((current time - t) /
        #     o!interSiteTopologyFailover)) MOD |D|.
        #
        # Note: We don't want to divide by zero here so they must
        #       have meant "f" instead of "o!interSiteTopologyFailover"
        k_idx = (i_idx + ((ntnow - t_time) / f)) % len(D_sort)

        # The local writable DC acts as an ISTG for its site if and
        # only if dk is the nTDSDSA object for the local DC. If the
        # local DC does not act as an ISTG, the KCC skips the
        # remainder of this task.
        d_dsa = D_sort[k_idx]
        d_dsa.dsa_is_istg = True

        # Update if we are the ISTG, otherwise return
        if d_dsa is not mydsa:
            return False

        # Nothing to do
        if self.site_topo_generator == mydsa.dsa_dnstr:
            return True

        self.site_topo_generator = mydsa.dsa_dnstr

        # If readonly database then do not perform a
        # persistent update
        if ro:
            return True

        # Perform update to the samdb
        ssdn = "CN=NTDS Site Settings,%s" % self.site_dnstr

        m = ldb.Message()
        m.dn = ldb.Dn(samdb, ssdn)

        m["interSiteTopologyGenerator"] = \
            ldb.MessageElement(mydsa.dsa_dnstr, ldb.FLAG_MOD_REPLACE,
                               "interSiteTopologyGenerator")
        try:
            samdb.modify(m)

        except ldb.LdbError, estr:
            raise Exception(
                "Could not set interSiteTopologyGenerator for (%s) - (%s)" %
                (ssdn, estr))
        return True
dataset/ETHPy150Open byt3bl33d3r/pth-toolkit/lib/python2.7/site-packages/samba/kcc_utils.py/Site.select_istg/swapped operands of `-`