Instanceof for Apex Date and DateTime

Just spent some time chasing a unit test error where the date returned in some code was a day out. The code being tested was:

SObject sob = ...
Object o = sob.get(f);
Date d;
if (o instanceof DateTime) {
    d = ((DateTime) o).date();
} else if (o instanceof Date) {
    d = (Date) o;
}

In the end this unit test demonstrated the cause of the problem:

@IsTest
private class InstanceofTest {
    @IsTest
    static void date() {
        Object o = Date.today();
        System.assertEquals(true, o instanceof Date);
        System.assertEquals(true, o instanceof DateTime);
    }
    @IsTest
    static void dateTime() {
        Object o = DateTime.now();
        System.assertEquals(false, o instanceof Date);
        System.assertEquals(true, o instanceof DateTime);
    }
}

So a Date is a DateTime (as well as a Date) and that was pushing the code through some unwanted timezone offsetting.

A fix is:

SObject sob = ...
Object o = sob.get(f);
Date d;
if (o instanceof Date) {
    d = (Date) o;
} else if (o instanceof DateTime) {
    d = ((DateTime) o).date();
}

and a lesson learned is to be paranoid about the type system in Apex.