Example usage for java.time.zone ZoneRules getOffset

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

Introduction

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

Prototype

public ZoneOffset getOffset(LocalDateTime localDateTime) 

Source Link

Document

Gets a suitable offset for the specified local date-time in these rules.

Usage

From source file:Main.java

public static void main(String[] args) {
    LocalDateTime now = LocalDateTime.now();
    ZoneId usChicago = ZoneId.of("America/Chicago");
    System.out.println("Zone ID:  " + usChicago.getId());

    ZoneRules rules = usChicago.getRules();
    System.out.println("isFixedOffset(): " + rules.isFixedOffset());
    ZoneOffset offset = rules.getOffset(now);
    System.out.println("Zone offset: " + offset);

    List<ZoneOffsetTransition> transitions = rules.getTransitions();
    System.out.println(transitions);

}

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   w ww. ja v a  2  s . 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 {/*from   w  w w  .j  ava2  s. c om*/
        // 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;
}