Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve support for joda datetime to java datetime in Painless #83099

Merged
merged 7 commits into from
Jan 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add augmented methods to suggest how to fix scripts still attempting to
use joda
  • Loading branch information
jdconrad committed Jan 25, 2022
commit 6852ae8a90d6c9cc6eb3945c05870b13f249999a
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,106 @@ 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();
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should have a separate file or section these Augmentations that's simply unsupported.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a good idea. I would want to do that in as a follow up, though.

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

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

public static int getHourOfDay(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getHourOfDay] is no longer available; " + "use [<ZonedDateTime instance>.getHour()] instead"
);
}

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

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

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

public static int getMinuteOfHour(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getMinuteOfHour] is no longer available; " + "use [<ZonedDateTime instance>.getMinute()] instead"
);
}

public static int getMonthOfYear(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getMonthOfYear] is no longer available; " + "use [<ZonedDateTime instance>.getMonthValue()] instead"
);
}

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

public static int getSecondOfMinute(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getSecondOfMinute] is no longer available; " + "use [<ZonedDateTime instance>.getSecond()] instead"
);
}

public static int getWeekOfWeekyear(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getWeekOfWeekyear] is no longer available; "
+ "use [<ZonedDateTime instance>.get(DateFormatters.WEEK_FIELDS.weekOfWeekBasedYear())] instead"
);
}

public static int getWeekyear(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getWeekyear] is no longer available; "
+ "use [<ZonedDateTime instance>.get(DateFormatters.WEEK_FIELDS.weekBasedYear())] instead"
);
}

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

public static int getYearOfEra(ZonedDateTime receiver) {
throw new UnsupportedOperationException(
"joda method [getYearOfEra] is no longer available; " + "use [<ZonedDateTime instance>.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
Original file line number Diff line number Diff line change
Expand Up @@ -1696,3 +1696,19 @@ setup:
- match: { hits.hits.1.fields.f_list.0: "dne" }
- match: { hits.hits.1.fields.f_list2.0: "789" }
- match: { hits.hits.1.fields.all.0: "10111213789876deflmnrstwyz" }

---
"unsupported date methods":
- skip:
features: "warnings"

- do:
search:
rest_total_hits_as_int: true
body:
query: { term: { _id: 1 } }
script_fields:
field:
script:
source: "doc.date.get(0)"
- match: { hits.hits.0.fields.field.0: '2017-01-01T12:11:12.000Z' }