You have to read not only the 2013 Pub 523, but 523 for the last few years to see the logic.
1. Base of prorate is the whole period you owned the house.
2. For Non-qualified period only count days after Jan 01 2009.
3. If you lived, then rented, then sold, then this rental period is not counted as non-qualified.
4. If you lived, rented, and lived again, then sold, then it is.
5. If you lived, rented for time A, lived, rented for time B, then sold, then A is non-qualified, and B is not...
To make you more confused,
6. For any rental periond after May 06 1997, deduct depreciations and file additional paperworks...