Skip to content

Commit

Permalink
Improve support for joda datetime to java datetime transition in Pain…
Browse files Browse the repository at this point in the history
…less (elastic#83099)

Within Painless, this change augments TemporalAccessor with the method getMillis to make 
milliseconds easier to access directly from a datetime within a document.

This change also adds several methods from Joda datetimes augmented onto ZonedDateTime that 
throw an UnsupportedOperationException with a suggestion of how to implement that Joda method 
using ZonedDateTime instead.
  • Loading branch information
jdconrad authored Jan 27, 2022
1 parent 43052a1 commit ab7f002
Show file tree
Hide file tree
Showing 5 changed files with 537 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/83099.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 83099
summary: Improve support for joda datetime to java datetime in Painless
area: Infra/Scripting
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,83 @@ public static Matcher matcher(Pattern receiver, int limitFactor, CharSequence in
/**
* Convert a {@link TemporalAccessor} into millis since epoch like {@link Instant#toEpochMilli()}.
*/
public static long toEpochMilli(TemporalAccessor v) {
return v.getLong(ChronoField.INSTANT_SECONDS) * 1_000 + v.get(ChronoField.NANO_OF_SECOND) / 1_000_000;
public static long toEpochMilli(TemporalAccessor receiver) {
return receiver.getLong(ChronoField.INSTANT_SECONDS) * 1_000 + receiver.get(ChronoField.NANO_OF_SECOND) / 1_000_000;
}

public static long getMillis(TemporalAccessor receiver) {
return toEpochMilli(receiver);
}

public static DayOfWeek getDayOfWeekEnum(ZonedDateTime receiver) {
return receiver.getDayOfWeek();
}

public static int getCenturyOfEra(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getCenturyOfEra] is no longer available; " + "use [get(ChronoField.YEAR_OF_ERA) / 100] instead"
);
}

public static int getEra(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getEra] is no longer available; use [get(ChronoField.ERA)] instead");
}

public static int getHourOfDay(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getHourOfDay] is no longer available; use [getHour()] instead");
}

public static int getMillisOfDay(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getMillisOfDay] is no longer available; use [get(ChronoField.MILLI_OF_DAY)] instead");
}

public static int getMillisOfSecond(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getMillisOfSecond] is no longer available; " + "use [get(ChronoField.MILLI_OF_SECOND)] instead"
);
}

public static int getMinuteOfDay(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getMinuteOfDay] is no longer available; " + "use [get(ChronoField.MINUTE_OF_DAY)] instead"
);
}

public static int getMinuteOfHour(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getMinuteOfHour] is no longer available; use [getMinute()] instead");
}

public static int getMonthOfYear(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getMonthOfYear] is no longer available; use [getMonthValue()] instead");
}

public static int getSecondOfDay(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getSecondOfDay] is no longer available; " + "use [get(ChronoField.SECOND_OF_DAY)] instead"
);
}

public static int getSecondOfMinute(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getSecondOfMinute] is no longer available; use [getSecond()] instead");
}

public static int getWeekOfWeekyear(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getWeekOfWeekyear] is no longer available; " + "use [get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)] instead"
);
}

public static int getWeekyear(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getWeekyear] is no longer available; use [get(IsoFields.WEEK_BASED_YEAR)] instead");
}

public static int getYearOfCentury(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"[getYearOfCentury] is no longer available; " + "use [get(ChronoField.YEAR_OF_ERA) % 100] instead"
);
}

public static int getYearOfEra(ZonedDateTime receiver) {
throw new UnsupportedOperationException("[getYearOfEra] is no longer available; use [get(ChronoField.YEAR_OF_ERA)] instead");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class java.time.temporal.TemporalAccessor {
boolean isSupported(TemporalField)
def query(TemporalQuery)
ValueRange range(TemporalField)
# An easy method to convert temporalAccessors to millis since epoch similar to Instan#toEpochMilli.
# An easy method to convert temporalAccessors to millis since epoch similar to Instant#toEpochMilli.
long org.elasticsearch.painless.api.Augmentation toEpochMilli()
long org.elasticsearch.painless.api.Augmentation getMillis()
}

class java.time.temporal.TemporalAdjuster {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,20 @@ class java.time.ZonedDateTime {
int getDayOfMonth()
DayOfWeek getDayOfWeek()
DayOfWeek org.elasticsearch.painless.api.Augmentation getDayOfWeekEnum()
int org.elasticsearch.painless.api.Augmentation getCenturyOfEra()
int org.elasticsearch.painless.api.Augmentation getEra()
int org.elasticsearch.painless.api.Augmentation getHourOfDay()
int org.elasticsearch.painless.api.Augmentation getMillisOfDay()
int org.elasticsearch.painless.api.Augmentation getMillisOfSecond()
int org.elasticsearch.painless.api.Augmentation getMinuteOfDay()
int org.elasticsearch.painless.api.Augmentation getMinuteOfHour()
int org.elasticsearch.painless.api.Augmentation getMonthOfYear()
int org.elasticsearch.painless.api.Augmentation getSecondOfDay()
int org.elasticsearch.painless.api.Augmentation getSecondOfMinute()
int org.elasticsearch.painless.api.Augmentation getWeekOfWeekyear()
int org.elasticsearch.painless.api.Augmentation getWeekyear()
int org.elasticsearch.painless.api.Augmentation getYearOfCentury()
int org.elasticsearch.painless.api.Augmentation getYearOfEra()
int getDayOfYear()
int getHour()
LocalDate toLocalDate()
Expand Down
Loading

0 comments on commit ab7f002

Please sign in to comment.