Skip to content

Commit

Permalink
Add a time-j2kt-native target.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 734154761
  • Loading branch information
stefanhaustein authored and copybara-github committed Mar 6, 2025
1 parent 683e9d2 commit 7a5905d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.protobuf.util.Timestamps.NANOS_PER_SECOND;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CompileTimeConstant;
Expand Down Expand Up @@ -144,6 +145,7 @@ public static boolean isPositive(Duration duration) {
*/
@CanIgnoreReturnValue
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
public static Duration checkNotNegative(Duration duration) {
checkArgument(!isNegative(duration), "duration (%s) must not be negative", toString(duration));
Expand All @@ -158,6 +160,7 @@ public static Duration checkNotNegative(Duration duration) {
*/
@CanIgnoreReturnValue
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
public static Duration checkPositive(Duration duration) {
checkArgument(isPositive(duration), "duration (%s) must be positive", toString(duration));
Expand Down Expand Up @@ -201,6 +204,7 @@ public static Duration checkValid(Duration.Builder durationBuilder) {
* @throws IllegalArgumentException if the given duration is not in the valid range.
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
public static String toString(Duration duration) {
checkValid(duration);
Expand Down Expand Up @@ -230,6 +234,7 @@ public static String toString(Duration duration) {
* @throws ParseException if the string is not in the duration format
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ktIncompatible
@J2ObjCIncompatible
public static Duration parse(String value) throws ParseException {
// Must end with "s".
Expand Down Expand Up @@ -281,6 +286,7 @@ public static Duration parse(String value) throws ParseException {
* @throws IllegalArgumentException if parsing fails
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ktIncompatible
@J2ObjCIncompatible
public static Duration parseUnchecked(@CompileTimeConstant String value) {
try {
Expand Down
14 changes: 14 additions & 0 deletions java/util/src/main/java/com/google/protobuf/util/Timestamps.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static com.google.common.math.LongMath.checkedSubtract;

import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.J2ktIncompatible;
import com.google.common.base.Strings;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.CompileTimeConstant;
Expand Down Expand Up @@ -66,6 +67,7 @@ public final class Timestamps {

@GwtIncompatible("Date formatting is not supported in non JVM java.time")
@J2ObjCIncompatible
@J2ktIncompatible
private static final ThreadLocal<SimpleDateFormat> timestampFormat =
new ThreadLocal<SimpleDateFormat>() {
@Override
Expand All @@ -76,6 +78,7 @@ protected SimpleDateFormat initialValue() {

@GwtIncompatible("Date formatting is not supported in non JVM java.time")
@J2ObjCIncompatible
@J2ktIncompatible
private static SimpleDateFormat createTimestampFormat() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ENGLISH);
GregorianCalendar calendar = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
Expand Down Expand Up @@ -200,6 +203,7 @@ public static Timestamp checkValid(Timestamp.Builder timestampBuilder) {
* @throws IllegalArgumentException if the given timestamp is not in the valid range.
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
public static String toString(Timestamp timestamp) {
checkValid(timestamp);
Expand Down Expand Up @@ -231,6 +235,7 @@ public static String toString(Timestamp timestamp) {
* @throws ParseException if parsing fails
*/
@GwtIncompatible("ParseException is not supported in Xplat")
@J2ktIncompatible
@J2ObjCIncompatible
public static Timestamp parse(String value) throws ParseException {
int dayOffset = value.indexOf('T');
Expand Down Expand Up @@ -298,6 +303,7 @@ public static Timestamp parse(String value) throws ParseException {
* @throws IllegalArgumentException if parsing fails
*/
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
public static Timestamp parseUnchecked(@CompileTimeConstant String value) {
try {
Expand All @@ -312,21 +318,25 @@ public static Timestamp parseUnchecked(@CompileTimeConstant String value) {
// the following 3 constants contain references to java.time.Instant methods (if that class is
// available at runtime); otherwise, they are null.
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ktIncompatible
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_NOW = instantMethod("now");

@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ktIncompatible
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_GET_EPOCH_SECOND = instantMethod("getEpochSecond");

@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ktIncompatible
@J2ObjCIncompatible
@Nullable
private static final Method INSTANT_GET_NANO = instantMethod("getNano");

@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ktIncompatible
@J2ObjCIncompatible
@Nullable
private static Method instantMethod(String methodName) {
Expand All @@ -345,6 +355,7 @@ private static Method instantMethod(String methodName) {
* instance to read the current time.
*/
@GwtIncompatible("Uses reflection to access methods of java.time.Instant")
@J2ktIncompatible
@J2ObjCIncompatible
@SuppressWarnings("nullness")
public static Timestamp now() {
Expand Down Expand Up @@ -515,6 +526,7 @@ static Timestamp normalizedTimestamp(long seconds, int nanos) {
}

@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
private static long parseTimezoneOffset(String value) throws ParseException {
int pos = value.indexOf(':');
Expand All @@ -533,6 +545,7 @@ private static long parseTimezoneOffset(String value) throws ParseException {
}

@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
static int parseNanos(String value) throws ParseException {
int result = 0;
Expand All @@ -550,6 +563,7 @@ static int parseNanos(String value) throws ParseException {

/** Format the nano part of a timestamp or a duration. */
@GwtIncompatible("Depends on String.format which is not supported in Xplat.")
@J2ktIncompatible
@J2ObjCIncompatible
static String formatNanos(int nanos) {
// Determine whether to use 3, 6, or 9 digits for the nano part.
Expand Down

0 comments on commit 7a5905d

Please sign in to comment.