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;
}
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();
}
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"));
}
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")));
}
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);
}
Aggregations