Search in sources :

Example 1 with MONTHS

use of java.time.temporal.ChronoUnit.MONTHS in project phoebus-olog by Olog.

the class TimeParser method parseTemporalAmount.

/**
 * Parse a temporal amount like "1 month 2 days" or "1 day 20 seconds"
 *
 *  <p>Provides either a time-based {@link Duration}
 *  or a calendar based {@link Period}.
 *
 *  <p>A period of "1 months" does not have a well defined
 *  length in time because a months could have 28 to 31 days.
 *  When the user specifies "1 month" we assume that
 *  a time span between the same day in different months
 *  is requested.
 *  As soon as the time span includes a month or year,
 *  a {@link Period} is returned and the smaller units
 *  from hours down are ignored.
 *
 *  <p>For time spans that only include days or less,
 *  a {@link Duration} is used.
 *
 *  @param string Text
 *  @return {@link Duration} or {@link Period}
 */
public static TemporalAmount parseTemporalAmount(final String string) {
    if (NOW.equalsIgnoreCase(string))
        return Duration.ZERO;
    final Matcher timeQuantityUnitsMatcher = timeQuantityUnitsPattern.matcher(string);
    final Map<ChronoUnit, Integer> timeQuantities = new HashMap<>();
    boolean use_period = false;
    while (timeQuantityUnitsMatcher.find()) {
        final double quantity = "".equals(timeQuantityUnitsMatcher.group(1)) ? 1.0 : Double.valueOf(timeQuantityUnitsMatcher.group(1));
        final int full = (int) quantity;
        final double fraction = quantity - full;
        final String unit = timeQuantityUnitsMatcher.group(2).toLowerCase();
        // -> We place fractional amounts in the next finer unit.
        if (unit.startsWith("y")) {
            timeQuantities.put(YEARS, full);
            if (fraction > 0) {
                final int next = (int) (fraction * 12 + 0.5);
                timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("mo")) {
            timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 4 * 7 + 0.5);
                timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("w")) {
            timeQuantities.compute(WEEKS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 7 + 0.5);
                timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("mi")) {
            timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 60 + 0.5);
                timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("h")) {
            timeQuantities.compute(HOURS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 60 + 0.5);
                timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("d")) {
            timeQuantities.compute(DAYS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 24 + 0.5);
                timeQuantities.compute(HOURS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("s")) {
            timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 1000 + 0.5);
                timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.equals("ms")) {
            timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 1000 + 0.5);
                timeQuantities.compute(MICROS, (u, prev) -> prev == null ? next : prev + next);
            }
        }
    }
    if (use_period) {
        Period result = Period.ZERO;
        if (timeQuantities.containsKey(YEARS))
            result = result.plusYears(timeQuantities.get(YEARS));
        if (timeQuantities.containsKey(WEEKS))
            result = result.plusDays(7 * timeQuantities.get(WEEKS));
        if (timeQuantities.containsKey(MONTHS))
            result = result.plusMonths(timeQuantities.get(MONTHS));
        if (timeQuantities.containsKey(DAYS))
            result = result.plusDays(timeQuantities.get(DAYS));
        // Ignoring hours, min, .. because they're insignificant compared to weeks
        return result;
    } else {
        Duration result = Duration.ofSeconds(0);
        for (Entry<ChronoUnit, Integer> entry : timeQuantities.entrySet()) result = result.plus(entry.getValue(), entry.getKey());
        return result;
    }
}
Also used : Period(java.time.Period) MONTHS(java.time.temporal.ChronoUnit.MONTHS) SECONDS(java.time.temporal.ChronoUnit.SECONDS) YEARS(java.time.temporal.ChronoUnit.YEARS) HOURS(java.time.temporal.ChronoUnit.HOURS) WEEKS(java.time.temporal.ChronoUnit.WEEKS) HashMap(java.util.HashMap) Instant(java.time.Instant) DAYS(java.time.temporal.ChronoUnit.DAYS) ChronoUnit(java.time.temporal.ChronoUnit) Matcher(java.util.regex.Matcher) MINUTES(java.time.temporal.ChronoUnit.MINUTES) MICROS(java.time.temporal.ChronoUnit.MICROS) Duration(java.time.Duration) Map(java.util.Map) Entry(java.util.Map.Entry) TemporalAmount(java.time.temporal.TemporalAmount) Pattern(java.util.regex.Pattern) MILLIS(java.time.temporal.ChronoUnit.MILLIS) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Period(java.time.Period) Duration(java.time.Duration) ChronoUnit(java.time.temporal.ChronoUnit)

Example 2 with MONTHS

use of java.time.temporal.ChronoUnit.MONTHS in project phoebus by ControlSystemStudio.

the class TimeParser method parseTemporalAmount.

/**
 * Parse a temporal amount like "1 month 2 days" or "1 day 20 seconds"
 *
 *  <p>Provides either a time-based {@link Duration}
 *  or a calendar based {@link Period}.
 *
 *  <p>A period of "1 months" does not have a well defined
 *  length in time because a months could have 28 to 31 days.
 *  When the user specifies "1 month" we assume that
 *  a time span between the same day in different months
 *  is requested.
 *  As soon as the time span includes a month or year,
 *  a {@link Period} is returned and the smaller units
 *  from hours down are ignored.
 *
 *  <p>For time spans that only include days or less,
 *  a {@link Duration} is used.
 *
 *  @param string Text
 *  @return {@link Duration} or {@link Period}
 */
