Example usage for java.time.zone ZoneRules isDaylightSavings

List of usage examples for java.time.zone ZoneRules isDaylightSavings

Introduction

In this page you can find the example usage for java.time.zone ZoneRules isDaylightSavings.

Prototype

public boolean isDaylightSavings(Instant instant) 

Source Link

Document

Checks if the specified instant is in daylight savings.

Usage

From source file:org.nodatime.tzvalidate.Java8Dump.java

@Override
public ZoneTransitions getTransitions(String id, int fromYear, int toYear) {
    ZoneId zone = ZoneId.of(id);
    ZoneRules rules = zone.getRules();
    DateTimeFormatter nameFormat = DateTimeFormatter.ofPattern("zzz", Locale.US);
    ZoneTransitions transitions = new ZoneTransitions(id);

    Instant start = ZonedDateTime.of(fromYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();
    transitions.addTransition(null, rules.getOffset(start).getTotalSeconds() * 1000,
            rules.isDaylightSavings(start), nameFormat.format(start.atZone(zone)));
    Instant end = ZonedDateTime.of(toYear, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC).toInstant();

    ZoneOffsetTransition transition = rules.nextTransition(start.minusNanos(1));
    while (transition != null && transition.getInstant().isBefore(end)) {
        Instant instant = transition.getInstant();
        transitions.addTransition(new Date(instant.toEpochMilli()),
                rules.getOffset(instant).getTotalSeconds() * 1000, rules.isDaylightSavings(instant),
                nameFormat.format(instant.atZone(zone)));
        transition = rules.nextTransition(instant);
    }//from  ww  w . j a v  a  2s  .  co m
    return transitions;
}

From source file:org.openhab.binding.airvisualnode.internal.handler.AirVisualNodeHandler.java

private State getChannelState(String channelId, NodeData nodeData) {
    State state = UnDefType.UNDEF;

    // Handle system channel IDs separately, because 'switch/case' expressions must be constant expressions
    if (CHANNEL_BATTERY_LEVEL.equals(channelId)) {
        state = new DecimalType(BigDecimal.valueOf(nodeData.getStatus().getBattery()).longValue());
    } else if (CHANNEL_WIFI_STRENGTH.equals(channelId)) {
        state = new DecimalType(
                BigDecimal.valueOf(Math.max(0, nodeData.getStatus().getWifiStrength() - 1)).longValue());
    } else {/*  w  ww  . j  a  va2s  .c  o  m*/
        // Handle binding-specific channel IDs
        switch (channelId) {
        case CHANNEL_CO2:
            state = new QuantityType<>(nodeData.getMeasurements().getCo2Ppm(), PARTS_PER_MILLION);
            break;
        case CHANNEL_HUMIDITY:
            state = new QuantityType<>(nodeData.getMeasurements().getHumidityRH(), PERCENT);
            break;
        case CHANNEL_AQI_US:
            state = new QuantityType<>(nodeData.getMeasurements().getPm25AQIUS(), ONE);
            break;
        case CHANNEL_PM_25:
            // PM2.5 is in ug/m3
            state = new QuantityType<>(nodeData.getMeasurements().getPm25Ugm3(),
                    MICRO(GRAM).divide(CUBIC_METRE));
            break;
        case CHANNEL_TEMP_CELSIUS:
            state = new QuantityType<>(nodeData.getMeasurements().getTemperatureC(), CELSIUS);
            break;
        case CHANNEL_TIMESTAMP:
            // It seem the Node timestamp is Unix timestamp converted from UTC time plus timezone offset.
            // Not sure about DST though, but it's best guess at now
            Instant instant = Instant.ofEpochMilli(nodeData.getStatus().getDatetime() * 1000L);
            ZonedDateTime zonedDateTime = ZonedDateTime.ofInstant(instant, ZoneId.of("UTC"));
            ZoneId zoneId = ZoneId.of(nodeData.getSettings().getTimezone());
            ZoneRules zoneRules = zoneId.getRules();
            zonedDateTime.minus(Duration.ofSeconds(zoneRules.getOffset(instant).getTotalSeconds()));
            if (zoneRules.isDaylightSavings(instant)) {
                zonedDateTime.minus(Duration.ofSeconds(zoneRules.getDaylightSavings(instant).getSeconds()));
            }
            state = new DateTimeType(zonedDateTime);
            break;
        case CHANNEL_USED_MEMORY:
            state = new DecimalType(BigDecimal.valueOf(nodeData.getStatus().getUsedMemory()).longValue());
            break;
        }
    }

    return state;
}