def get_optime(mc):
    """ Get optime of primary in the replica set.

    Changed in version 3.2.
    If using protocolVersion: 1, optime returns a document that contains:
        - ts, the Timestamp of the last operation applied to this member of the replica set from the oplog.
        - t, the term in which the last applied operation was originally generated on the primary.
    If using protocolVersion: 0, optime returns the Timestamp of the last operation applied to this member of the replica set from the oplog.

    Refer to https://docs.mongodb.com/manual/reference/command/replSetGetStatus/
    """
    rs_status = mc['admin'].command({'replSetGetStatus': 1})
    members = rs_status.get('members')
    if not members:
        raise Exception('no member in replica set')
    for member in rs_status['members']:
        role = member.get('stateStr')
        if role == 'PRIMARY':
            optime = member.get('optime')
            if isinstance(optime, dict) and 'ts' in optime:  # for MongoDB v3.2
                return optime['ts']
            else:
                return optime
    raise Exception('no primary in replica set')