Exemplo n.º 1
0
def to_java_zoneddatetime(value):
    """
    Converts any of the supported date types to ``java.time.ZonedDateTime``. If
    ``value`` does not have timezone information, the system default will be
    used.

    Examples:
        .. code-block::

            java_time = to_java_zoneddatetime(items["date_item"])

    Args:
        value: the value to convert

    Returns:
        java.time.ZonedDateTime: the converted value

    Raises:
        TypeError: if the type of ``value`` is not supported by this module
    """
    if isinstance(value, ZonedDateTime):
        return value
    timezone_id = ZoneId.systemDefault()
    # java.time.LocalDateTime
    if isinstance(value, LocalDateTime):
        return value.atZone(timezone_id)
    # python datetime
    if isinstance(value, datetime.datetime):
        if value.tzinfo is not None:
            timezone_id = ZoneId.ofOffset(
                "GMT",
                ZoneOffset.ofTotalSeconds(
                    int(value.utcoffset().total_seconds())))
        return ZonedDateTime.of(value.year, value.month, value.day, value.hour,
                                value.minute, value.second,
                                value.microsecond * 1000, timezone_id)
    # java.util.Calendar
    if isinstance(value, Calendar):
        return ZonedDateTime.ofInstant(value.toInstant(),
                                       ZoneId.of(value.getTimeZone().getID()))
    # java.util.Date
    if isinstance(value, Date):
        return ZonedDateTime.ofInstant(
            value.toInstant(),
            ZoneId.ofOffset(
                "GMT", ZoneOffset.ofHours(0 - value.getTimezoneOffset() / 60)))
    # Joda DateTime
    if JodaDateTime and isinstance(value, JodaDateTime):
        return value.toGregorianCalendar().toZonedDateTime()
    # openHAB DateTimeType
    if DateTimeType and isinstance(value, DateTimeType):
        return to_java_zoneddatetime(value.getZonedDateTime())
    # Eclipse Smarthome DateTimeType
    if EclipseDateTimeType and isinstance(value, EclipseDateTimeType):
        return to_java_zoneddatetime(value.calendar)
    # Legacy (OH1.x compat) DateTimeType
    if LegacyDateTimeType and isinstance(value, LegacyDateTimeType):
        return to_java_zoneddatetime(value.calendar)

    raise TypeError("Unknown type: {}".format(str(type(value))))
Exemplo n.º 2
0
def to_java_zoneddatetime(value):
    """Converts any known DateTime type to a ``java.time.ZonedDateTime`` type.

    Args:
        value: any known DateTime value.
    
    Returns:
        | A ``java.time.ZonedDateTime`` representing ``value``.
        | If ``value`` does not have timezone information, the system default
          will be used.
    
    Raises:
        TypeError: type of ``value`` is not recognized by this package.
    """
    timezone_id = ZoneId.systemDefault()
    if isinstance(value, ZonedDateTime):
        return value
    # java.time.LocalDateTime
    if isinstance(value, LocalDateTime):
        return value.atZone(timezone_id)
    # python datetime
    if isinstance(value, datetime.datetime):
        if value.tzinfo is not None:
            timezone_id = ZoneId.ofOffset(
                "GMT",
                ZoneOffset.ofTotalSeconds(value.utcoffset().total_seconds()))
        return ZonedDateTime.of(value.year, value.month, value.day, value.hour,
                                value.minute, value.second,
                                value.microsecond * 1000, timezone_id)
    # java.util.Calendar
    if isinstance(value, Calendar):
        return ZonedDateTime.ofInstant(value.toInstant(),
                                       ZoneId.of(value.getTimeZone().getID()))
    # java.util.Date
    if isinstance(value, Date):
        return ZonedDateTime.ofInstant(
            value.toInstant(),
            ZoneId.ofOffset(
                "GMT",
                ZoneOffset.ofTotalSeconds(value.getTimezoneOffset() * 60)))
    # Joda DateTime
    if isinstance(value, DateTime):
        return value.toGregorianCalendar().toZonedDateTime()
    # OH DateTimeType or ESH DateTimeType
    if isinstance(value, (LegacyDateTimeType, DateTimeType)):
        return to_java_zoneddatetime(value.calendar)

    raise TypeError("Unknown type: " + str(type(value)))
Exemplo n.º 3
0
def to_java_zoneddatetime(value):
    '''Returns java.time.ZonedDateTime (with system timezone, if none specified). 
    Accepts any date type used by this module.'''
    timezone_id = ZoneId.systemDefault()
    if isinstance(value, ZonedDateTime):
        return value
    # java.time.LocalDateTime
    if isinstance(value, LocalDateTime):
        return value.atZone(timezone_id)
    # python datetime
    if isinstance(value, datetime.datetime):
        if value.tzinfo is not None:
            timezone_id = ZoneId.ofOffset(
                "GMT",
                ZoneOffset.ofTotalSeconds(value.utcoffset().total_seconds()))
        return ZonedDateTime.of(value.year, value.month, value.day, value.hour,
                                value.minute, value.second,
                                value.microsecond * 1000, timezone_id)
    # java.util.Calendar
    if isinstance(value, Calendar):
        return ZonedDateTime.ofInstant(value.toInstant(),
                                       ZoneId.of(value.getTimeZone().getID()))
    # java.util.Date
    if isinstance(value, Date):
        return ZonedDateTime.ofInstant(
            value.toInstant(),
            ZoneId.ofOffset(
                "GMT",
                ZoneOffset.ofTotalSeconds(value.getTimezoneOffset() * 60)))
    # Joda DateTime
    if isinstance(value, DateTime):
        return value.toGregorianCalendar().toZonedDateTime()
    # OH DateTimeType or ESH DateTimeType
    if isinstance(value, (LegacyDateTimeType, DateTimeType)):
        return to_java_zoneddatetime(value.calendar)

    raise Exception("Invalid conversion: " + str(type(value)))