Search in sources :

Example 61 with ZoneOffset

use of java.time.ZoneOffset in project jdk8u_jdk by JetBrains.

the class ZoneInfoOld method transToString.

private int transToString(long val, int off_old, int[] offsets, StringBuilder sb) {
    int dst = (int) ((val >>> DST_NSHIFT) & 0xfL);
    int save = (dst == 0) ? 0 : offsets[dst] / 1000;
    int off = offsets[(int) (val & OFFSET_MASK)] / 1000;
    long second = (val >> TRANSITION_NSHIFT) / 1000;
    ZoneOffset offset_old = ZoneOffset.ofTotalSeconds(off_old);
    ZoneOffset offset = ZoneOffset.ofTotalSeconds(off);
    sb.append("          " + LocalDateTime.ofEpochSecond(second, 0, offset_old));
    sb.append("  [utc=" + second + "   raw=" + Long.toHexString(val >> TRANSITION_NSHIFT) + ", offset=" + off + "/" + offset + ", saving=" + save + "]");
    return off;
}
Also used : ZoneOffset(java.time.ZoneOffset)

Example 62 with ZoneOffset

use of java.time.ZoneOffset in project jmeter by apache.

the class TimeShift method execute.

/** {@inheritDoc} */
@Override
public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
    String dateString;
    LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
    DateTimeFormatter formatter = null;
    if (!StringUtils.isEmpty(format)) {
        try {
            formatter = dateTimeFormatterCache.get(format, key -> createFormatter((String) key));
        } catch (IllegalArgumentException ex) {
            // $NON-NLS-1$
            log.error("Format date pattern '{}' is invalid (see https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)", format, ex);
            return "";
        }
    }
    if (!dateToShift.isEmpty()) {
        try {
            if (formatter != null) {
                localDateTimeToShift = LocalDateTime.parse(dateToShift, formatter);
            } else {
                localDateTimeToShift = LocalDateTime.ofInstant(Instant.ofEpochMilli(Long.parseLong(dateToShift)), ZoneId.systemDefault());
            }
        } catch (DateTimeParseException | NumberFormatException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the date '{}' to shift", dateToShift, ex);
        }
    }
    // Check amount value to shift
    if (!StringUtils.isEmpty(amountToShift)) {
        try {
            Duration duration = Duration.parse(amountToShift);
            localDateTimeToShift = localDateTimeToShift.plus(duration);
        } catch (DateTimeParseException ex) {
            // $NON-NLS-1$
            log.error("Failed to parse the amount duration '{}' to shift (see https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-) ", amountToShift, ex);
        }
    }
    if (formatter != null) {
        dateString = localDateTimeToShift.format(formatter);
    } else {
        ZoneOffset offset = ZoneOffset.systemDefault().getRules().getOffset(localDateTimeToShift);
        dateString = String.valueOf(localDateTimeToShift.toInstant(offset).toEpochMilli());
    }
    if (!StringUtils.isEmpty(variableName)) {
        JMeterVariables vars = getVariables();
        if (vars != null) {
            // vars will be null on TestPlan
            vars.put(variableName, dateString);
        }
    }
    return dateString;
}
Also used : LocalDateTime(java.time.LocalDateTime) DateTimeFormatterBuilder(java.time.format.DateTimeFormatterBuilder) ChronoField(java.time.temporal.ChronoField) JMeterUtils(org.apache.jmeter.util.JMeterUtils) Arrays(java.util.Arrays) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) Collection(java.util.Collection) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) SampleResult(org.apache.jmeter.samplers.SampleResult) Instant(java.time.Instant) Cache(com.github.benmanes.caffeine.cache.Cache) StringUtils(org.apache.commons.lang3.StringUtils) ZoneId(java.time.ZoneId) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) DateTimeParseException(java.time.format.DateTimeParseException) List(java.util.List) CompoundVariable(org.apache.jmeter.engine.util.CompoundVariable) Year(java.time.Year) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset) Sampler(org.apache.jmeter.samplers.Sampler) DateTimeParseException(java.time.format.DateTimeParseException) JMeterVariables(org.apache.jmeter.threads.JMeterVariables) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) ZoneOffset(java.time.ZoneOffset)

Aggregations

ZoneOffset (java.time.ZoneOffset)62 Test (org.testng.annotations.Test)35 LocalDateTime (java.time.LocalDateTime)9 ZoneRules (java.time.zone.ZoneRules)9 ZoneId (java.time.ZoneId)8 ZonedDateTime (java.time.ZonedDateTime)7 Instant (java.time.Instant)6 DateTimeFormatter (java.time.format.DateTimeFormatter)5 OffsetDateTime (java.time.OffsetDateTime)4 ZoneOffsetTransition (java.time.zone.ZoneOffsetTransition)4 Clock (java.time.Clock)3 LocalTime (java.time.LocalTime)2 ChronoField (java.time.temporal.ChronoField)2 Cache (com.github.benmanes.caffeine.cache.Cache)1 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 DateTimeException (java.time.DateTimeException)1 DayOfWeek (java.time.DayOfWeek)1 Duration (java.time.Duration)1 LocalDate (java.time.LocalDate)1 Month (java.time.Month)1