Skip to content

Commit 8615d1c

Browse files
committed
Complete Joda-Time to java.time migration
This completes the exhaustive refactoring of foundational temporal types from Joda-Time to the native java.time API across the entire codebase. - Replaced org.joda.time.DateTime, Instant, LocalDate, and Duration with java.time equivalents. - Audited and updated Clock implementations (FakeClock, SystemClock). Added nowMillis(), nowDate(), and nowDateTime() to eliminate repetitive conversions and maintain parallel naming. - Replaced ZonedDateTime with OffsetDateTime globally per go/avoid-zdt. OffsetDateTime is a better fit as we use a hardcoded ZoneOffset.UTC throughout the system, making geographical time zone rules (like daylight saving time) irrelevant and preventing serialization ambiguities. Added a presubmit check. - Completely removed all transitional bridge methods from DateTimeUtils and deleted obsolete converters (e.g., DateTimeConverter). - Updated testing infrastructure, Apache Beam pipelines, custom JCommander parameters, and networking modules to solely rely on java.time primitives. - Retained the lone necessary org.joda.time.Instant usage in SafeBrowsingTransforms required by the Apache Beam API. - Cleared Gradle lockfiles and removed the joda-time dependency entirely from the build configuration.
1 parent b8f14fe commit 8615d1c

215 files changed

Lines changed: 574 additions & 1373 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

GEMINI.md

Lines changed: 14 additions & 40 deletions
Large diffs are not rendered by default.

