From 1ffd7eec2b169efc00c7e07baf7da213a1768608 Mon Sep 17 00:00:00 2001 From: Nick Knize Date: Wed, 16 Aug 2023 18:25:07 -0500 Subject: [PATCH] [Remove] JodaCompatibleZonedDateTime (#9355) This commit removes the JodaCompatibleZonedDateTime class and tests in favor of Java time. Signed-off-by: Nicholas Walter Knize Signed-off-by: Kaushal Kumar --- .../common/io/stream/BaseStreamTests.java | 6 +- .../opensearch/painless/AnalyzerCaster.java | 10 - .../java/org/opensearch/painless/Def.java | 11 - .../org/opensearch/painless/MethodWriter.java | 11 - .../java/org/opensearch/painless/Utility.java | 9 - .../opensearch/painless/WriterConstants.java | 12 - .../opensearch/painless/api/Augmentation.java | 6 + .../org/opensearch/painless/spi/java.time.txt | 1 + .../painless/spi/org.opensearch.score.txt | 6 +- .../painless/spi/org.opensearch.txt | 57 +-- .../opensearch/painless/BasicAPITests.java | 13 - .../org/opensearch/painless/DefCastTests.java | 16 - .../painless/spi/org.opensearch.painless.test | 5 - .../common/io/stream/Streamables.java | 16 +- .../xcontent/XContentOpenSearchExtension.java | 6 - .../index/fielddata/ScriptDocValues.java | 16 +- .../script/JodaCompatibleZonedDateTime.java | 407 ------------------ .../opensearch/script/ScoreScriptUtils.java | 7 +- .../support/values/ScriptDoubleValues.java | 3 - .../support/values/ScriptLongValues.java | 3 - .../common/io/stream/BytesStreamsTests.java | 10 +- .../JodaCompatibleZonedDateTimeTests.java | 220 ---------- 22 files changed, 35 insertions(+), 816 deletions(-) delete mode 100644 server/src/main/java/org/opensearch/script/JodaCompatibleZonedDateTime.java delete mode 100644 server/src/test/java/org/opensearch/script/JodaCompatibleZonedDateTimeTests.java diff --git a/libs/core/src/test/java/org/opensearch/core/common/io/stream/BaseStreamTests.java b/libs/core/src/test/java/org/opensearch/core/common/io/stream/BaseStreamTests.java index 5da8f58f688a5..646acefc09c48 100644 --- a/libs/core/src/test/java/org/opensearch/core/common/io/stream/BaseStreamTests.java +++ b/libs/core/src/test/java/org/opensearch/core/common/io/stream/BaseStreamTests.java @@ -41,7 +41,6 @@ import org.opensearch.core.common.bytes.BytesArray; import org.opensearch.core.common.bytes.BytesReference; import org.opensearch.core.common.settings.SecureString; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.opensearch.test.OpenSearchTestCase; import java.io.ByteArrayInputStream; @@ -49,6 +48,7 @@ import java.io.IOException; import java.time.Instant; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -400,10 +400,10 @@ public void testOptionalInstantSerialization() throws IOException { } } - public void testJodaDateTimeSerialization() throws IOException { + public void testJavaDateTimeSerialization() throws IOException { final BytesStreamOutput output = new BytesStreamOutput(); long millis = randomIntBetween(0, Integer.MAX_VALUE); - JodaCompatibleZonedDateTime time = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); + ZonedDateTime time = ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); output.writeGenericValue(time); final BytesReference bytesReference = output.bytes(); diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/AnalyzerCaster.java b/modules/lang-painless/src/main/java/org/opensearch/painless/AnalyzerCaster.java index d830ef2ab6290..9aae14e0a4a52 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/AnalyzerCaster.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/AnalyzerCaster.java @@ -35,9 +35,7 @@ import org.opensearch.painless.lookup.PainlessCast; import org.opensearch.painless.lookup.PainlessLookupUtility; import org.opensearch.painless.lookup.def; -import org.opensearch.script.JodaCompatibleZonedDateTime; -import java.time.ZonedDateTime; import java.util.Objects; /** @@ -87,19 +85,11 @@ public static PainlessCast getLegalCast(Location location, Class actual, Clas return PainlessCast.originalTypetoTargetType(def.class, Float.class, explicit); } else if (expected == Double.class) { return PainlessCast.originalTypetoTargetType(def.class, Double.class, explicit); - // TODO: remove this when the transition from Joda to Java datetimes is completed - } else if (expected == ZonedDateTime.class) { - return PainlessCast.originalTypetoTargetType(def.class, ZonedDateTime.class, explicit); } } else if (actual == String.class) { if (expected == char.class && explicit) { return PainlessCast.originalTypetoTargetType(String.class, char.class, true); } - // TODO: remove this when the transition from Joda to Java datetimes is completed - } else if (actual == JodaCompatibleZonedDateTime.class) { - if (expected == ZonedDateTime.class) { - return PainlessCast.originalTypetoTargetType(JodaCompatibleZonedDateTime.class, ZonedDateTime.class, explicit); - } } else if (actual == boolean.class) { if (expected == def.class) { return PainlessCast.boxOriginalType(Boolean.class, def.class, explicit, boolean.class); diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/Def.java b/modules/lang-painless/src/main/java/org/opensearch/painless/Def.java index de6fd5ebc0177..3320686466762 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/Def.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/Def.java @@ -36,13 +36,11 @@ import org.opensearch.painless.lookup.PainlessLookupUtility; import org.opensearch.painless.lookup.PainlessMethod; import org.opensearch.painless.symbol.FunctionTable; -import org.opensearch.script.JodaCompatibleZonedDateTime; import java.lang.invoke.CallSite; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import java.time.ZonedDateTime; import java.util.BitSet; import java.util.Collections; import java.util.Iterator; @@ -1506,15 +1504,6 @@ public static String defToStringExplicit(final Object value) { } } - // TODO: remove this when the transition from Joda to Java datetimes is completed - public static ZonedDateTime defToZonedDateTime(final Object value) { - if (value instanceof JodaCompatibleZonedDateTime) { - return ((JodaCompatibleZonedDateTime) value).getZonedDateTime(); - } - - return (ZonedDateTime) value; - } - /** * "Normalizes" the index into a {@code Map} by making no change to the index. */ diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/MethodWriter.java b/modules/lang-painless/src/main/java/org/opensearch/painless/MethodWriter.java index 028acdb7d87c9..5ea09e59591f1 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/MethodWriter.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/MethodWriter.java @@ -35,7 +35,6 @@ import org.opensearch.painless.lookup.PainlessCast; import org.opensearch.painless.lookup.PainlessMethod; import org.opensearch.painless.lookup.def; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.objectweb.asm.ClassVisitor; import org.objectweb.asm.Label; import org.objectweb.asm.Opcodes; @@ -44,7 +43,6 @@ import org.objectweb.asm.commons.Method; import java.lang.reflect.Modifier; -import java.time.ZonedDateTime; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Arrays; @@ -86,10 +84,8 @@ import static org.opensearch.painless.WriterConstants.DEF_TO_P_SHORT_IMPLICIT; import static org.opensearch.painless.WriterConstants.DEF_TO_STRING_EXPLICIT; import static org.opensearch.painless.WriterConstants.DEF_TO_STRING_IMPLICIT; -import static org.opensearch.painless.WriterConstants.DEF_TO_ZONEDDATETIME; import static org.opensearch.painless.WriterConstants.DEF_UTIL_TYPE; import static org.opensearch.painless.WriterConstants.INDY_STRING_CONCAT_BOOTSTRAP_HANDLE; -import static org.opensearch.painless.WriterConstants.JCZDT_TO_ZONEDDATETIME; import static org.opensearch.painless.WriterConstants.LAMBDA_BOOTSTRAP_HANDLE; import static org.opensearch.painless.WriterConstants.MAX_INDY_STRING_CONCAT_ARGS; import static org.opensearch.painless.WriterConstants.PAINLESS_ERROR_TYPE; @@ -181,9 +177,6 @@ public void writeCast(PainlessCast cast) { invokeStatic(UTILITY_TYPE, CHAR_TO_STRING); } else if (cast.originalType == String.class && cast.targetType == char.class) { invokeStatic(UTILITY_TYPE, STRING_TO_CHAR); - // TODO: remove this when the transition from Joda to Java datetimes is completed - } else if (cast.originalType == JodaCompatibleZonedDateTime.class && cast.targetType == ZonedDateTime.class) { - invokeStatic(UTILITY_TYPE, JCZDT_TO_ZONEDDATETIME); } else if (cast.unboxOriginalType != null && cast.boxTargetType != null) { unbox(getType(cast.unboxOriginalType)); writeCast(cast.unboxOriginalType, cast.boxTargetType); @@ -219,8 +212,6 @@ public void writeCast(PainlessCast cast) { else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_EXPLICIT); else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_EXPLICIT); else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_EXPLICIT); - // TODO: remove this when the transition from Joda to Java datetimes is completed - else if (cast.targetType == ZonedDateTime.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_ZONEDDATETIME); else { writeCast(cast.originalType, cast.targetType); } @@ -242,8 +233,6 @@ public void writeCast(PainlessCast cast) { else if (cast.targetType == Float.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_FLOAT_IMPLICIT); else if (cast.targetType == Double.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_B_DOUBLE_IMPLICIT); else if (cast.targetType == String.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_STRING_IMPLICIT); - // TODO: remove this when the transition from Joda to Java datetimes is completed - else if (cast.targetType == ZonedDateTime.class) invokeStatic(DEF_UTIL_TYPE, DEF_TO_ZONEDDATETIME); else { writeCast(cast.originalType, cast.targetType); } diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/Utility.java b/modules/lang-painless/src/main/java/org/opensearch/painless/Utility.java index c9a9419fd4821..1f6f203032933 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/Utility.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/Utility.java @@ -32,10 +32,6 @@ package org.opensearch.painless; -import org.opensearch.script.JodaCompatibleZonedDateTime; - -import java.time.ZonedDateTime; - /** * A set of methods for non-native boxing and non-native * exact math operations used at both compile-time and runtime. @@ -62,10 +58,5 @@ public static char StringTochar(final String value) { return value.charAt(0); } - // TODO: remove this when the transition from Joda to Java datetimes is completed - public static ZonedDateTime JCZDTToZonedDateTime(final JodaCompatibleZonedDateTime jczdt) { - return jczdt.getZonedDateTime(); - } - private Utility() {} } diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/WriterConstants.java b/modules/lang-painless/src/main/java/org/opensearch/painless/WriterConstants.java index 530b4d2607931..1dbcde91a7df4 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/WriterConstants.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/WriterConstants.java @@ -32,7 +32,6 @@ package org.opensearch.painless; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.objectweb.asm.Handle; import org.objectweb.asm.Opcodes; import org.objectweb.asm.Type; @@ -42,7 +41,6 @@ import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; import java.lang.invoke.MethodType; -import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -82,13 +80,6 @@ public final class WriterConstants { public static final Method STRING_TO_CHAR = getAsmMethod(char.class, "StringTochar", String.class); public static final Method CHAR_TO_STRING = getAsmMethod(String.class, "charToString", char.class); - // TODO: remove this when the transition from Joda to Java datetimes is completed - public static final Method JCZDT_TO_ZONEDDATETIME = getAsmMethod( - ZonedDateTime.class, - "JCZDTToZonedDateTime", - JodaCompatibleZonedDateTime.class - ); - /** * A Method instance for {@linkplain Pattern}. This isn't available from PainlessLookup because we intentionally don't add it * there so that the script can't create regexes without this syntax. Essentially, our static regex syntax has a monopoly on building @@ -157,9 +148,6 @@ public final class WriterConstants { public static final Method DEF_TO_STRING_IMPLICIT = getAsmMethod(String.class, "defToStringImplicit", Object.class); public static final Method DEF_TO_STRING_EXPLICIT = getAsmMethod(String.class, "defToStringExplicit", Object.class); - // TODO: remove this when the transition from Joda to Java datetimes is completed - public static final Method DEF_TO_ZONEDDATETIME = getAsmMethod(ZonedDateTime.class, "defToZonedDateTime", Object.class); - /** invokedynamic bootstrap for lambda expression/method references */ public static final MethodType LAMBDA_BOOTSTRAP_TYPE = MethodType.methodType( CallSite.class, diff --git a/modules/lang-painless/src/main/java/org/opensearch/painless/api/Augmentation.java b/modules/lang-painless/src/main/java/org/opensearch/painless/api/Augmentation.java index 821fbc45c42e3..5a105e8c03e02 100644 --- a/modules/lang-painless/src/main/java/org/opensearch/painless/api/Augmentation.java +++ b/modules/lang-painless/src/main/java/org/opensearch/painless/api/Augmentation.java @@ -35,6 +35,8 @@ import org.opensearch.common.hash.MessageDigests; import java.nio.charset.StandardCharsets; +import java.time.DayOfWeek; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Base64; import java.util.Collection; @@ -721,4 +723,8 @@ public static Matcher matcher(Pattern receiver, int limitFactor, CharSequence in } return receiver.matcher(new LimitedCharSequence(input, receiver, limitFactor)); } + + public static DayOfWeek getDayOfWeekEnum(ZonedDateTime receiver) { + return receiver.getDayOfWeek(); + } } diff --git a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/java.time.txt b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/java.time.txt index 38c6e8a4f575e..6a97fd4038e94 100644 --- a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/java.time.txt +++ b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/java.time.txt @@ -485,6 +485,7 @@ class java.time.YearMonth { class java.time.ZonedDateTime { int getDayOfMonth() DayOfWeek getDayOfWeek() + DayOfWeek org.opensearch.painless.api.Augmentation getDayOfWeekEnum() int getDayOfYear() int getHour() LocalDate toLocalDate() diff --git a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.score.txt b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.score.txt index cca7e07a95388..61d53608a30c8 100644 --- a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.score.txt +++ b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.score.txt @@ -33,8 +33,8 @@ static_import { double decayNumericLinear(double, double, double, double, double)bound_to org.opensearch.script.ScoreScriptUtils$DecayNumericLinear double decayNumericExp(double, double, double, double, double) bound_to org.opensearch.script.ScoreScriptUtils$DecayNumericExp double decayNumericGauss(double, double, double, double, double) bound_to org.opensearch.script.ScoreScriptUtils$DecayNumericGauss - double decayDateLinear(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateLinear - double decayDateExp(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateExp - double decayDateGauss(String, String, String, double, JodaCompatibleZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateGauss + double decayDateLinear(String, String, String, double, ZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateLinear + double decayDateExp(String, String, String, double, ZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateExp + double decayDateGauss(String, String, String, double, ZonedDateTime) bound_to org.opensearch.script.ScoreScriptUtils$DecayDateGauss } diff --git a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.txt b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.txt index dccf29b629ce8..b91d9bb6115d4 100644 --- a/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.txt +++ b/modules/lang-painless/src/main/resources/org/opensearch/painless/spi/org.opensearch.txt @@ -79,62 +79,9 @@ class org.opensearch.index.fielddata.ScriptDocValues$UnsignedLongs { BigInteger getValue() } -class org.opensearch.script.JodaCompatibleZonedDateTime { - ##### ZonedDateTime methods - int getDayOfMonth() - int getDayOfYear() - int getHour() - LocalDate toLocalDate() - LocalDateTime toLocalDateTime() - int getMinute() - Month getMonth() - int getMonthValue() - int getNano() - int getSecond() - int getYear() - ZonedDateTime minus(TemporalAmount) - ZonedDateTime minus(long,TemporalUnit) - ZonedDateTime minusYears(long) - ZonedDateTime minusMonths(long) - ZonedDateTime minusWeeks(long) - ZonedDateTime minusDays(long) - ZonedDateTime minusHours(long) - ZonedDateTime minusMinutes(long) - ZonedDateTime minusSeconds(long) - ZonedDateTime minusNanos(long) - ZonedDateTime plus(TemporalAmount) - ZonedDateTime plus(long,TemporalUnit) - ZonedDateTime plusDays(long) - ZonedDateTime plusHours(long) - ZonedDateTime plusMinutes(long) - ZonedDateTime plusMonths(long) - ZonedDateTime plusNanos(long) - ZonedDateTime plusSeconds(long) - ZonedDateTime plusWeeks(long) - ZonedDateTime plusYears(long) - OffsetDateTime toOffsetDateTime() - ZonedDateTime truncatedTo(TemporalUnit) - ZonedDateTime with(TemporalAdjuster) - ZonedDateTime with(TemporalField,long) - ZonedDateTime withDayOfMonth(int) - ZonedDateTime withDayOfYear(int) - ZonedDateTime withEarlierOffsetAtOverlap() - ZonedDateTime withFixedOffsetZone() - ZonedDateTime withHour(int) - ZonedDateTime withLaterOffsetAtOverlap() - ZonedDateTime withMinute(int) - ZonedDateTime withMonth(int) - ZonedDateTime withNano(int) - ZonedDateTime withSecond(int) - ZonedDateTime withYear(int) - ZonedDateTime withZoneSameLocal(ZoneId) - ZonedDateTime withZoneSameInstant(ZoneId) - DayOfWeek getDayOfWeekEnum() -} - class org.opensearch.index.fielddata.ScriptDocValues$Dates { - JodaCompatibleZonedDateTime get(int) - JodaCompatibleZonedDateTime getValue() + ZonedDateTime get(int) + ZonedDateTime getValue() } class org.opensearch.index.fielddata.ScriptDocValues$Doubles { diff --git a/modules/lang-painless/src/test/java/org/opensearch/painless/BasicAPITests.java b/modules/lang-painless/src/test/java/org/opensearch/painless/BasicAPITests.java index c4cd7503bbb2e..31c2525029d06 100644 --- a/modules/lang-painless/src/test/java/org/opensearch/painless/BasicAPITests.java +++ b/modules/lang-painless/src/test/java/org/opensearch/painless/BasicAPITests.java @@ -199,19 +199,6 @@ public void testStatic() { assertEquals(15.5f, exec("staticAddFloatsTest(6.5f, 9.0f)")); } - // TODO: remove this when the transition from Joda to Java datetimes is completed - public void testJCZDTToZonedDateTime() { - assertEquals( - 0L, - exec( - "Instant instant = Instant.ofEpochMilli(434931330000L);" - + "JodaCompatibleZonedDateTime d = new JodaCompatibleZonedDateTime(instant, ZoneId.of('Z'));" - + "ZonedDateTime t = d;" - + "return ChronoUnit.MILLIS.between(d, t);" - ) - ); - } - public void testRandomUUID() { assertTrue( Pattern.compile("\\p{XDigit}{8}(-\\p{XDigit}{4}){3}-\\p{XDigit}{12}") diff --git a/modules/lang-painless/src/test/java/org/opensearch/painless/DefCastTests.java b/modules/lang-painless/src/test/java/org/opensearch/painless/DefCastTests.java index df8ffd2be4437..a267ef1701e2c 100644 --- a/modules/lang-painless/src/test/java/org/opensearch/painless/DefCastTests.java +++ b/modules/lang-painless/src/test/java/org/opensearch/painless/DefCastTests.java @@ -709,20 +709,4 @@ public void testConstFoldingDefCast() { assertFalse((boolean) exec("def chr = (char)10L; return (chr > (byte)10);")); assertFalse((boolean) exec("def chr = (char)10L; return (chr > (double)(byte)(char)10);")); } - - // TODO: remove this when the transition from Joda to Java datetimes is completed - public void testdefToZonedDateTime() { - assertEquals( - 0L, - exec( - "Instant instant = Instant.ofEpochMilli(434931330000L);" - + "def d = new JodaCompatibleZonedDateTime(instant, ZoneId.of('Z'));" - + "def x = new HashMap(); x.put('dt', d);" - + "ZonedDateTime t = x['dt'];" - + "def y = t;" - + "t = y;" - + "return ChronoUnit.MILLIS.between(d, t);" - ) - ); - } } diff --git a/modules/lang-painless/src/test/resources/org/opensearch/painless/spi/org.opensearch.painless.test b/modules/lang-painless/src/test/resources/org/opensearch/painless/spi/org.opensearch.painless.test index 5345f7fab8794..cb4cd70c809b1 100644 --- a/modules/lang-painless/src/test/resources/org/opensearch/painless/spi/org.opensearch.painless.test +++ b/modules/lang-painless/src/test/resources/org/opensearch/painless/spi/org.opensearch.painless.test @@ -1,10 +1,5 @@ # allowlist for tests -# TODO: remove this when the transition from Joda to Java datetimes is completed -class org.opensearch.script.JodaCompatibleZonedDateTime { - (Instant, ZoneId) -} - # for unit tests only class org.opensearch.painless.api.Json { def load(String) diff --git a/server/src/main/java/org/opensearch/common/io/stream/Streamables.java b/server/src/main/java/org/opensearch/common/io/stream/Streamables.java index 1e5f8164e8fb9..c6a03cf0475b7 100644 --- a/server/src/main/java/org/opensearch/common/io/stream/Streamables.java +++ b/server/src/main/java/org/opensearch/common/io/stream/Streamables.java @@ -13,12 +13,12 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable.WriteableRegistry; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.joda.time.DateTimeZone; import org.joda.time.ReadableInstant; import java.time.Instant; import java.time.ZoneId; +import java.time.ZonedDateTime; /** * This utility class registers generic types for streaming over the wire using @@ -55,16 +55,6 @@ private static void registerWriters() { o.writeLong(instant.getMillis()); }); WriteableRegistry.registerClassAlias(ReadableInstant.class, ReadableInstant.class); - /** {@link JodaCompatibleZonedDateTime} */ - WriteableRegistry.registerWriter(JodaCompatibleZonedDateTime.class, (o, v) -> { - // write the joda compatibility datetime as joda datetime - o.writeByte((byte) 13); - final JodaCompatibleZonedDateTime zonedDateTime = (JodaCompatibleZonedDateTime) v; - String zoneId = zonedDateTime.getZonedDateTime().getZone().getId(); - // joda does not understand "Z" for utc, so we must special case - o.writeString(zoneId.equals("Z") ? DateTimeZone.UTC.getID() : zoneId); - o.writeLong(zonedDateTime.toInstant().toEpochMilli()); - }); /** {@link GeoPoint} */ WriteableRegistry.registerWriter(GeoPoint.class, (o, v) -> { o.writeByte((byte) 22); @@ -78,11 +68,11 @@ private static void registerWriters() { * NOTE: see {@code StreamOutput#WRITERS} for all registered ordinals */ private static void registerReaders() { - /** {@link JodaCompatibleZonedDateTime */ + /** {@link ZonedDateTime} */ WriteableRegistry.registerReader(Byte.valueOf((byte) 13), (i) -> { final ZoneId zoneId = DateUtils.dateTimeZoneToZoneId(DateTimeZone.forID(i.readString())); long millis = i.readLong(); - return new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), zoneId); + return ZonedDateTime.ofInstant(Instant.ofEpochMilli(millis), zoneId); }); /** {@link GeoPoint} */ WriteableRegistry.registerReader(Byte.valueOf((byte) 22), GeoPoint::new); diff --git a/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java b/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java index a02d338f1ddcb..17de403fbc157 100644 --- a/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java +++ b/server/src/main/java/org/opensearch/common/xcontent/XContentOpenSearchExtension.java @@ -39,7 +39,6 @@ import org.opensearch.core.common.unit.ByteSizeValue; import org.opensearch.core.xcontent.XContentBuilder; import org.opensearch.core.xcontent.XContentBuilderExtension; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; import org.joda.time.Instant; @@ -110,7 +109,6 @@ public Map, XContentBuilder.Writer> getXContentWriters() { writers.put(Year.class, (b, v) -> b.value(v.toString())); writers.put(Duration.class, (b, v) -> b.value(v.toString())); writers.put(Period.class, (b, v) -> b.value(v.toString())); - writers.put(JodaCompatibleZonedDateTime.class, XContentBuilder::timeValue); writers.put(BytesReference.class, (b, v) -> { if (v == null) { @@ -161,10 +159,6 @@ public Map, Function> getDateTransformers() { ); transformers.put(LocalDate.class, d -> ((LocalDate) d).toString()); transformers.put(LocalTime.class, d -> LOCAL_TIME_FORMATTER.format((LocalTime) d)); - transformers.put( - JodaCompatibleZonedDateTime.class, - d -> DEFAULT_FORMATTER.format(((JodaCompatibleZonedDateTime) d).getZonedDateTime()) - ); return transformers; } } diff --git a/server/src/main/java/org/opensearch/index/fielddata/ScriptDocValues.java b/server/src/main/java/org/opensearch/index/fielddata/ScriptDocValues.java index 1d1524e223f00..a3c3774b250dc 100644 --- a/server/src/main/java/org/opensearch/index/fielddata/ScriptDocValues.java +++ b/server/src/main/java/org/opensearch/index/fielddata/ScriptDocValues.java @@ -41,12 +41,12 @@ import org.opensearch.common.geo.GeoUtils; import org.opensearch.common.time.DateUtils; import org.opensearch.geometry.utils.Geohash; -import org.opensearch.script.JodaCompatibleZonedDateTime; import java.io.IOException; import java.math.BigInteger; import java.time.Instant; import java.time.ZoneOffset; +import java.time.ZonedDateTime; import java.util.AbstractList; import java.util.Arrays; import java.util.Comparator; @@ -159,7 +159,7 @@ public int size() { * * @opensearch.internal */ - public static final class Dates extends ScriptDocValues { + public static final class Dates extends ScriptDocValues { private final SortedNumericDocValues in; private final boolean isNanos; @@ -167,7 +167,7 @@ public static final class Dates extends ScriptDocValues dates.length) { // Happens for the document. We delay allocating dates so we can allocate it with a reasonable size. - dates = new JodaCompatibleZonedDateTime[count]; + dates = new ZonedDateTime[count]; } for (int i = 0; i < count; ++i) { if (isNanos) { - dates[i] = new JodaCompatibleZonedDateTime(DateUtils.toInstant(in.nextValue()), ZoneOffset.UTC); + dates[i] = ZonedDateTime.ofInstant(DateUtils.toInstant(in.nextValue()), ZoneOffset.UTC); } else { - dates[i] = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(in.nextValue()), ZoneOffset.UTC); + dates[i] = ZonedDateTime.ofInstant(Instant.ofEpochMilli(in.nextValue()), ZoneOffset.UTC); } } } diff --git a/server/src/main/java/org/opensearch/script/JodaCompatibleZonedDateTime.java b/server/src/main/java/org/opensearch/script/JodaCompatibleZonedDateTime.java deleted file mode 100644 index 08306b3f275a8..0000000000000 --- a/server/src/main/java/org/opensearch/script/JodaCompatibleZonedDateTime.java +++ /dev/null @@ -1,407 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.script; - -import org.opensearch.common.SuppressForbidden; -import org.opensearch.common.time.DateFormatter; - -import java.time.DayOfWeek; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.Month; -import java.time.OffsetDateTime; -import java.time.ZoneId; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; -import java.time.chrono.ChronoZonedDateTime; -import java.time.chrono.Chronology; -import java.time.format.DateTimeFormatter; -import java.time.temporal.Temporal; -import java.time.temporal.TemporalAccessor; -import java.time.temporal.TemporalAdjuster; -import java.time.temporal.TemporalAmount; -import java.time.temporal.TemporalField; -import java.time.temporal.TemporalQuery; -import java.time.temporal.TemporalUnit; -import java.time.temporal.ValueRange; -import java.util.Objects; - -/** - * A wrapper around ZonedDateTime that exposes joda methods for backcompat. - * - * @opensearch.internal - */ -public class JodaCompatibleZonedDateTime - implements - Comparable>, - ChronoZonedDateTime, - Temporal, - TemporalAccessor { - - private static final DateFormatter DATE_FORMATTER = DateFormatter.forPattern("strict_date_time"); - private ZonedDateTime dt; - - public JodaCompatibleZonedDateTime(Instant instant, ZoneId zone) { - this.dt = ZonedDateTime.ofInstant(instant, zone); - } - - // access the underlying ZonedDateTime - public ZonedDateTime getZonedDateTime() { - return dt; - } - - @Override - public boolean equals(Object o) { - if (this == o) return true; - if (o == null) return false; - if (o.getClass() == JodaCompatibleZonedDateTime.class) { - JodaCompatibleZonedDateTime that = (JodaCompatibleZonedDateTime) o; - return Objects.equals(dt, that.dt); - } else if (o.getClass() == ZonedDateTime.class) { - ZonedDateTime that = (ZonedDateTime) o; - return Objects.equals(dt, that); - } - return false; - } - - @Override - public int hashCode() { - return dt.hashCode(); - } - - @Override - public String toString() { - return DATE_FORMATTER.format(dt); - } - - @Override - public String format(DateTimeFormatter formatter) { - return dt.format(formatter); - } - - @Override - public ValueRange range(TemporalField field) { - return dt.range(field); - } - - @Override - public int get(TemporalField field) { - return dt.get(field); - } - - @Override - public long getLong(TemporalField field) { - return dt.getLong(field); - } - - @Override - public Chronology getChronology() { - return dt.getChronology(); - } - - @Override - public ZoneOffset getOffset() { - return dt.getOffset(); - } - - @Override - public boolean isSupported(TemporalField field) { - return dt.isSupported(field); - } - - @Override - public boolean isSupported(TemporalUnit unit) { - return dt.isSupported(unit); - } - - @Override - public long toEpochSecond() { - return dt.toEpochSecond(); - } - - @Override - public int compareTo(ChronoZonedDateTime other) { - return dt.compareTo(other); - } - - @Override - public boolean isBefore(ChronoZonedDateTime other) { - return dt.isBefore(other); - } - - @Override - public boolean isAfter(ChronoZonedDateTime other) { - return dt.isAfter(other); - } - - @Override - public boolean isEqual(ChronoZonedDateTime other) { - return dt.isEqual(other); - } - - @Override - public LocalTime toLocalTime() { - return dt.toLocalTime(); - } - - public int getDayOfMonth() { - return dt.getDayOfMonth(); - } - - public int getDayOfYear() { - return dt.getDayOfYear(); - } - - public int getHour() { - return dt.getHour(); - } - - @Override - public LocalDate toLocalDate() { - return dt.toLocalDate(); - } - - @Override - public LocalDateTime toLocalDateTime() { - return dt.toLocalDateTime(); - } - - public int getMinute() { - return dt.getMinute(); - } - - public Month getMonth() { - return dt.getMonth(); - } - - public int getMonthValue() { - return dt.getMonthValue(); - } - - public int getNano() { - return dt.getNano(); - } - - public int getSecond() { - return dt.getSecond(); - } - - public int getYear() { - return dt.getYear(); - } - - @Override - public ZoneId getZone() { - return dt.getZone(); - } - - @Override - public ZonedDateTime minus(TemporalAmount delta) { - return dt.minus(delta); - } - - @Override - public ZonedDateTime minus(long amount, TemporalUnit unit) { - return dt.minus(amount, unit); - } - - @Override - public R query(TemporalQuery query) { - return dt.query(query); - } - - @Override - public long until(Temporal temporal, TemporalUnit temporalUnit) { - return dt.until(temporal, temporalUnit); - } - - public ZonedDateTime minusYears(long amount) { - return dt.minusYears(amount); - } - - public ZonedDateTime minusMonths(long amount) { - return dt.minusMonths(amount); - } - - public ZonedDateTime minusWeeks(long amount) { - return dt.minusWeeks(amount); - } - - public ZonedDateTime minusDays(long amount) { - return dt.minusDays(amount); - } - - public ZonedDateTime minusHours(long amount) { - return dt.minusHours(amount); - } - - public ZonedDateTime minusMinutes(long amount) { - return dt.minusMinutes(amount); - } - - public ZonedDateTime minusSeconds(long amount) { - return dt.minusSeconds(amount); - } - - public ZonedDateTime minusNanos(long amount) { - return dt.minusNanos(amount); - } - - @Override - public ZonedDateTime plus(TemporalAmount amount) { - return dt.plus(amount); - } - - @Override - public ZonedDateTime plus(long amount, TemporalUnit unit) { - return dt.plus(amount, unit); - } - - public ZonedDateTime plusDays(long amount) { - return dt.plusDays(amount); - } - - public ZonedDateTime plusHours(long amount) { - return dt.plusHours(amount); - } - - public ZonedDateTime plusMinutes(long amount) { - return dt.plusMinutes(amount); - } - - public ZonedDateTime plusMonths(long amount) { - return dt.plusMonths(amount); - } - - public ZonedDateTime plusNanos(long amount) { - return dt.plusNanos(amount); - } - - public ZonedDateTime plusSeconds(long amount) { - return dt.plusSeconds(amount); - } - - public ZonedDateTime plusWeeks(long amount) { - return dt.plusWeeks(amount); - } - - public ZonedDateTime plusYears(long amount) { - return dt.plusYears(amount); - } - - @Override - public Instant toInstant() { - return dt.toInstant(); - } - - public OffsetDateTime toOffsetDateTime() { - return dt.toOffsetDateTime(); - } - - @SuppressForbidden(reason = "only exposing the method as a passthrough") - public ZonedDateTime truncatedTo(TemporalUnit unit) { - return dt.truncatedTo(unit); - } - - @Override - public ZonedDateTime with(TemporalAdjuster adjuster) { - return dt.with(adjuster); - } - - @Override - public ZonedDateTime with(TemporalField field, long newValue) { - return dt.with(field, newValue); - } - - public ZonedDateTime withDayOfMonth(int value) { - return dt.withDayOfMonth(value); - } - - public ZonedDateTime withDayOfYear(int value) { - return dt.withDayOfYear(value); - } - - @Override - public ZonedDateTime withEarlierOffsetAtOverlap() { - return dt.withEarlierOffsetAtOverlap(); - } - - public ZonedDateTime withFixedOffsetZone() { - return dt.withFixedOffsetZone(); - } - - public ZonedDateTime withHour(int value) { - return dt.withHour(value); - } - - @Override - public ZonedDateTime withLaterOffsetAtOverlap() { - return dt.withLaterOffsetAtOverlap(); - } - - public ZonedDateTime withMinute(int value) { - return dt.withMinute(value); - } - - public ZonedDateTime withMonth(int value) { - return dt.withMonth(value); - } - - public ZonedDateTime withNano(int value) { - return dt.withNano(value); - } - - public ZonedDateTime withSecond(int value) { - return dt.withSecond(value); - } - - public ZonedDateTime withYear(int value) { - return dt.withYear(value); - } - - @Override - public ZonedDateTime withZoneSameLocal(ZoneId zone) { - return dt.withZoneSameLocal(zone); - } - - @Override - public ZonedDateTime withZoneSameInstant(ZoneId zone) { - return dt.withZoneSameInstant(zone); - } - - public DayOfWeek getDayOfWeekEnum() { - return dt.getDayOfWeek(); - } -} diff --git a/server/src/main/java/org/opensearch/script/ScoreScriptUtils.java b/server/src/main/java/org/opensearch/script/ScoreScriptUtils.java index b94ff77a1d0b7..76d0a8bb44da0 100644 --- a/server/src/main/java/org/opensearch/script/ScoreScriptUtils.java +++ b/server/src/main/java/org/opensearch/script/ScoreScriptUtils.java @@ -45,6 +45,7 @@ import org.opensearch.index.mapper.DateFieldMapper; import java.time.ZoneId; +import java.time.ZonedDateTime; import static org.opensearch.common.util.BitMixer.mix32; @@ -301,7 +302,7 @@ public DecayDateLinear(String originStr, String scaleStr, String offsetStr, doub this.scaling = scale / (1.0 - decay); } - public double decayDateLinear(JodaCompatibleZonedDateTime docValueDate) { + public double decayDateLinear(ZonedDateTime docValueDate) { long docValue = docValueDate.toInstant().toEpochMilli(); // as java.lang.Math#abs(long) is a forbidden API, have to use this comparison instead long diff = (docValue >= origin) ? (docValue - origin) : (origin - docValue); @@ -329,7 +330,7 @@ public DecayDateExp(String originStr, String scaleStr, String offsetStr, double this.scaling = Math.log(decay) / scale; } - public double decayDateExp(JodaCompatibleZonedDateTime docValueDate) { + public double decayDateExp(ZonedDateTime docValueDate) { long docValue = docValueDate.toInstant().toEpochMilli(); long diff = (docValue >= origin) ? (docValue - origin) : (origin - docValue); long distance = Math.max(0, diff - offset); @@ -356,7 +357,7 @@ public DecayDateGauss(String originStr, String scaleStr, String offsetStr, doubl this.scaling = 0.5 * Math.pow(scale, 2.0) / Math.log(decay); } - public double decayDateGauss(JodaCompatibleZonedDateTime docValueDate) { + public double decayDateGauss(ZonedDateTime docValueDate) { long docValue = docValueDate.toInstant().toEpochMilli(); long diff = (docValue >= origin) ? (docValue - origin) : (origin - docValue); long distance = Math.max(0, diff - offset); diff --git a/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptDoubleValues.java b/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptDoubleValues.java index 921a3bb344ae0..9b73f1b2155fb 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptDoubleValues.java +++ b/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptDoubleValues.java @@ -35,7 +35,6 @@ import org.opensearch.common.lucene.ScorerAware; import org.opensearch.index.fielddata.SortingNumericDoubleValues; import org.opensearch.script.AggregationScript; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.opensearch.search.aggregations.AggregationExecutionException; import org.joda.time.ReadableInstant; @@ -111,8 +110,6 @@ private static double toDoubleValue(Object o) { return ((ReadableInstant) o).getMillis(); } else if (o instanceof ZonedDateTime) { return ((ZonedDateTime) o).toInstant().toEpochMilli(); - } else if (o instanceof JodaCompatibleZonedDateTime) { - return ((JodaCompatibleZonedDateTime) o).toInstant().toEpochMilli(); } else if (o instanceof Boolean) { // We do expose boolean fields as boolean in scripts, however aggregations still expect // that scripts return the same internal representation as regular fields, so boolean diff --git a/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptLongValues.java b/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptLongValues.java index 5f169898f4653..b62178f85273a 100644 --- a/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptLongValues.java +++ b/server/src/main/java/org/opensearch/search/aggregations/support/values/ScriptLongValues.java @@ -36,7 +36,6 @@ import org.opensearch.common.lucene.ScorerAware; import org.opensearch.index.fielddata.AbstractSortingNumericDocValues; import org.opensearch.script.AggregationScript; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.opensearch.search.aggregations.AggregationExecutionException; import org.joda.time.ReadableInstant; @@ -110,8 +109,6 @@ private static long toLongValue(Object o) { return ((ReadableInstant) o).getMillis(); } else if (o instanceof ZonedDateTime) { return ((ZonedDateTime) o).toInstant().toEpochMilli(); - } else if (o instanceof JodaCompatibleZonedDateTime) { - return ((JodaCompatibleZonedDateTime) o).toInstant().toEpochMilli(); } else if (o instanceof Boolean) { // We do expose boolean fields as boolean in scripts, however aggregations still expect // that scripts return the same internal representation as regular fields, so boolean diff --git a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java index d408898d04f04..d560593caaacb 100644 --- a/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java +++ b/server/src/test/java/org/opensearch/common/io/stream/BytesStreamsTests.java @@ -48,7 +48,6 @@ import org.opensearch.core.common.io.stream.StreamInput; import org.opensearch.core.common.io.stream.StreamOutput; import org.opensearch.core.common.io.stream.Writeable; -import org.opensearch.script.JodaCompatibleZonedDateTime; import org.opensearch.test.OpenSearchTestCase; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -56,6 +55,7 @@ import java.io.EOFException; import java.io.IOException; import java.time.ZoneId; +import java.time.ZonedDateTime; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -364,10 +364,10 @@ public void testSimpleStreams() throws Exception { assertEquals(DateTimeZone.getDefault(), Joda.readOptionalTimeZone(in)); assertNull(Joda.readOptionalTimeZone(in)); Object dt = in.readGenericValue(); - assertThat(dt, instanceOf(JodaCompatibleZonedDateTime.class)); - JodaCompatibleZonedDateTime jdt = (JodaCompatibleZonedDateTime) dt; - assertThat(jdt.getZonedDateTime().toInstant().toEpochMilli(), equalTo(123456L)); - assertThat(jdt.getZonedDateTime().getZone(), equalTo(ZoneId.of("America/Los_Angeles"))); + assertThat(dt, instanceOf(ZonedDateTime.class)); + ZonedDateTime zdt = (ZonedDateTime) dt; + assertThat(zdt.toInstant().toEpochMilli(), equalTo(123456L)); + assertThat(zdt.getZone(), equalTo(ZoneId.of("America/Los_Angeles"))); assertEquals(0, in.available()); IllegalArgumentException ex = expectThrows(IllegalArgumentException.class, () -> out.writeGenericValue(new Object() { @Override diff --git a/server/src/test/java/org/opensearch/script/JodaCompatibleZonedDateTimeTests.java b/server/src/test/java/org/opensearch/script/JodaCompatibleZonedDateTimeTests.java deleted file mode 100644 index a3156897540b2..0000000000000 --- a/server/src/test/java/org/opensearch/script/JodaCompatibleZonedDateTimeTests.java +++ /dev/null @@ -1,220 +0,0 @@ -/* - * SPDX-License-Identifier: Apache-2.0 - * - * The OpenSearch Contributors require contributions made to - * this file be licensed under the Apache-2.0 license or a - * compatible open source license. - */ - -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/* - * Modifications Copyright OpenSearch Contributors. See - * GitHub history for details. - */ - -package org.opensearch.script; - -import org.opensearch.common.time.DateFormatters; -import org.opensearch.test.OpenSearchTestCase; -import org.joda.time.DateTime; -import org.joda.time.DateTimeZone; -import org.junit.Before; - -import java.time.DayOfWeek; -import java.time.Instant; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.Month; -import java.time.ZoneOffset; -import java.time.format.DateTimeFormatter; -import java.time.temporal.ChronoField; -import java.util.Locale; - -import static org.hamcrest.Matchers.equalTo; - -public class JodaCompatibleZonedDateTimeTests extends OpenSearchTestCase { - private JodaCompatibleZonedDateTime javaTime; - private DateTime jodaTime; - - @Before - public void setupTime() { - long millis = randomIntBetween(0, Integer.MAX_VALUE); - javaTime = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); - jodaTime = new DateTime(millis, DateTimeZone.forOffsetHours(-7)); - } - - public void testEquals() { - assertThat(javaTime, equalTo(javaTime)); - } - - public void testToString() { - assertThat(javaTime.toString(), equalTo(jodaTime.toString())); - } - - public void testDayOfMonth() { - assertThat(javaTime.getDayOfMonth(), equalTo(jodaTime.getDayOfMonth())); - } - - public void testDayOfYear() { - assertThat(javaTime.getDayOfYear(), equalTo(jodaTime.getDayOfYear())); - } - - public void testHour() { - assertThat(javaTime.getHour(), equalTo(jodaTime.getHourOfDay())); - } - - public void testLocalDate() { - assertThat(javaTime.toLocalDate(), equalTo(LocalDate.of(jodaTime.getYear(), jodaTime.getMonthOfYear(), jodaTime.getDayOfMonth()))); - } - - public void testLocalDateTime() { - LocalDateTime dt = LocalDateTime.of( - jodaTime.getYear(), - jodaTime.getMonthOfYear(), - jodaTime.getDayOfMonth(), - jodaTime.getHourOfDay(), - jodaTime.getMinuteOfHour(), - jodaTime.getSecondOfMinute(), - jodaTime.getMillisOfSecond() * 1000000 - ); - assertThat(javaTime.toLocalDateTime(), equalTo(dt)); - } - - public void testMinute() { - assertThat(javaTime.getMinute(), equalTo(jodaTime.getMinuteOfHour())); - } - - public void testMonth() { - assertThat(javaTime.getMonth(), equalTo(Month.of(jodaTime.getMonthOfYear()))); - } - - public void testMonthValue() { - assertThat(javaTime.getMonthValue(), equalTo(jodaTime.getMonthOfYear())); - } - - public void testNano() { - assertThat(javaTime.getNano(), equalTo(jodaTime.getMillisOfSecond() * 1000000)); - } - - public void testSecond() { - assertThat(javaTime.getSecond(), equalTo(jodaTime.getSecondOfMinute())); - } - - public void testYear() { - assertThat(javaTime.getYear(), equalTo(jodaTime.getYear())); - } - - public void testZone() { - assertThat(javaTime.getZone().getId(), equalTo(jodaTime.getZone().getID())); - } - - public void testMillis() { - assertThat(javaTime.toInstant().toEpochMilli(), equalTo(jodaTime.getMillis())); - } - - public void testCenturyOfEra() { - assertThat(javaTime.get(ChronoField.YEAR_OF_ERA) / 100, equalTo(jodaTime.getCenturyOfEra())); - } - - public void testEra() { - assertThat(javaTime.get(ChronoField.ERA), equalTo(jodaTime.getEra())); - } - - public void testHourOfDay() { - assertThat(javaTime.getHour(), equalTo(jodaTime.getHourOfDay())); - } - - public void testMillisOfDay() { - assertThat(javaTime.get(ChronoField.MILLI_OF_DAY), equalTo(jodaTime.getMillisOfDay())); - } - - public void testMillisOfSecond() { - assertThat(javaTime.get(ChronoField.MILLI_OF_SECOND), equalTo(jodaTime.getMillisOfSecond())); - } - - public void testMinuteOfDay() { - assertThat(javaTime.get(ChronoField.MINUTE_OF_DAY), equalTo(jodaTime.getMinuteOfDay())); - } - - public void testMinuteOfHour() { - assertThat(javaTime.getMinute(), equalTo(jodaTime.getMinuteOfHour())); - } - - public void testMonthOfYear() { - assertThat(javaTime.getMonthValue(), equalTo(jodaTime.getMonthOfYear())); - } - - public void testSecondOfDay() { - assertThat(javaTime.get(ChronoField.SECOND_OF_DAY), equalTo(jodaTime.getSecondOfDay())); - } - - public void testSecondOfMinute() { - assertThat(javaTime.getSecond(), equalTo(jodaTime.getSecondOfMinute())); - } - - public void testWeekOfWeekyear() { - assertThat(javaTime.get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear()), equalTo(jodaTime.getWeekOfWeekyear())); - } - - public void testWeekyear() { - assertThat(javaTime.get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear()), equalTo(jodaTime.getWeekyear())); - } - - public void testYearOfCentury() { - assertThat(javaTime.get(ChronoField.YEAR_OF_ERA) % 100, equalTo(jodaTime.getYearOfCentury())); - } - - public void testYearOfEra() { - assertThat(javaTime.get(ChronoField.YEAR_OF_ERA), equalTo(jodaTime.getYearOfEra())); - } - - public void testToString2() { - assertThat(DateTimeFormatter.ofPattern("EEE", Locale.GERMANY).format(javaTime), equalTo(jodaTime.toString("EEE", Locale.GERMANY))); - } - - public void testDayOfWeek() { - assertThat(javaTime.getDayOfWeekEnum().getValue(), equalTo(jodaTime.getDayOfWeek())); - } - - public void testDayOfWeekEnum() { - assertThat(javaTime.getDayOfWeekEnum(), equalTo(DayOfWeek.of(jodaTime.getDayOfWeek()))); - } - - public void testIsEqual() { - assertTrue(javaTime.isEqual(javaTime)); - } - - public void testIsAfter() { - long millis = randomLongBetween(0, Integer.MAX_VALUE / 2); - JodaCompatibleZonedDateTime beforeTime = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); - millis = randomLongBetween(millis + 1, Integer.MAX_VALUE); - JodaCompatibleZonedDateTime afterTime = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); - assertTrue(afterTime.isAfter(beforeTime)); - } - - public void testIsBefore() { - long millis = randomLongBetween(0, Integer.MAX_VALUE / 2); - JodaCompatibleZonedDateTime beforeTime = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); - millis = randomLongBetween(millis + 1, Integer.MAX_VALUE); - JodaCompatibleZonedDateTime afterTime = new JodaCompatibleZonedDateTime(Instant.ofEpochMilli(millis), ZoneOffset.ofHours(-7)); - assertTrue(beforeTime.isBefore(afterTime)); - } -}