Search in sources :

Example 36 with MutableDateTime

use of org.joda.time.MutableDateTime in project midpoint by Evolveum.

the class DateInput method computeDateTime.

public Date computeDateTime() {
    Date dateFieldInputValue = getDateTextField().getModelObject();
    if (dateFieldInputValue == null) {
        return null;
    }
    Integer hoursInput = ((TextField<Integer>) get(HOURS)).getModelObject();
    Integer minutesInput = ((TextField<Integer>) get(MINUTES)).getModelObject();
    AM_PM amOrPmInput = ((DropDownChoice<DateTimeField.AM_PM>) get(AM_OR_PM_CHOICE)).getModelObject();
    // Get year, month and day ignoring any timezone of the Date object
    Calendar cal = Calendar.getInstance();
    cal.setTime(dateFieldInputValue);
    int year = cal.get(Calendar.YEAR);
    int month = cal.get(Calendar.MONTH) + 1;
    int day = cal.get(Calendar.DAY_OF_MONTH);
    int hours = (hoursInput == null ? 0 : hoursInput % 24);
    int minutes = (minutesInput == null ? 0 : minutesInput);
    // Use the input to create a date object with proper timezone
    MutableDateTime date = new MutableDateTime(year, month, day, hours, minutes, 0, 0, DateTimeZone.forTimeZone(getClientTimeZone()));
    // Adjust for halfday if needed
    if (use12HourFormat()) {
        int halfday = (amOrPmInput == AM_PM.PM ? 1 : 0);
        date.set(DateTimeFieldType.halfdayOfDay(), halfday);
        date.set(DateTimeFieldType.hourOfHalfday(), hours % 12);
    }
    // The date will be in the server's timezone
    Date convertedDateValue = newDateInstance(date.getMillis());
    setConvertedInput(convertedDateValue);
    return convertedDateValue;
}
Also used : DropDownChoice(org.apache.wicket.markup.html.form.DropDownChoice) GregorianCalendar(java.util.GregorianCalendar) Calendar(java.util.Calendar) TextField(org.apache.wicket.markup.html.form.TextField) DateTextField(org.apache.wicket.datetime.markup.html.form.DateTextField) MutableDateTime(org.joda.time.MutableDateTime) DateTimeField(org.apache.wicket.extensions.yui.calendar.DateTimeField) Date(java.util.Date)

Example 37 with MutableDateTime

use of org.joda.time.MutableDateTime in project elasticsearch by elastic.

the class DateMathParser method parseMath.

private long parseMath(String mathString, long time, boolean roundUp, DateTimeZone timeZone) throws ElasticsearchParseException {
    if (timeZone == null) {
        timeZone = DateTimeZone.UTC;
    }
    MutableDateTime dateTime = new MutableDateTime(time, timeZone);
    for (int i = 0; i < mathString.length(); ) {
        char c = mathString.charAt(i++);
        final boolean round;
        final int sign;
        if (c == '/') {
            round = true;
            sign = 1;
        } else {
            round = false;
            if (c == '+') {
                sign = 1;
            } else if (c == '-') {
                sign = -1;
            } else {
                throw new ElasticsearchParseException("operator not supported for date math [{}]", mathString);
            }
        }
        if (i >= mathString.length()) {
            throw new ElasticsearchParseException("truncated date math [{}]", mathString);
        }
        final int num;
        if (!Character.isDigit(mathString.charAt(i))) {
            num = 1;
        } else {
            int numFrom = i;
            while (i < mathString.length() && Character.isDigit(mathString.charAt(i))) {
                i++;
            }
            if (i >= mathString.length()) {
                throw new ElasticsearchParseException("truncated date math [{}]", mathString);
            }
            num = Integer.parseInt(mathString.substring(numFrom, i));
        }
        if (round) {
            if (num != 1) {
                throw new ElasticsearchParseException("rounding `/` can only be used on single unit types [{}]", mathString);
            }
        }
        char unit = mathString.charAt(i++);
        MutableDateTime.Property propertyToRound = null;
        switch(unit) {
            case 'y':
                if (round) {
                    propertyToRound = dateTime.yearOfCentury();
                } else {
                    dateTime.addYears(sign * num);
                }
                break;
            case 'M':
                if (round) {
                    propertyToRound = dateTime.monthOfYear();
                } else {
                    dateTime.addMonths(sign * num);
                }
                break;
            case 'w':
                if (round) {
                    propertyToRound = dateTime.weekOfWeekyear();
                } else {
                    dateTime.addWeeks(sign * num);
                }
                break;
            case 'd':
                if (round) {
                    propertyToRound = dateTime.dayOfMonth();
                } else {
                    dateTime.addDays(sign * num);
                }
                break;
            case 'h':
            case 'H':
                if (round) {
                    propertyToRound = dateTime.hourOfDay();
                } else {
                    dateTime.addHours(sign * num);
                }
                break;
            case 'm':
                if (round) {
                    propertyToRound = dateTime.minuteOfHour();
                } else {
                    dateTime.addMinutes(sign * num);
                }
                break;
            case 's':
                if (round) {
                    propertyToRound = dateTime.secondOfMinute();
                } else {
                    dateTime.addSeconds(sign * num);
                }
                break;
            default:
                throw new ElasticsearchParseException("unit [{}] not supported for date math [{}]", unit, mathString);
        }
        if (propertyToRound != null) {
            if (roundUp) {
                // we want to go up to the next whole value, even if we are already on a rounded value
                propertyToRound.add(1);
                propertyToRound.roundFloor();
                // subtract 1 millisecond to get the largest inclusive value
                dateTime.addMillis(-1);
            } else {
                propertyToRound.roundFloor();
            }
        }
    }
    return dateTime.getMillis();
}
Also used : ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) MutableDateTime(org.joda.time.MutableDateTime)

