How does BIP9 handle expiration dates over retargeting periods? Suppose a BIP9 feature is set to expire on day T. Suppose further that the retargeting period before T actually ends on day T+5, but the retargeting period (2016 blocks) actually signals for the BIP9 feature 95%+. Does the feature get activated or not?

I ask as this is a realistic scenario should BIP148 gain traction.
As per BIP 9, the transition to FAILED takes precendence. Which means that the feature will not be activated via BIP09.
The relevant part of the code.

case STARTED: 
            if (GetMedianTimePast(block.parent) &gt;= timeout) {
                return FAILED;
            }
            int count = 0;
            walk = block;
            for (i = 0; i &lt; 2016; i++) {
...