public static TemporalAmount parseTemporalAmount(final String string) {
    if (NOW.equalsIgnoreCase(string))
        return Duration.ZERO;
    final Matcher timeQuantityUnitsMatcher = timeQuantityUnitsPattern.matcher(string);
    final Map<ChronoUnit, Integer> timeQuantities = new HashMap<>();
    boolean use_period = false;
    while (timeQuantityUnitsMatcher.find()) {
        final double quantity = "".equals(timeQuantityUnitsMatcher.group(1)) ? 1.0 : Double.valueOf(timeQuantityUnitsMatcher.group(1));
        final int full = (int) quantity;
        final double fraction = quantity - full;
        final String unit = timeQuantityUnitsMatcher.group(2).toLowerCase();
        // -> We place fractional amounts in the next finer unit.
        if (unit.startsWith("y")) {
            timeQuantities.put(YEARS, full);
            if (fraction > 0) {
                final int next = (int) (fraction * 12 + 0.5);
                timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("mo")) {
            timeQuantities.compute(MONTHS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 4 * 7 + 0.5);
                timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("w")) {
            timeQuantities.compute(WEEKS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 7 + 0.5);
                timeQuantities.compute(DAYS, (u, prev) -> prev == null ? next : prev + next);
            }
            use_period = true;
        } else if (unit.startsWith("mi")) {
            timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 60 + 0.5);
                timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("h")) {
            timeQuantities.compute(HOURS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 60 + 0.5);
                timeQuantities.compute(MINUTES, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("d")) {
            timeQuantities.compute(DAYS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 24 + 0.5);
                timeQuantities.compute(HOURS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.startsWith("s")) {
            timeQuantities.compute(SECONDS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 1000 + 0.5);
                timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? next : prev + next);
            }
        } else if (unit.equals("ms")) {
            timeQuantities.compute(MILLIS, (u, prev) -> prev == null ? full : prev + full);
            if (fraction > 0) {
                final int next = (int) (fraction * 1000 + 0.5);
                timeQuantities.compute(MICROS, (u, prev) -> prev == null ? next : prev + next);
            }
        }
    }
    if (use_period) {
        Period result = Period.ZERO;
        if (timeQuantities.containsKey(YEARS))
            result = result.plusYears(timeQuantities.get(YEARS));
        if (timeQuantities.containsKey(WEEKS))
            result = result.plusDays(7 * timeQuantities.get(WEEKS));
        if (timeQuantities.containsKey(MONTHS))
            result = result.plusMonths(timeQuantities.get(MONTHS));
        if (timeQuantities.containsKey(DAYS))
            result = result.plusDays(timeQuantities.get(DAYS));
        // Ignoring hours, min, .. because they're insignificant compared to weeks
        return result;
    } else {
        Duration result = Duration.ofSeconds(0);
        for (Entry<ChronoUnit, Integer> entry : timeQuantities.entrySet()) result = result.plus(entry.getValue(), entry.getKey());
        return result;
    }
}
Also used : Period(java.time.Period) MONTHS(java.time.temporal.ChronoUnit.MONTHS) SECONDS(java.time.temporal.ChronoUnit.SECONDS) YEARS(java.time.temporal.ChronoUnit.YEARS) HOURS(java.time.temporal.ChronoUnit.HOURS) TimeZone(java.util.TimeZone) WEEKS(java.time.temporal.ChronoUnit.WEEKS) LocalDateTime(java.time.LocalDateTime) HashMap(java.util.HashMap) Instant(java.time.Instant) DAYS(java.time.temporal.ChronoUnit.DAYS) ChronoUnit(java.time.temporal.ChronoUnit) Matcher(java.util.regex.Matcher) MINUTES(java.time.temporal.ChronoUnit.MINUTES) MICROS(java.time.temporal.ChronoUnit.MICROS) Duration(java.time.Duration) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) Entry(java.util.Map.Entry) TemporalAmount(java.time.temporal.TemporalAmount) Pattern(java.util.regex.Pattern) MILLIS(java.time.temporal.ChronoUnit.MILLIS) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) Period(java.time.Period) Duration(java.time.Duration) ChronoUnit(java.time.temporal.ChronoUnit)

Aggregations

Duration (java.time.Duration)2 Instant (java.time.Instant)2 Period (java.time.Period)2 ChronoUnit (java.time.temporal.ChronoUnit)2 DAYS (java.time.temporal.ChronoUnit.DAYS)2 HOURS (java.time.temporal.ChronoUnit.HOURS)2 MICROS (java.time.temporal.ChronoUnit.MICROS)2 MILLIS (java.time.temporal.ChronoUnit.MILLIS)2 MINUTES (java.time.temporal.ChronoUnit.MINUTES)2 MONTHS (java.time.temporal.ChronoUnit.MONTHS)2 SECONDS (java.time.temporal.ChronoUnit.SECONDS)2 WEEKS (java.time.temporal.ChronoUnit.WEEKS)2 YEARS (java.time.temporal.ChronoUnit.YEARS)2 TemporalAmount (java.time.temporal.TemporalAmount)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Matcher (java.util.regex.Matcher)2 Pattern (java.util.regex.Pattern)2 LocalDateTime (java.time.LocalDateTime)1