Example 38 with MutableDateTime

use of org.joda.time.MutableDateTime in project elasticsearch by elastic.

the class SimpleJodaTests method testRounding.

public void testRounding() {
    long TIME = utcTimeInMillis("2009-02-03T01:01:01");
    MutableDateTime time = new MutableDateTime(DateTimeZone.UTC);
    time.setMillis(TIME);
    assertThat(time.monthOfYear().roundFloor().toString(), equalTo("2009-02-01T00:00:00.000Z"));
    time.setMillis(TIME);
    assertThat(time.hourOfDay().roundFloor().toString(), equalTo("2009-02-03T01:00:00.000Z"));
    time.setMillis(TIME);
    assertThat(time.dayOfMonth().roundFloor().toString(), equalTo("2009-02-03T00:00:00.000Z"));
}
Also used : MutableDateTime(org.joda.time.MutableDateTime)

Example 39 with MutableDateTime

use of org.joda.time.MutableDateTime in project elasticsearch by elastic.

the class SimpleJodaTests method testRoundingSetOnTime.

public void testRoundingSetOnTime() {
    MutableDateTime time = new MutableDateTime(DateTimeZone.UTC);
    time.setRounding(time.getChronology().monthOfYear(), MutableDateTime.ROUND_FLOOR);
    time.setMillis(utcTimeInMillis("2009-02-03T01:01:01"));
    assertThat(time.toString(), equalTo("2009-02-01T00:00:00.000Z"));
    assertThat(time.getMillis(), equalTo(utcTimeInMillis("2009-02-01T00:00:00.000Z")));
    time.setMillis(utcTimeInMillis("2009-05-03T01:01:01"));
    assertThat(time.toString(), equalTo("2009-05-01T00:00:00.000Z"));
    assertThat(time.getMillis(), equalTo(utcTimeInMillis("2009-05-01T00:00:00.000Z")));
    time = new MutableDateTime(DateTimeZone.UTC);
    time.setRounding(time.getChronology().dayOfMonth(), MutableDateTime.ROUND_FLOOR);
    time.setMillis(utcTimeInMillis("2009-02-03T01:01:01"));
    assertThat(time.toString(), equalTo("2009-02-03T00:00:00.000Z"));
    assertThat(time.getMillis(), equalTo(utcTimeInMillis("2009-02-03T00:00:00.000Z")));
    time.setMillis(utcTimeInMillis("2009-02-02T23:01:01"));
    assertThat(time.toString(), equalTo("2009-02-02T00:00:00.000Z"));
    assertThat(time.getMillis(), equalTo(utcTimeInMillis("2009-02-02T00:00:00.000Z")));
    time = new MutableDateTime(DateTimeZone.UTC);
    time.setRounding(time.getChronology().weekOfWeekyear(), MutableDateTime.ROUND_FLOOR);
    time.setMillis(utcTimeInMillis("2011-05-05T01:01:01"));
    assertThat(time.toString(), equalTo("2011-05-02T00:00:00.000Z"));
    assertThat(time.getMillis(), equalTo(utcTimeInMillis("2011-05-02T00:00:00.000Z")));
}
Also used : MutableDateTime(org.joda.time.MutableDateTime)

