mirror of
https://github.com/google/nomulus
synced 2026-05-23 00:01:58 +00:00
Refactor bsa, dns, batch, and reporting packages to java.time (#3031)
This commit migrates the BSA, DNS, batch, and reporting packages from Joda-Time to java.time. Key changes include: - Updated Sleeper, Clock, and BigqueryUtils to use java.time types natively. - Refactored models like RdeRevision and Tld to eliminate redundant Joda conversions, utilizing new DateTimeUtils static utilities for LocalDate. - Improved test safety by replacing dynamic Instant.now() calls with static parsed constants. - Migrated temporal arithmetic in test suites to use DateTimeUtils convenience methods (plusDays, minusDays). - Updated BigqueryUtils serialization to preserve millisecond precision and formatting for large years, ensuring consistency with previous Joda behavior. - Enhanced code readability by converting long concatenated strings to Java text blocks in LordnLogTest. - Resolved environmental test failures in SyncRegistrarsSheetTest by synchronizing the FakeClock with the JPA extension. - Updated project engineering standards (GEMINI.md) to prefer Truth's .hasValue() for Optional assertions. Verified with a clean full build and all relevant test suites passing.
This commit is contained in:
@@ -15,6 +15,7 @@
|
||||
package google.registry.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import com.google.common.collect.Iterables;
|
||||
import com.google.common.collect.Lists;
|
||||
@@ -30,14 +31,14 @@ import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import javax.annotation.Nullable;
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.ReadableDuration;
|
||||
|
||||
/** Utilities methods and constants related to Joda {@link DateTime} objects. */
|
||||
public abstract class DateTimeUtils {
|
||||
|
||||
/** The start of the epoch, in a convenient constant. */
|
||||
@Deprecated public static final DateTime START_OF_TIME = new DateTime(0, DateTimeZone.UTC);
|
||||
@Deprecated public static final DateTime START_OF_TIME = new DateTime(0, UTC);
|
||||
|
||||
/** The start of the UNIX epoch (which is defined in UTC), in a convenient constant. */
|
||||
public static final Instant START_INSTANT = Instant.ofEpochMilli(0);
|
||||
@@ -49,8 +50,7 @@ public abstract class DateTimeUtils {
|
||||
* but Java uses milliseconds, so this is the largest representable date that will survive a
|
||||
* round-trip through the database.
|
||||
*/
|
||||
@Deprecated
|
||||
public static final DateTime END_OF_TIME = new DateTime(Long.MAX_VALUE / 1000, DateTimeZone.UTC);
|
||||
@Deprecated public static final DateTime END_OF_TIME = new DateTime(Long.MAX_VALUE / 1000, UTC);
|
||||
|
||||
/**
|
||||
* An instant in the far future that we can treat as infinity.
|
||||
@@ -76,6 +76,10 @@ public abstract class DateTimeUtils {
|
||||
.toFormatter()
|
||||
.withZone(ZoneOffset.UTC);
|
||||
|
||||
/** A formatter that produces lowercase, filename-safe and job-name-safe timestamps. */
|
||||
public static final DateTimeFormatter LOWERCASE_TIMESTAMP_FORMATTER =
|
||||
DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH-mm-ss'z'").withZone(ZoneOffset.UTC);
|
||||
|
||||
/** Formats an {@link Instant} to an ISO-8601 string. */
|
||||
public static String formatInstant(Instant instant) {
|
||||
return ISO_8601_FORMATTER.format(instant);
|
||||
@@ -147,6 +151,18 @@ public abstract class DateTimeUtils {
|
||||
return !timeToCheck.isAfter(timeToCompareTo);
|
||||
}
|
||||
|
||||
/** Converts a Joda-Time Duration to a java.time.Duration. */
|
||||
@Nullable
|
||||
public static java.time.Duration toJavaDuration(@Nullable ReadableDuration duration) {
|
||||
return duration == null ? null : java.time.Duration.ofMillis(duration.getMillis());
|
||||
}
|
||||
|
||||
/** Converts a java.time.Duration to a Joda-Time Duration. */
|
||||
@Nullable
|
||||
public static org.joda.time.Duration toJodaDuration(@Nullable java.time.Duration duration) {
|
||||
return duration == null ? null : org.joda.time.Duration.millis(duration.toMillis());
|
||||
}
|
||||
|
||||
/** Returns whether the first {@link Instant} is equal to or earlier than the second. */
|
||||
public static boolean isBeforeOrAt(Instant timeToCheck, Instant timeToCompareTo) {
|
||||
return !timeToCheck.isAfter(timeToCompareTo);
|
||||
@@ -237,7 +253,17 @@ public abstract class DateTimeUtils {
|
||||
}
|
||||
|
||||
public static LocalDate toLocalDate(Date date) {
|
||||
return new LocalDate(date.getTime(), DateTimeZone.UTC);
|
||||
return new LocalDate(date.getTime(), UTC);
|
||||
}
|
||||
|
||||
/** Converts a java.time.LocalDate to a Joda-Time LocalDate. */
|
||||
public static LocalDate toJodaLocalDate(java.time.LocalDate localDate) {
|
||||
return new LocalDate(localDate.getYear(), localDate.getMonthValue(), localDate.getDayOfMonth());
|
||||
}
|
||||
|
||||
/** Converts an Instant to a Joda-Time LocalDate in UTC. */
|
||||
public static LocalDate toJodaLocalDate(Instant instant) {
|
||||
return new LocalDate(instant.toEpochMilli(), UTC);
|
||||
}
|
||||
|
||||
/** Convert a joda {@link DateTime} to a java.time {@link Instant}, null-safe. */
|
||||
@@ -249,7 +275,7 @@ public abstract class DateTimeUtils {
|
||||
/** Convert a java.time {@link Instant} to a joda {@link DateTime}, null-safe. */
|
||||
@Nullable
|
||||
public static DateTime toDateTime(@Nullable Instant instant) {
|
||||
return (instant == null) ? null : new DateTime(instant.toEpochMilli(), DateTimeZone.UTC);
|
||||
return (instant == null) ? null : new DateTime(instant.toEpochMilli(), UTC);
|
||||
}
|
||||
|
||||
/** Convert a java.time {@link java.time.Instant} to a joda {@link org.joda.time.Instant}. */
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
|
||||
package google.registry.util;
|
||||
|
||||
import java.time.Duration;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.ReadableDuration;
|
||||
|
||||
@@ -32,6 +33,15 @@ public interface Sleeper {
|
||||
*/
|
||||
void sleep(ReadableDuration duration) throws InterruptedException;
|
||||
|
||||
/**
|
||||
* Puts the current thread to sleep.
|
||||
*
|
||||
* @throws InterruptedException if this thread was interrupted
|
||||
*/
|
||||
default void sleep(Duration duration) throws InterruptedException {
|
||||
sleep(DateTimeUtils.toJodaDuration(duration));
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the current thread to sleep, ignoring interrupts.
|
||||
*
|
||||
@@ -42,6 +52,18 @@ public interface Sleeper {
|
||||
*/
|
||||
void sleepUninterruptibly(ReadableDuration duration);
|
||||
|
||||
/**
|
||||
* Puts the current thread to sleep, ignoring interrupts.
|
||||
*
|
||||
* <p>If {@link InterruptedException} was caught, then {@code Thread.currentThread().interrupt()}
|
||||
* will be called at the end of the {@code duration}.
|
||||
*
|
||||
* @see com.google.common.util.concurrent.Uninterruptibles#sleepUninterruptibly
|
||||
*/
|
||||
default void sleepUninterruptibly(Duration duration) {
|
||||
sleepUninterruptibly(DateTimeUtils.toJodaDuration(duration));
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the current thread to interruptible sleep.
|
||||
*
|
||||
@@ -57,4 +79,20 @@ public interface Sleeper {
|
||||
throw new RuntimeException("Interrupted.", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the current thread to interruptible sleep.
|
||||
*
|
||||
* <p>This is a convenience method for {@link #sleep} that properly converts an {@link
|
||||
* InterruptedException} to a {@link RuntimeException}.
|
||||
*/
|
||||
default void sleepInterruptibly(Duration duration) {
|
||||
try {
|
||||
sleep(duration);
|
||||
} catch (InterruptedException e) {
|
||||
// Restore current thread's interrupted state.
|
||||
Thread.currentThread().interrupt();
|
||||
throw new RuntimeException("Interrupted.", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,11 +14,11 @@
|
||||
|
||||
package google.registry.util;
|
||||
|
||||
import static java.time.temporal.ChronoUnit.MILLIS;
|
||||
import static org.joda.time.DateTimeZone.UTC;
|
||||
|
||||
import jakarta.inject.Inject;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.DateTime;
|
||||
|
||||
@@ -43,6 +43,6 @@ public class SystemClock implements Clock {
|
||||
// (which uses millisecond precision via DateTimeConverter). This prevents subtle comparison
|
||||
// bugs where a high-precision Instant would be considered "after" a truncated database
|
||||
// timestamp.
|
||||
return Instant.now().truncatedTo(ChronoUnit.MILLIS);
|
||||
return Instant.now().truncatedTo(MILLIS);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,11 +15,11 @@
|
||||
package google.registry.util;
|
||||
|
||||
import static com.google.common.base.Preconditions.checkArgument;
|
||||
import static google.registry.util.DateTimeUtils.toJavaDuration;
|
||||
|
||||
import com.google.common.util.concurrent.Uninterruptibles;
|
||||
import jakarta.inject.Inject;
|
||||
import java.io.Serializable;
|
||||
import java.time.Duration;
|
||||
import javax.annotation.concurrent.ThreadSafe;
|
||||
import org.joda.time.ReadableDuration;
|
||||
|
||||
@@ -41,6 +41,6 @@ public final class SystemSleeper implements Sleeper, Serializable {
|
||||
@Override
|
||||
public void sleepUninterruptibly(ReadableDuration duration) {
|
||||
checkArgument(duration.getMillis() >= 0);
|
||||
Uninterruptibles.sleepUninterruptibly(Duration.ofMillis(duration.getMillis()));
|
||||
Uninterruptibles.sleepUninterruptibly(toJavaDuration(duration));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user