use of java.time.temporal.ChronoUnit.MICROS 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;
}
}
use of java.time.temporal.ChronoUnit.MICROS 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;
}
}
Aggregations