'Convert Python datetime to Excel and set formatting'

def _cast_datetime(self, value):

    if isinstance(value, datetime.datetime):
        value = to_excel(value, self.base_date)
        self.number_format = numbers.FORMAT_DATE_DATETIME
    elif isinstance(value, datetime.date):
        value = to_excel(value, self.base_date)
        self.number_format = numbers.FORMAT_DATE_YYYYMMDD2
    elif isinstance(value, datetime.time):
        value = time_to_days(value)
        self.number_format = numbers.FORMAT_DATE_TIME6
    elif isinstance(value, datetime.timedelta):
        value = timedelta_to_days(value)
        self.number_format = numbers.FORMAT_DATE_TIMEDELTA
    return value
