Example #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))))
Example #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)))
    def _getSunData(time):
        # Constants
        K = 0.017453

        # Change this reflecting your destination
        latitude = 52.347767
        longitude = 13.621287

        # allways +1 Berlin winter time
        local = time.withZoneSameInstant(ZoneOffset.ofHours(1))

        day = local.getDayOfYear()
        hour = local.getHour() + (local.getMinute() / 60.0)

        # Source: http://www.jgiesen.de/SME/tk/index.htm
        deklination = -23.45 * math.cos(K * 360 * (day + 10) / 365)
        zeitgleichung = 60.0 * (-0.171 * math.sin(0.0337 * day + 0.465) -
                                0.1299 * math.sin(0.01787 * day - 0.168))
        stundenwinkel = 15.0 * (hour - (15.0 - longitude) / 15.0 - 12.0 +
                                zeitgleichung / 60.0)
        x = math.sin(K * latitude) * math.sin(K * deklination) + math.cos(
            K * latitude) * math.cos(K * deklination) * math.cos(
                K * stundenwinkel)
        y = -(math.sin(K * latitude) * x - math.sin(K * deklination)) / (
            math.cos(K * latitude) * math.sin(math.acos(x)))
        elevation = math.asin(x) / K

        isBreak = hour <= 12.0 + (15.0 -
                                  longitude) / 15.0 - zeitgleichung / 60.0
        if isBreak: azimut = math.acos(y) / K
        else: azimut = 360.0 - math.acos(y) / K

        return elevation, azimut
Example #4
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)))