import datetime


def datetime_as_iso(value):
    """Helper function to format datetime object to ISO8601 string."""
    if not isinstance(value, datetime.datetime):
        return ""
    retval = value.strftime("%Y-%m-%dT%H:%M:%S%z")
    if not value.utcoffset():
        return "".join([retval, "Z"])
    return retval