org.joda.time.MutablePeriod Java Examples
The following examples show how to use
org.joda.time.MutablePeriod.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DurationUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Parses a duration from a period formatted string. Values are accepted in the following formats: * * <p>Formats Ns - Seconds. Example: 5s<br> * Nm - Minutes. Example: 13m<br> * Nh - Hours. Example: 2h * * <pre> * parseDuration(null) = NullPointerException() * parseDuration("") = Duration.standardSeconds(0) * parseDuration("2s") = Duration.standardSeconds(2) * parseDuration("5m") = Duration.standardMinutes(5) * parseDuration("3h") = Duration.standardHours(3) * </pre> * * @param value The period value to parse. * @return The {@link Duration} parsed from the supplied period string. */ public static Duration parseDuration(String value) { checkNotNull(value, "The specified duration must be a non-null value!"); PeriodParser parser = new PeriodFormatterBuilder() .appendSeconds() .appendSuffix("s") .appendMinutes() .appendSuffix("m") .appendHours() .appendSuffix("h") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, value, 0, Locale.getDefault()); Duration duration = period.toDurationFrom(new DateTime(0)); checkArgument(duration.getMillis() > 0, "The window duration must be greater than 0!"); return duration; }
Example #2
Source File: DurationUtils.java From DataflowTemplates with Apache License 2.0 | 6 votes |
/** * Parses a duration from a period formatted string. Values are accepted in the following formats: * * <p>Formats Ns - Seconds. Example: 5s<br> * Nm - Minutes. Example: 13m<br> * Nh - Hours. Example: 2h * * <pre> * parseDuration(null) = NullPointerException() * parseDuration("") = Duration.standardSeconds(0) * parseDuration("2s") = Duration.standardSeconds(2) * parseDuration("5m") = Duration.standardMinutes(5) * parseDuration("3h") = Duration.standardHours(3) * </pre> * * @param value The period value to parse. * @return The {@link Duration} parsed from the supplied period string. */ public static Duration parseDuration(String value) { checkNotNull(value, "The specified duration must be a non-null value!"); PeriodParser parser = new PeriodFormatterBuilder() .appendSeconds() .appendSuffix("s") .appendMinutes() .appendSuffix("m") .appendHours() .appendSuffix("h") .toParser(); MutablePeriod period = new MutablePeriod(); parser.parseInto(period, value, 0, Locale.getDefault()); Duration duration = period.toDurationFrom(new DateTime(0)); checkArgument(duration.getMillis() > 0, "The window duration must be greater than 0!"); return duration; }
Example #3
Source File: ISODURATION.java From warp10-platform with Apache License 2.0 | 6 votes |
@Override public Object apply(WarpScriptStack stack) throws WarpScriptException { Object top = stack.pop(); if (!(top instanceof Long)) { throw new WarpScriptException(getName() + " expects a number of time units (LONG) on top of the stack."); } long duration = ((Number) top).longValue(); StringBuffer buf = new StringBuffer(); ReadablePeriod period = new MutablePeriod(duration / Constants.TIME_UNITS_PER_MS); ISOPeriodFormat.standard().getPrinter().printTo(buf, period, Locale.US); stack.push(buf.toString()); return stack; }
Example #4
Source File: TimeIntervalUtils.java From CloverETL-Engine with GNU Lesser General Public License v2.1 | 6 votes |
/** * Parses the input as a time interval in JIRA-like syntax. * Plain numbers without a unit are treated using the provided <code>unit</code>. * When <code>input</code> is <code>null</code> or an empty string, * <code>null</code> is returned. * * Returns the duration of the interval in milliseconds. * * @param input a time interval formatted using JIRA-like syntax * @param unit the default unit * @return duration of the interval in milliseconds * @throws IllegalArgumentException */ public static long parseInterval(String input, DefaultUnit unit) throws IllegalArgumentException { if (input == null) { throw new NullPointerException("input"); } if (input.isEmpty()) { throw new IllegalArgumentException("empty string"); } input = input.replace("ms", "x"); // replace "ms", which collides with "m", with non-conflicting "x" MutablePeriod parsedPeriod = new MutablePeriod(); int parseResult = parser.parseInto(parsedPeriod, input, 0, locale); if (parseResult < 0) { int position = ~parseResult; // bitwise complement to get the position of the error return parseLong(input, unit, position); } else if (parseResult < input.length()) { // the input has not been read to its end return parseLong(input, unit, parseResult); } return parsedPeriod.toPeriod().toStandardDuration().getMillis(); }
Example #5
Source File: DateTimeUtils.java From presto with Apache License 2.0 | 5 votes |
@Override public int parseInto(ReadWritablePeriod period, String text, int position, Locale locale) { int bestValidPos = position; ReadWritablePeriod bestValidPeriod = null; int bestInvalidPos = position; for (PeriodParser parser : parsers) { ReadWritablePeriod parsedPeriod = new MutablePeriod(); int parsePos = parser.parseInto(parsedPeriod, text, position, locale); if (parsePos >= position) { if (parsePos > bestValidPos) { bestValidPos = parsePos; bestValidPeriod = parsedPeriod; if (parsePos >= text.length()) { break; } } } else if (parsePos < 0) { parsePos = ~parsePos; if (parsePos > bestInvalidPos) { bestInvalidPos = parsePos; } } } if (bestValidPos > position || (bestValidPos == position)) { // Restore the state to the best valid parse. if (bestValidPeriod != null) { period.setPeriod(bestValidPeriod); } return bestValidPos; } return ~bestInvalidPos; }
Example #6
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object7() throws Exception { MutablePeriod m = new MutablePeriod(1, 0, 1, 1, 1, 1, 1, 1, PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3D", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #7
Source File: TestReadablePeriodConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetInto_Object() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); ReadablePeriodConverter.INSTANCE.setInto(m, new Period(0, 0, 0, 3, 0, 4, 0, 5), null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(4, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(5, m.getMillis()); }
Example #8
Source File: TestReadableIntervalConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object1() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
Example #9
Source File: TestReadableIntervalConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object2() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, CopticChronology.getInstance()); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
Example #10
Source File: TestPeriodFormatter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testParseMutablePeriod_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); assertEquals(expect, f.parseMutablePeriod("P1Y2M3W4DT5H6M7.008S")); try { f.parseMutablePeriod("ABC"); fail(); } catch (IllegalArgumentException ex) {} }
Example #11
Source File: TestPeriodFormatter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testParseInto_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); MutablePeriod result = new MutablePeriod(); assertEquals(20, f.parseInto(result, "P1Y2M3W4DT5H6M7.008S", 0)); assertEquals(expect, result); try { f.parseInto(null, "P1Y2M3W4DT5H6M7.008S", 0); fail(); } catch (IllegalArgumentException ex) {} assertEquals(~0, f.parseInto(result, "ABC", 0)); }
Example #12
Source File: BasePeriod.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Creates a new period based on another using the {@link ConverterManager}. * * @param period the period to convert * @param type which set of fields this period supports, null means use type from object * @param chrono the chronology to use, null means ISO default * @throws IllegalArgumentException if period is invalid * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected BasePeriod(Object period, PeriodType type, Chronology chrono) { super(); PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period); type = (type == null ? converter.getPeriodType(period) : type); type = checkPeriodType(type); iType = type; if (this instanceof ReadWritablePeriod) { iValues = new int[size()]; chrono = DateTimeUtils.getChronology(chrono); converter.setInto((ReadWritablePeriod) this, period, chrono); } else { iValues = new MutablePeriod(period, type, chrono).getValues(); } }
Example #13
Source File: PeriodFormatter.java From astor with GNU General Public License v2.0 | 5 votes |
/** * Parses a period from the given text, returning a new MutablePeriod. * * @param text text to parse * @return parsed value in a MutablePeriod object * @throws IllegalArgumentException if any field is out of range */ public MutablePeriod parseMutablePeriod(String text) { checkParser(); MutablePeriod period = new MutablePeriod(0, iParseType); int newPos = getParser().parseInto(period, text, 0, iLocale); if (newPos >= 0) { if (newPos >= text.length()) { return period; } } else { newPos = ~newPos; } throw new IllegalArgumentException(FormatUtils.createErrorMessage(text, newPos)); }
Example #14
Source File: TestReadableDurationConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetInto_Object() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); ReadableDurationConverter.INSTANCE.setInto(m, new Duration( 3L * DateTimeConstants.MILLIS_PER_DAY + 4L * DateTimeConstants.MILLIS_PER_MINUTE + 5L ), null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(3 * 24, m.getHours()); assertEquals(4, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(5, m.getMillis()); }
Example #15
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object1() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y6M9DT12H24M48S", null); assertEquals(2, m.getYears()); assertEquals(6, m.getMonths()); assertEquals(9, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #16
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object2() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M48S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #17
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object3() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M48.034S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(48, m.getSeconds()); assertEquals(34, m.getMillis()); }
Example #18
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object4() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M.056S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(56, m.getMillis()); }
Example #19
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object5() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M56.S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(56, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #20
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object6() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M56.1234567S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(56, m.getSeconds()); assertEquals(123, m.getMillis()); }
Example #21
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object7() throws Exception { MutablePeriod m = new MutablePeriod(1, 0, 1, 1, 1, 1, 1, 1, PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3D", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #22
Source File: TestReadablePeriodConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetInto_Object() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearMonthDayTime()); ReadablePeriodConverter.INSTANCE.setInto(m, new Period(0, 0, 0, 3, 0, 4, 0, 5), null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(0, m.getHours()); assertEquals(4, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(5, m.getMillis()); }
Example #23
Source File: TestReadableIntervalConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object1() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, null); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
Example #24
Source File: TestReadableIntervalConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object2() throws Exception { Interval i = new Interval(100L, 223L); MutablePeriod m = new MutablePeriod(PeriodType.millis()); ReadableIntervalConverter.INSTANCE.setInto(m, i, CopticChronology.getInstance()); assertEquals(0, m.getYears()); assertEquals(0, m.getMonths()); assertEquals(0, m.getWeeks()); assertEquals(0, m.getDays()); assertEquals(0, m.getHours()); assertEquals(0, m.getMinutes()); assertEquals(0, m.getSeconds()); assertEquals(123, m.getMillis()); }
Example #25
Source File: TestPeriodFormatter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testParseMutablePeriod_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); assertEquals(expect, f.parseMutablePeriod("P1Y2M3W4DT5H6M7.008S")); try { f.parseMutablePeriod("ABC"); fail(); } catch (IllegalArgumentException ex) {} }
Example #26
Source File: TestPeriodFormatter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testParseInto_simple() { MutablePeriod expect = new MutablePeriod(1, 2, 3, 4, 5, 6, 7, 8); MutablePeriod result = new MutablePeriod(); assertEquals(20, f.parseInto(result, "P1Y2M3W4DT5H6M7.008S", 0)); assertEquals(expect, result); try { f.parseInto(null, "P1Y2M3W4DT5H6M7.008S", 0); fail(); } catch (IllegalArgumentException ex) {} assertEquals(~0, f.parseInto(result, "ABC", 0)); }
Example #27
Source File: TestStringConverter.java From astor with GNU General Public License v2.0 | 5 votes |
public void testSetIntoPeriod_Object5() throws Exception { MutablePeriod m = new MutablePeriod(PeriodType.yearWeekDayTime()); StringConverter.INSTANCE.setInto(m, "P2Y4W3DT12H24M56.S", null); assertEquals(2, m.getYears()); assertEquals(4, m.getWeeks()); assertEquals(3, m.getDays()); assertEquals(12, m.getHours()); assertEquals(24, m.getMinutes()); assertEquals(56, m.getSeconds()); assertEquals(0, m.getMillis()); }
Example #28
Source File: ADDDURATION.java From warp10-platform with Apache License 2.0 | 5 votes |
/** * Convert an ISO8601 duration to a Period. * @param duration * @return * @throws WarpScriptException */ public static ReadWritablePeriodWithSubSecondOffset durationToPeriod(String duration) throws WarpScriptException { // Separate seconds from digits below second precision String[] tokens = UnsafeString.split(duration, '.'); long offset = 0; if (tokens.length > 2) { throw new WarpScriptException("Invalid ISO8601 duration"); } if (2 == tokens.length) { duration = tokens[0].concat("S"); String tmp = tokens[1].substring(0, tokens[1].length() - 1); try { offset = ((Double) (Double.parseDouble("0." + tmp) * Constants.TIME_UNITS_PER_S)).longValue(); } catch (NumberFormatException e) { throw new WarpScriptException("Parsing of sub second precision part of duration has failed. tried to parse: " + tmp); } } ReadWritablePeriod period = new MutablePeriod(); if (ISOPeriodFormat.standard().getParser().parseInto(period, duration, 0, Locale.US) < 0) { throw new WarpScriptException("Parsing of duration without sub second precision has failed. Tried to parse: " + duration); } return new ReadWritablePeriodWithSubSecondOffset(period, offset); }
Example #29
Source File: Time_22_BasePeriod_s.java From coming with MIT License | 5 votes |
/** * Creates a new period based on another using the {@link ConverterManager}. * * @param period the period to convert * @param type which set of fields this period supports, null means use type from object * @param chrono the chronology to use, null means ISO default * @throws IllegalArgumentException if period is invalid * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected BasePeriod(Object period, PeriodType type, Chronology chrono) { super(); PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period); type = (type == null ? converter.getPeriodType(period) : type); type = checkPeriodType(type); iType = type; if (this instanceof ReadWritablePeriod) { iValues = new int[size()]; chrono = DateTimeUtils.getChronology(chrono); converter.setInto((ReadWritablePeriod) this, period, chrono); } else { iValues = new MutablePeriod(period, type, chrono).getValues(); } }
Example #30
Source File: Time_22_BasePeriod_t.java From coming with MIT License | 5 votes |
/** * Creates a new period based on another using the {@link ConverterManager}. * * @param period the period to convert * @param type which set of fields this period supports, null means use type from object * @param chrono the chronology to use, null means ISO default * @throws IllegalArgumentException if period is invalid * @throws IllegalArgumentException if an unsupported field's value is non-zero */ protected BasePeriod(Object period, PeriodType type, Chronology chrono) { super(); PeriodConverter converter = ConverterManager.getInstance().getPeriodConverter(period); type = (type == null ? converter.getPeriodType(period) : type); type = checkPeriodType(type); iType = type; if (this instanceof ReadWritablePeriod) { iValues = new int[size()]; chrono = DateTimeUtils.getChronology(chrono); converter.setInto((ReadWritablePeriod) this, period, chrono); } else { iValues = new MutablePeriod(period, type, chrono).getValues(); } }