def to_datetime(when, log=logging.getLogger('{}.time_utils'.format(LOG_PREFIX)), output='Java'):
    "Based on what type when is, converts when to a Python DateTime object.\n    Type:\n        - int: returns now.plusMillis(when)\n        - openHAB number type: returns now.plusMillis(when.intValue())\n        - ISO8601 string: DateTime(when)\n        - Duration definition: see parse_duration_to_datetime\n        - java ZonedDateTime\n        For python make sure the datetime object is not assigned to a variable when this function is called)\n        otherwise a java.time.sql object will be returned due to a bug in Jython\n        - Python datetime\n        - Python time: returns DateTime with today date and system timezone\n\n    Arguments:\n        - when: the Object to convert to a DateTime\n        - log: optional logger, when not supplied one is created for logging errors\n        - output: object returned as a string. If not specified returns a ZonedDateTime object\n                  'Python': return datetime object\n                  'Java': return a ZonedDateTime object\n\n    Returns:\n        - ZonedDateTime specified by when\n        - datetime specified by when if output = 'Python'\n        - ZonedDateTime specified by when if output = 'Java'\n    "
    log.debug(((('when is: ' + str(when)) + ' output is ') + str(output)))
    dt_python = None
    dt_java = None
    try:
        if isinstance(when, (str, unicode)):
            if is_iso8601(when):
                log.debug(('when is iso8601: ' + str(when)))
                dt_java = ZonedDateTime.parse(str(when))
            else:
                log.debug(('when is duration: ' + str(when)))
                dt_python = (datetime.now() + parse_duration(when, log))
        elif isinstance(when, int):
            log.debug(('when is int: ' + str(when)))
            dt_java = ZonedDateTime.now().plus(when, ChronoUnit.MILLIS)
        elif isinstance(when, scope.DateTimeType):
            log.debug(('when is DateTimeType: ' + str(when)))
            dt_java = when.getZonedDateTime()
        elif isinstance(when, (scope.DecimalType, scope.PercentType, scope.QuantityType)):
            log.debug(('when is decimal, percent or quantity type: ' + str(when)))
            dt_python = (datetime.now() + timedelta(milliseconds=when.intValue()))
        elif isinstance(when, datetime):
            log.debug(('when is datetime: ' + str(when)))
            dt_python = when
        elif isinstance(when, ZonedDateTime):
            log.debug(('when is ZonedDateTime: ' + str(when)))
            dt_java = when
        elif isinstance(when, time):
            log.debug(('when is python time object: ' + str(when)))
            dt_java = ZonedDateTime.now().withHour(when.hour).withMinute(when.minute).withSecond(when.second).withNano((when.microsecond * 1000))
        else:
            log.warn('When is an unknown type {}'.format(type(when)))
            return None
    except:
        log.error('Exception: {}'.format(traceback.format_exc()))
    if (output == 'Python'):
        log.debug('returning dt python')
        return (dt_python if (dt_python is not None) else to_python_datetime(dt_java))
    elif (output == 'Java'):
        log.debug('returning dt java')
        return (dt_java if (dt_java is not None) else to_java_zoneddatetime(dt_python))
    elif (output == 'Joda'):
        log.error("to_datetime trying to return dt joda - use output = 'Python' or output = 'Java' instead")
    else:
        log.error('to_datetime cannot output [{}]'.format(output))
Based on what type when is, converts when to a Python DateTime object.
Type:
    - int: returns now.plusMillis(when)
    - openHAB number type: returns now.plusMillis(when.intValue())
    - ISO8601 string: DateTime(when)
    - Duration definition: see parse_duration_to_datetime
    - java ZonedDateTime
    For python make sure the datetime object is not assigned to a variable when this function is called)
    otherwise a java.time.sql object will be returned due to a bug in Jython
    - Python datetime
    - Python time: returns DateTime with today date and system timezone

Arguments:
    - when: the Object to convert to a DateTime
    - log: optional logger, when not supplied one is created for logging errors
    - output: object returned as a string. If not specified returns a ZonedDateTime object
              'Python': return datetime object
              'Java': return a ZonedDateTime object

Returns:
    - ZonedDateTime specified by when
    - datetime specified by when if output = 'Python'
    - ZonedDateTime specified by when if output = 'Java'
def to_datetime(when, log=logging.getLogger('{}.time_utils'.format(LOG_PREFIX)), output='Java'):
    "Based on what type when is, converts when to a Python DateTime object.\n    Type:\n        - int: returns now.plusMillis(when)\n        - openHAB number type: returns now.plusMillis(when.intValue())\n        - ISO8601 string: DateTime(when)\n        - Duration definition: see parse_duration_to_datetime\n        - java ZonedDateTime\n        For python make sure the datetime object is not assigned to a variable when this function is called)\n        otherwise a java.time.sql object will be returned due to a bug in Jython\n        - Python datetime\n        - Python time: returns DateTime with today date and system timezone\n\n    Arguments:\n        - when: the Object to convert to a DateTime\n        - log: optional logger, when not supplied one is created for logging errors\n        - output: object returned as a string. If not specified returns a ZonedDateTime object\n                  'Python': return datetime object\n                  'Java': return a ZonedDateTime object\n\n    Returns:\n        - ZonedDateTime specified by when\n        - datetime specified by when if output = 'Python'\n        - ZonedDateTime specified by when if output = 'Java'\n    "
    log.debug(((('when is: ' + str(when)) + ' output is ') + str(output)))
    dt_python = None
    dt_java = None
    try:
        if isinstance(when, (str, unicode)):
            if is_iso8601(when):
                log.debug(('when is iso8601: ' + str(when)))
                dt_java = ZonedDateTime.parse(str(when))
            else:
                log.debug(('when is duration: ' + str(when)))
                dt_python = (datetime.now() + parse_duration(when, log))
        elif isinstance(when, int):
            log.debug(('when is int: ' + str(when)))
            dt_java = ZonedDateTime.now().plus(when, ChronoUnit.MILLIS)
        elif isinstance(when, scope.DateTimeType):
            log.debug(('when is DateTimeType: ' + str(when)))
            dt_java = when.getZonedDateTime()
        elif isinstance(when, (scope.DecimalType, scope.PercentType, scope.QuantityType)):
            log.debug(('when is decimal, percent or quantity type: ' + str(when)))
            dt_python = (datetime.now() + timedelta(milliseconds=when.intValue()))
        elif isinstance(when, datetime):
            log.debug(('when is datetime: ' + str(when)))
            dt_python = when
        elif isinstance(when, ZonedDateTime):
            log.debug(('when is ZonedDateTime: ' + str(when)))
            dt_java = when
        elif isinstance(when, time):
            log.debug(('when is python time object: ' + str(when)))
            dt_java = ZonedDateTime.now().withHour(when.hour).withMinute(when.minute).withSecond(when.second).withNano((when.microsecond * 1000))
        else:
            log.warn('When is an unknown type {}'.format(type(when)))
            return None
    except:
        log.error('Exception: {}'.format(traceback.format_exc()))
    if (output == 'Python'):
        log.debug('returning dt python')
        return (dt_python if (dt_python is not None) else to_python_datetime(dt_java))
    elif (output == 'Java'):
        log.debug('returning dt java')
        return (dt_java if (dt_java is not None) else to_java_zoneddatetime(dt_python))
    elif (output == 'Joda'):
        log.error("to_datetime trying to return dt joda - use output = 'Python' or output = 'Java' instead")
    else:
        log.error('to_datetime cannot output [{}]'.format(output))