Example 40 with MutableDateTime

use of org.joda.time.MutableDateTime in project h2o-3 by h2oai.

the class AstMktime method apply.

@Override
public Val apply(Env env, Env.StackHelp stk, AstRoot[] asts) {
    // Seven args, all required.  See if any are arrays.
    Frame[] fs = new Frame[nargs() - 1];
    int[] is = new int[nargs() - 1];
    // Sample frame (for auto-expanding constants)
    Frame x = null;
    for (int i = 1; i < nargs(); i++) if (asts[i] instanceof AstId || asts[i] instanceof AstExec)
        fs[i - 1] = x = stk.track(asts[i].exec(env)).getFrame();
    else
        is[i - 1] = (int) asts[i].exec(env).getNum();
    if (x == null) {
        // Single point
        long msec = new MutableDateTime(// year
        is[0], // month
        is[1] + 1, // day
        is[2] + 1, // hour
        is[3], // minute
        is[4], // second
        is[5], // msec
        is[6]).getMillis();
        return new ValNum(msec);
    }
    // Make constant Vecs for the constant args.  Commonly, they'll all be zero
    Vec[] vecs = new Vec[7];
    for (int i = 0; i < 7; i++) {
        if (fs[i] == null) {
            vecs[i] = x.anyVec().makeCon(is[i]);
        } else {
            if (fs[i].numCols() != 1)
                throw new IllegalArgumentException("Expect single column");
            vecs[i] = fs[i].anyVec();
        }
    }
    // Convert whole column to epoch msec
    Frame fr2 = new MRTask() {

        @Override
        public void map(Chunk[] chks, NewChunk[] nchks) {
            MutableDateTime dt = new MutableDateTime(0);
            NewChunk n = nchks[0];
            int rlen = chks[0]._len;
            for (int r = 0; r < rlen; r++) {
                dt.setDateTime(// year
                (int) chks[0].at8(r), // month
                (int) chks[1].at8(r) + 1, // day
                (int) chks[2].at8(r) + 1, // hour
                (int) chks[3].at8(r), // minute
                (int) chks[4].at8(r), // second
                (int) chks[5].at8(r), // msec
                (int) chks[6].at8(r));
                n.addNum(dt.getMillis());
            }
        }
    }.doAll(new byte[] { Vec.T_NUM }, vecs).outputFrame(new String[] { "msec" }, null);
    // Clean up the constants
    for (int i = 0; i < nargs() - 1; i++) if (fs[i] == null)
        vecs[i].remove();
    return new ValFrame(fr2);
}
Also used : ValFrame(water.rapids.vals.ValFrame) Frame(water.fvec.Frame) AstExec(water.rapids.ast.AstExec) MutableDateTime(org.joda.time.MutableDateTime) ValNum(water.rapids.vals.ValNum) NewChunk(water.fvec.NewChunk) ValFrame(water.rapids.vals.ValFrame) AstId(water.rapids.ast.params.AstId) Vec(water.fvec.Vec) MRTask(water.MRTask)

Aggregations

MutableDateTime (org.joda.time.MutableDateTime)76 DateTime (org.joda.time.DateTime)12 Instant (org.joda.time.Instant)6 DateTimeFormatter (org.joda.time.format.DateTimeFormatter)6 Date (java.util.Date)5 Chronology (org.joda.time.Chronology)4 DateTimeZone (org.joda.time.DateTimeZone)4 ISOChronology (org.joda.time.chrono.ISOChronology)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)3 SlidingWindows (org.apache.beam.sdk.transforms.windowing.SlidingWindows)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 XMLGregorianCalendar (javax.xml.datatype.XMLGregorianCalendar)2 KV (org.apache.beam.sdk.values.KV)2 TimestampedValue (org.apache.beam.sdk.values.TimestampedValue)2 Duration (org.joda.time.Duration)2 Category (org.junit.experimental.categories.Category)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1