Skip to content

Commit ab7f002

Browse files
authored
Improve support for joda datetime to java datetime transition in Painless (#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.
1 parent 43052a1 commit ab7f002

File tree

5 files changed

+537
-3
lines changed

5 files changed

+537
-3
lines changed

docs/changelog/83099.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 83099
2+
summary: Improve support for joda datetime to java datetime in Painless
3+
area: Infra/Scripting
4+
type: enhancement
5+
issues: []

modules/lang-painless/src/main/java/org/elasticsearch/painless/api/Augmentation.java

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,11 +708,83 @@ public static Matcher matcher(Pattern receiver, int limitFactor, CharSequence in
708708
/**
709709
* Convert a {@link TemporalAccessor} into millis since epoch like {@link Instant#toEpochMilli()}.
710710
*/
711-
public static long toEpochMilli(TemporalAccessor v) {
712-
return v.getLong(ChronoField.INSTANT_SECONDS) * 1_000 + v.get(ChronoField.NANO_OF_SECOND) / 1_000_000;
711+
public static long toEpochMilli(TemporalAccessor receiver) {
712+
return receiver.getLong(ChronoField.INSTANT_SECONDS) * 1_000 + receiver.get(ChronoField.NANO_OF_SECOND) / 1_000_000;
713+
}
714+
715+
public static long getMillis(TemporalAccessor receiver) {
716+
return toEpochMilli(receiver);
713717
}
714718

715719
public static DayOfWeek getDayOfWeekEnum(ZonedDateTime receiver) {
716720
return receiver.getDayOfWeek();
717721
}
722+
723+
public static int getCenturyOfEra(ZonedDateTime receiver) {
724+
throw new UnsupportedOperationException(
725+
"[getCenturyOfEra] is no longer available; " + "use [get(ChronoField.YEAR_OF_ERA) / 100] instead"
726+
);
727+
}
728+
729+
public static int getEra(ZonedDateTime receiver) {
730+
throw new UnsupportedOperationException("[getEra] is no longer available; use [get(ChronoField.ERA)] instead");
731+
}
732+
733+
public static int getHourOfDay(ZonedDateTime receiver) {
734+
throw new UnsupportedOperationException("[getHourOfDay] is no longer available; use [getHour()] instead");
735+
}
736+
737+
public static int getMillisOfDay(ZonedDateTime receiver) {
738+
throw new UnsupportedOperationException("[getMillisOfDay] is no longer available; use [get(ChronoField.MILLI_OF_DAY)] instead");
739+
}
740+
741+
public static int getMillisOfSecond(ZonedDateTime receiver) {
742+
throw new UnsupportedOperationException(
743+
"[getMillisOfSecond] is no longer available; " + "use [get(ChronoField.MILLI_OF_SECOND)] instead"
744+
);
745+
}
746+
747+
public static int getMinuteOfDay(ZonedDateTime receiver) {
748+
throw new UnsupportedOperationException(
749+
"[getMinuteOfDay] is no longer available; " + "use [get(ChronoField.MINUTE_OF_DAY)] instead"
750+
);
751+
}
752+
753+
public static int getMinuteOfHour(ZonedDateTime receiver) {
754+
throw new UnsupportedOperationException("[getMinuteOfHour] is no longer available; use [getMinute()] instead");
755+
}
756+
757+
public static int getMonthOfYear(ZonedDateTime receiver) {
758+
throw new UnsupportedOperationException("[getMonthOfYear] is no longer available; use [getMonthValue()] instead");
759+
}
760+
761+
public static int getSecondOfDay(ZonedDateTime receiver) {
762+
throw new UnsupportedOperationException(
763+
"[getSecondOfDay] is no longer available; " + "use [get(ChronoField.SECOND_OF_DAY)] instead"
764+
);
765+
}
766+
767+
public static int getSecondOfMinute(ZonedDateTime receiver) {
768+
throw new UnsupportedOperationException("[getSecondOfMinute] is no longer available; use [getSecond()] instead");
769+
}
770+
771+
public static int getWeekOfWeekyear(ZonedDateTime receiver) {
772+
throw new UnsupportedOperationException(
773+
"[getWeekOfWeekyear] is no longer available; " + "use [get(IsoFields.WEEK_OF_WEEK_BASED_YEAR)] instead"
774+
);
775+
}
776+
777+
public static int getWeekyear(ZonedDateTime receiver) {
778+
throw new UnsupportedOperationException("[getWeekyear] is no longer available; use [get(IsoFields.WEEK_BASED_YEAR)] instead");
779+
}
780+
781+
public static int getYearOfCentury(ZonedDateTime receiver) {
782+
throw new UnsupportedOperationException(
783+
"[getYearOfCentury] is no longer available; " + "use [get(ChronoField.YEAR_OF_ERA) % 100] instead"
784+
);
785+
}
786+
787+
public static int getYearOfEra(ZonedDateTime receiver) {
788+
throw new UnsupportedOperationException("[getYearOfEra] is no longer available; use [get(ChronoField.YEAR_OF_ERA)] instead");
789+
}
718790
}

modules/lang-painless/src/main/resources/org/elasticsearch/painless/java.time.temporal.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ class java.time.temporal.TemporalAccessor {
2929
boolean isSupported(TemporalField)
3030
def query(TemporalQuery)
3131
ValueRange range(TemporalField)
32-
# An easy method to convert temporalAccessors to millis since epoch similar to Instan#toEpochMilli.
32+
# An easy method to convert temporalAccessors to millis since epoch similar to Instant#toEpochMilli.
3333
long org.elasticsearch.painless.api.Augmentation toEpochMilli()
34+
long org.elasticsearch.painless.api.Augmentation getMillis()
3435
}
3536

3637
class java.time.temporal.TemporalAdjuster {

modules/lang-painless/src/main/resources/org/elasticsearch/painless/java.time.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,20 @@ class java.time.ZonedDateTime {
475475
int getDayOfMonth()
476476
DayOfWeek getDayOfWeek()
477477
DayOfWeek org.elasticsearch.painless.api.Augmentation getDayOfWeekEnum()
478+
int org.elasticsearch.painless.api.Augmentation getCenturyOfEra()
479+
int org.elasticsearch.painless.api.Augmentation getEra()
480+
int org.elasticsearch.painless.api.Augmentation getHourOfDay()
481+
int org.elasticsearch.painless.api.Augmentation getMillisOfDay()
482+
int org.elasticsearch.painless.api.Augmentation getMillisOfSecond()
483+
int org.elasticsearch.painless.api.Augmentation getMinuteOfDay()
484+
int org.elasticsearch.painless.api.Augmentation getMinuteOfHour()
485+
int org.elasticsearch.painless.api.Augmentation getMonthOfYear()
486+
int org.elasticsearch.painless.api.Augmentation getSecondOfDay()
487+
int org.elasticsearch.painless.api.Augmentation getSecondOfMinute()
488+
int org.elasticsearch.painless.api.Augmentation getWeekOfWeekyear()
489+
int org.elasticsearch.painless.api.Augmentation getWeekyear()
490+
int org.elasticsearch.painless.api.Augmentation getYearOfCentury()
491+
int org.elasticsearch.painless.api.Augmentation getYearOfEra()
478492
int getDayOfYear()
479493
int getHour()
480494
LocalDate toLocalDate()

0 commit comments

Comments
 (0)