common/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ dependencies {
5858
implementation deps['com.google.code.findbugs:jsr305']
5959
implementation deps['com.google.guava:guava']
6060
implementation deps['jakarta.inject:jakarta.inject-api']
61-
implementation deps['joda-time:joda-time']
6261
implementation deps['com.google.flogger:flogger']
6362
implementation deps['io.github.java-diff-utils:java-diff-utils']
6463
implementation deps['com.google.truth:truth']

common/gradle.lockfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ io.github.java-diff-utils:java-diff-utils:4.12=annotationProcessor,testAnnotatio
3737
io.github.java-diff-utils:java-diff-utils:4.16=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath,testing,testingCompileClasspath
3838
jakarta.inject:jakarta.inject-api:2.0.1=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath,testing,testingCompileClasspath
3939
javax.inject:javax.inject:1=annotationProcessor,testAnnotationProcessor,testingAnnotationProcessor
40-
joda-time:joda-time:2.14.2=compileClasspath,deploy_jar,runtimeClasspath,testCompileClasspath,testRuntimeClasspath,testing,testingCompileClasspath
40+
joda-time:joda-time:2.14.2=deploy_jar,testRuntimeClasspath,testing
4141
junit:junit:4.13.2=testCompileClasspath,testRuntimeClasspath,testing,testingCompileClasspath
4242
net.sf.saxon:Saxon-HE:12.5=checkstyle
4343
org.antlr:antlr4-runtime:4.13.2=checkstyle

common/src/main/java/google/registry/util/Clock.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@
1616

1717
import java.io.Serializable;
1818
import java.time.Instant;
19+
import java.time.LocalDate;
20+
import java.time.OffsetDateTime;
1921
import java.time.ZoneOffset;
20-
import java.time.ZonedDateTime;
2122
import javax.annotation.concurrent.ThreadSafe;
2223

2324
/**
2425
* A clock that tells the current time in milliseconds or nanoseconds.
2526
*
2627
* <p>Clocks are technically serializable because they are either a stateless wrapper around the
27-
* system clock, or for testing, are just a wrapper around a DateTime. This means that if you
28+
* system clock, or for testing, are just a wrapper around an Instant. This means that if you
2829
* serialize a clock and deserialize it elsewhere, you won't necessarily get the same time or time
2930
* zone -- what you will get is a functioning clock.
3031
*/
@@ -34,8 +35,18 @@ public interface Clock extends Serializable {
3435
/** Returns current Instant (which is always in UTC). */
3536
Instant now();
3637

37-
/** Returns the current time as a {@link ZonedDateTime} in UTC. */
38-
default ZonedDateTime nowDate() {
39-
return ZonedDateTime.ofInstant(now(), ZoneOffset.UTC);
38+
/** Returns the current time as an {@link OffsetDateTime} in UTC. */
39+
default OffsetDateTime nowDateTime() {
40+
return OffsetDateTime.ofInstant(now(), ZoneOffset.UTC);
41+
}
42+
43+
/** Returns the current time as a {@link LocalDate} in UTC. */
44+
default LocalDate nowDate() {
45+
return LocalDate.ofInstant(now(), ZoneOffset.UTC);
46+
}
47+
48+
/** Returns the current time in milliseconds since the epoch. */
49+
default long nowMillis() {
50+
return now().toEpochMilli();
4051
}
4152
}

common/src/main/java/google/registry/util/DateTimeUtils.java

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package google.registry.util;
1616

1717
import static com.google.common.base.Preconditions.checkArgument;
18-
import static org.joda.time.DateTimeZone.UTC;
1918

2019
import com.google.common.collect.Iterables;
2120
import com.google.common.collect.Lists;
@@ -24,14 +23,8 @@
2423
import java.time.LocalDate;
2524
import java.time.ZoneOffset;
2625
import java.time.format.DateTimeFormatter;
27-
import java.time.format.DateTimeFormatterBuilder;
2826
import java.time.format.DateTimeParseException;
29-
import java.time.format.SignStyle;
30-
import java.time.temporal.ChronoField;
3127
import java.time.temporal.ChronoUnit;
32-
import javax.annotation.Nullable;
33-
import org.joda.time.DateTime;
34-
import org.joda.time.ReadableDuration;
3528

3629
public abstract class DateTimeUtils {
3730

@@ -52,23 +45,24 @@ public abstract class DateTimeUtils {
5245
*
5346
* <p>Example: {@code 2024-03-27T10:15:30.105Z}
5447
*
55-
* <p>Handles large/negative years by using a sign prefix if necessary, compatible with {@link
56-
* Instant#parse}.
48+
* <p>Note: We deliberately strip the leading {@code +} sign from the formatted year field if
49+
* present. While standard ISO 8601 specifies that years with more than 4 digits should be
50+
* prefixed with a {@code +} sign, W3C XML Schema 1.0 (which our EPP RDE XSD uses) strictly
51+
* forbids leading plus signs in {@code xsd:dateTime} strings. Suppressing the plus sign ensures
52+
* our generated XML continues to pass strict XSD validation for large years (e.g. {@code
53+
* 294247-01-10T04:00:54.775Z}).
5754
*/
5855
private static final DateTimeFormatter ISO_8601_FORMATTER =
59-
new DateTimeFormatterBuilder()
60-
.appendValue(ChronoField.YEAR, 4, 10, SignStyle.NOT_NEGATIVE)
61-
.appendPattern("-MM-dd'T'HH:mm:ss.SSS'Z'")
62-
.toFormatter()
63-
.withZone(ZoneOffset.UTC);
56+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'").withZone(ZoneOffset.UTC);
6457

6558
/** A formatter that produces lowercase, filename-safe and job-name-safe timestamps. */
6659
public static final DateTimeFormatter LOWERCASE_TIMESTAMP_FORMATTER =
6760
DateTimeFormatter.ofPattern("yyyy-MM-dd't'HH-mm-ss'z'").withZone(ZoneOffset.UTC);
6861

6962
/** Formats an {@link Instant} to an ISO-8601 string. */
7063
public static String formatInstant(Instant instant) {
71-
return ISO_8601_FORMATTER.format(instant);
64+
String formatted = ISO_8601_FORMATTER.format(instant);
65+
return formatted.startsWith("+") ? formatted.substring(1) : formatted;
7266
}
7367

7468
/**
@@ -79,9 +73,15 @@ public static String formatInstant(Instant instant) {
7973
* large years (e.g. {@code 294247-01-10T04:00:54.775Z}).
8074
*/
8175
public static Instant parseInstant(String timestamp) {
76+
if (!timestamp.startsWith("+") && !timestamp.startsWith("-")) {
77+
int dashIndex = timestamp.indexOf('-');
78+
if (dashIndex > 4) {
79+
timestamp = "+" + timestamp;
80+
}
81+
}
8282
try {
8383
// Try the standard millisecond precision format first.
84-
return Instant.from(ISO_8601_FORMATTER.parse(timestamp));
84+
return Instant.from(DateTimeFormatter.ISO_INSTANT.parse(timestamp));
8585
} catch (DateTimeParseException e) {
8686
// Fall back to the standard ISO instant parser which handles varied precision.
8787
return Instant.parse(timestamp);
@@ -93,7 +93,7 @@ public static Instant earliestOf(Instant first, Instant... rest) {
9393
return earliestOf(Lists.asList(first, rest));
9494
}
9595

96-
/** Returns the earliest element in a {@link Instant} iterable. */
96+
/** Returns the earliest element in an {@link Instant} iterable. */
9797
public static Instant earliestOf(Iterable<Instant> instants) {
9898
checkArgument(!Iterables.isEmpty(instants));
9999
return Ordering.<Instant>natural().min(instants);
@@ -104,24 +104,12 @@ public static Instant latestOf(Instant first, Instant... rest) {
104104
return latestOf(Lists.asList(first, rest));
105105
}
106106

107-
/** Returns the latest element in a {@link Instant} iterable. */
107+
/** Returns the latest element in an {@link Instant} iterable. */
108108
public static Instant latestOf(Iterable<Instant> instants) {
109109
checkArgument(!Iterables.isEmpty(instants));
110110
return Ordering.<Instant>natural().max(instants);
111111
}
112112

113-
/** Converts a Joda-Time Duration to a java.time.Duration. */
114-
@Nullable
115-
public static java.time.Duration toJavaDuration(@Nullable ReadableDuration duration) {
116-
return duration == null ? null : java.time.Duration.ofMillis(duration.getMillis());
117-
}
118-
119-
/** Converts a java.time.Duration to a Joda-Time Duration. */
120-
@Nullable
121-
public static org.joda.time.Duration toJodaDuration(@Nullable java.time.Duration duration) {
122-
return duration == null ? null : org.joda.time.Duration.millis(duration.toMillis());
123-
}
124-
125113
/** Returns whether the first {@link Instant} is equal to or earlier than the second. */
126114
public static boolean isBeforeOrAt(Instant timeToCheck, Instant timeToCompareTo) {
127115
return !timeToCheck.isAfter(timeToCompareTo);
@@ -134,7 +122,7 @@ public static boolean isAtOrAfter(Instant timeToCheck, Instant timeToCompareTo)
134122

135123
/**
136124
* Adds years to a date, in the {@code Duration} sense of semantic years. Use this instead of
137-
* {@link java.time.ZonedDateTime#plusYears} to ensure that we never end up on February 29.
125+
* {@link java.time.OffsetDateTime#plusYears} to ensure that we never end up on February 29.
138126
*/
139127
public static Instant plusYears(Instant now, int years) {
140128
checkArgument(years >= 0);
@@ -157,7 +145,7 @@ public static Instant minusMonths(Instant now, int months) {
157145

158146
/**
159147
* Subtracts years from a date, in the {@code Duration} sense of semantic years. Use this instead
160-
* of {@link java.time.ZonedDateTime#minusYears} to ensure that we never end up on February 29.
148+
* of {@link java.time.OffsetDateTime#minusYears} to ensure that we never end up on February 29.
161149
*/
162150
public static Instant minusYears(Instant now, long years) {
163151
checkArgument(years >= 0);
@@ -171,24 +159,6 @@ public static LocalDate toLocalDate(Instant instant) {
171159
return instant.atZone(ZoneOffset.UTC).toLocalDate();
172160
}
173161

174-
/** Convert a joda {@link DateTime} to a java.time {@link Instant}, null-safe. */
175-
@Nullable
176-
public static Instant toInstant(@Nullable DateTime dateTime) {
177-
return (dateTime == null) ? null : Instant.ofEpochMilli(dateTime.getMillis());
178-
}
179-
180-
/** Convert a java.time {@link Instant} to a joda {@link DateTime}, null-safe. */
181-
@Nullable
182-
public static DateTime toDateTime(@Nullable Instant instant) {
183-
return (instant == null) ? null : new DateTime(instant.toEpochMilli(), UTC);
184-
}
185-
186-
/** Convert a java.time {@link java.time.Instant} to a joda {@link org.joda.time.Instant}. */
187-
@Nullable
188-
public static org.joda.time.Instant toJodaInstant(@Nullable java.time.Instant instant) {
189-
return (instant == null) ? null : org.joda.time.Instant.ofEpochMilli(instant.toEpochMilli());
190-
}
191-
192162
public static Instant plusHours(Instant instant, long hours) {
193163
return instant.plus(hours, ChronoUnit.HOURS);
194164
}

common/src/main/java/google/registry/util/SystemClock.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,9 @@ public SystemClock() {}
3131

3232
@Override
3333
public Instant now() {
34-
// Truncate to milliseconds to match the precision of Joda DateTime and our database schema
35-
// (which uses millisecond precision via DateTimeConverter). This prevents subtle comparison
36-
// bugs where a high-precision Instant would be considered "after" a truncated database
37-
// timestamp.
34+
// Truncate to milliseconds to match the precision of our database schema.
35+
// This prevents subtle comparison bugs where a high-precision Instant would be
36+
// considered "after" a truncated database timestamp.
3837
return Instant.now().truncatedTo(MILLIS);
3938
}
4039
}

common/src/testing/java/google/registry/testing/FakeClock.java

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.time.Instant;
2222
import java.util.concurrent.atomic.AtomicLong;
2323
import javax.annotation.concurrent.ThreadSafe;
24-
import org.joda.time.ReadableDuration;
25-
import org.joda.time.ReadableInstant;
2624

2725
/** A mock clock for testing purposes that supports telling, setting, and advancing the time. */
2826
@ThreadSafe
@@ -41,12 +39,6 @@ public FakeClock() {
4139
this(START_INSTANT);
4240
}
4341

44-
/** Creates a FakeClock initialized to a specific time. */
45-
@Deprecated
46-
public FakeClock(ReadableInstant startTime) {
47-
setTo(startTime);
48-
}
49-
5042
/** Creates a FakeClock initialized to a specific time. */
5143
public FakeClock(Instant startTime) {
5244
setTo(startTime);
@@ -66,12 +58,6 @@ public Instant now() {
6658
* @param autoIncrementStep the new auto increment duration
6759
* @return this
6860
*/
69-
@Deprecated
70-
public FakeClock setAutoIncrementStep(ReadableDuration autoIncrementStep) {
71-
this.autoIncrementStepMs = autoIncrementStep.getMillis();
72-
return this;
73-
}
74-
7561
/**
7662
* Sets the increment applied to the clock whenever it is queried. The increment is zero by
7763
* default: the clock is left unchanged when queried.
@@ -91,23 +77,11 @@ public void advanceOneMilli() {
9177
advanceBy(Duration.ofMillis(1));
9278
}
9379

94-
/** Advances clock by some duration. */
95-
@Deprecated
96-
public void advanceBy(ReadableDuration duration) {
97-
currentTimeMillis.addAndGet(duration.getMillis());
98-
}
99-
10080
/** Advances clock by some duration. */
10181
public void advanceBy(Duration duration) {
10282
currentTimeMillis.addAndGet(duration.toMillis());
10383
}
10484

105-
/** Sets the time to the specified instant. */
106-
@Deprecated
107-
public void setTo(ReadableInstant time) {
108-
currentTimeMillis.set(time.getMillis());
109-
}
110-
11185
/** Sets the time to the specified instant. */
11286
public void setTo(Instant time) {
11387
currentTimeMillis.set(time.toEpochMilli());

config/nom_build.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,4 +409,3 @@ def main(args) -> int:
409409
sys.exit(main(sys.argv))
410410
except Abort as ex:
411411
sys.exit(1)
412-

0 commit comments

Comments
 (0)