You must either be a bona-fide resident for an uninterrupted period covering the whole tax year[0] or have been out of the US for at least 330 full days in the year[1] although in the latter case your presence in a foreign country must also be legal according to US law.