Desugared library: Partial desugaring of new time apis

Change-Id: Iff5961794841ec6ef64e828f5109468e48c3b74d
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs.json b/src/library_desugar/jdk11/desugar_jdk_libs.json
index 51350bc..b30e713f 100644
--- a/src/library_desugar/jdk11/desugar_jdk_libs.json
+++ b/src/library_desugar/jdk11/desugar_jdk_libs.json
@@ -9,7 +9,9 @@
       "api_level_below_or_equal": 10000,
       "api_level_greater_or_equal": 26,
       "rewrite_prefix": {
-        "java.time.DesugarLocalDate": "j$.time.DesugarLocalDate"
+        "java.time.DesugarLocalDate": "j$.time.DesugarLocalDate",
+        "java.time.DesugarClock": "j$.time.DesugarClock",
+        "java.time.DesugarOffsetTime": "j$.time.DesugarOffsetTime"
       },
       "retarget_static_field": {
         "java.time.LocalDate java.time.LocalDate#EPOCH": "java.time.LocalDate java.time.DesugarLocalDate#EPOCH"
@@ -18,7 +20,9 @@
         "java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate)": "java.time.DesugarLocalDate",
         "java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate, java.time.Period)": "java.time.DesugarLocalDate",
         "java.time.LocalDate java.time.LocalDate#ofInstant(java.time.Instant, java.time.ZoneId)": "java.time.DesugarLocalDate",
-        "long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)": "java.time.DesugarLocalDate"
+        "long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)": "java.time.DesugarLocalDate",
+        "java.time.Clock java.time.Clock#tickMillis(java.time.ZoneId)": "java.time.DesugarClock",
+        "long java.time.OffsetTime#toEpochSecond(java.time.LocalDate)": "java.time.DesugarOffsetTime"
       },
       "amend_library_field": [
         "public static java.time.LocalDate java.time.LocalDate#EPOCH"
@@ -27,7 +31,9 @@
         "public java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate)",
         "public java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate, java.time.Period)",
         "public static java.time.LocalDate java.time.LocalDate#ofInstant(java.time.Instant, java.time.ZoneId)",
-        "public long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)"
+        "public long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)",
+        "public static java.time.Clock java.time.Clock#tickMillis(java.time.ZoneId)",
+        "public long java.time.OffsetTime#toEpochSecond(java.time.LocalDate)"
       ]
     },
     {
diff --git a/src/library_desugar/jdk11/desugar_jdk_libs_nio.json b/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
index 8ff9834..778c395 100644
--- a/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
+++ b/src/library_desugar/jdk11/desugar_jdk_libs_nio.json
@@ -17,6 +17,8 @@
       "api_level_greater_or_equal": 26,
       "rewrite_prefix": {
         "java.time.DesugarLocalDate": "j$.time.DesugarLocalDate",
+        "java.time.DesugarClock": "j$.time.DesugarClock",
+        "java.time.DesugarOffsetTime": "j$.time.DesugarOffsetTime",
         "java.nio.file.DesugarFiles": "j$.nio.file.DesugarFiles"
       },
       "retarget_static_field": {
@@ -27,6 +29,8 @@
         "java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate, java.time.Period)": "java.time.DesugarLocalDate",
         "java.time.LocalDate java.time.LocalDate#ofInstant(java.time.Instant, java.time.ZoneId)": "java.time.DesugarLocalDate",
         "long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)": "java.time.DesugarLocalDate",
+        "java.time.Clock java.time.Clock#tickMillis(java.time.ZoneId)": "java.time.DesugarClock",
+        "long java.time.OffsetTime#toEpochSecond(java.time.LocalDate)": "java.time.DesugarOffsetTime",
         "java.lang.String java.nio.file.Files#readString(java.nio.file.Path)": "java.nio.file.DesugarFiles",
         "java.lang.String java.nio.file.Files#readString(java.nio.file.Path, java.nio.charset.Charset)": "java.nio.file.DesugarFiles",
         "java.nio.file.Path java.nio.file.Files#writeString(java.nio.file.Path, java.lang.CharSequence, java.nio.file.OpenOption[])": "java.nio.file.DesugarFiles",
@@ -40,6 +44,8 @@
         "public java.util.stream.Stream java.time.LocalDate#datesUntil(java.time.LocalDate, java.time.Period)",
         "public static java.time.LocalDate java.time.LocalDate#ofInstant(java.time.Instant, java.time.ZoneId)",
         "public long java.time.LocalDate#toEpochSecond(java.time.LocalTime, java.time.ZoneOffset)",
+        "public static java.time.Clock java.time.Clock#tickMillis(java.time.ZoneId)",
+        "public long java.time.OffsetTime#toEpochSecond(java.time.LocalDate)",
         "public static java.lang.String java.nio.file.Files#readString(java.nio.file.Path)",
         "public static java.lang.String java.nio.file.Files#readString(java.nio.file.Path, java.nio.charset.Charset)",
         "public static java.nio.file.Path java.nio.file.Files#writeString(java.nio.file.Path, java.lang.CharSequence, java.nio.file.OpenOption[])",
diff --git a/src/test/examplesJava9/newtime/NewTimeMain.java b/src/test/examplesJava9/newtime/NewTimeMain.java
new file mode 100644
index 0000000..ab28a4d
--- /dev/null
+++ b/src/test/examplesJava9/newtime/NewTimeMain.java
@@ -0,0 +1,22 @@
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package newtime;
+
+import java.time.Clock;
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.OffsetTime;
+import java.time.ZoneId;
+import java.time.ZoneOffset;
+
+public class NewTimeMain {
+
+  public static void main(String[] args) {
+    Clock utc = Clock.tickMillis(ZoneId.of("UTC"));
+    System.out.println(utc.getZone());
+    OffsetTime ot = OffsetTime.of(LocalTime.NOON, ZoneOffset.UTC);
+    System.out.println(ot.toEpochSecond(LocalDate.MIN));
+  }
+}
diff --git a/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/NewTimeTest.java b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/NewTimeTest.java
new file mode 100644
index 0000000..d9dbd73
--- /dev/null
+++ b/src/test/java/com/android/tools/r8/desugar/desugaredlibrary/jdk11/NewTimeTest.java
@@ -0,0 +1,97 @@
+// Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+package com.android.tools.r8.desugar.desugaredlibrary.jdk11;
+
+import static com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification.DEFAULT_SPECIFICATIONS;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK11;
+import static com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification.JDK11_PATH;
+import static org.junit.Assert.assertEquals;
+
+import com.android.tools.r8.TestParameters;
+import com.android.tools.r8.ToolHelper;
+import com.android.tools.r8.desugar.desugaredlibrary.DesugaredLibraryTestBase;
+import com.android.tools.r8.desugar.desugaredlibrary.test.CompilationSpecification;
+import com.android.tools.r8.desugar.desugaredlibrary.test.LibraryDesugaringSpecification;
+import com.android.tools.r8.utils.AndroidApiLevel;
+import com.android.tools.r8.utils.StringUtils;
+import com.android.tools.r8.utils.codeinspector.CodeInspector;
+import com.android.tools.r8.utils.codeinspector.InstructionSubject;
+import com.google.common.collect.ImmutableList;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+import org.junit.runners.Parameterized.Parameters;
+
+@RunWith(Parameterized.class)
+public class NewTimeTest extends DesugaredLibraryTestBase {
+
+  private final TestParameters parameters;
+  private final LibraryDesugaringSpecification libraryDesugaringSpecification;
+  private final CompilationSpecification compilationSpecification;
+
+  private static final Path INPUT_JAR =
+      Paths.get(ToolHelper.EXAMPLES_JAVA9_BUILD_DIR + "newtime.jar");
+  private static final String EXPECTED_OUTPUT = StringUtils.lines("UTC", "-31557014135553600");
+  private static final String MAIN_CLASS = "newtime.NewTimeMain";
+
+  @Parameters(name = "{0}, spec: {1}, {2}")
+  public static List<Object[]> data() {
+    return buildParameters(
+        getTestParameters().withDexRuntimes().withAllApiLevels().build(),
+        ImmutableList.of(JDK11, JDK11_PATH),
+        DEFAULT_SPECIFICATIONS);
+  }
+
+  public NewTimeTest(
+      TestParameters parameters,
+      LibraryDesugaringSpecification libraryDesugaringSpecification,
+      CompilationSpecification compilationSpecification) {
+    this.parameters = parameters;
+    this.libraryDesugaringSpecification = libraryDesugaringSpecification;
+    this.compilationSpecification = compilationSpecification;
+  }
+
+  @Test
+  public void test() throws Exception {
+    testForDesugaredLibrary(parameters, libraryDesugaringSpecification, compilationSpecification)
+        .addProgramFiles(INPUT_JAR)
+        .addKeepMainRule(MAIN_CLASS)
+        .compile()
+        .withArt6Plus64BitsLib()
+        .inspect(this::assertCalls)
+        .run(parameters.getRuntime(), MAIN_CLASS)
+        .assertSuccessWithOutput(EXPECTED_OUTPUT);
+  }
+
+  private void assertCalls(CodeInspector codeInspector) {
+    codeInspector
+        .clazz(MAIN_CLASS)
+        .uniqueMethodWithOriginalName("main")
+        .streamInstructions()
+        .filter(c -> c.isInvoke())
+        .forEach(this::assertCorrectInvoke);
+  }
+
+  private void assertCorrectInvoke(InstructionSubject invoke) {
+    String name = invoke.getMethod().getName().toString();
+    if (name.equals("tickMillis")) {
+      if (parameters.getApiLevel().isLessThan(AndroidApiLevel.O)) {
+        assertEquals("j$.time.Clock", invoke.getMethod().getHolderType().toString());
+      } else {
+        assertEquals("j$.time.DesugarClock", invoke.getMethod().getHolderType().toString());
+      }
+    }
+    if (name.equals("toEpochSecond")) {
+      if (parameters.getApiLevel().isLessThan(AndroidApiLevel.O)) {
+        assertEquals("j$.time.OffsetTime", invoke.getMethod().getHolderType().toString());
+      } else {
+        assertEquals("j$.time.DesugarOffsetTime", invoke.getMethod().getHolderType().toString());
+      }
+    }
+  }
+}