Skip to content

Commit

Permalink
Merge pull request #1477 from yue9944882/support-rfc3339micro-timestamp
Browse files Browse the repository at this point in the history
Support rfc3339 micro-sec (6 digit) timestamp
  • Loading branch information
k8s-ci-robot committed Jan 7, 2021
2 parents 6267ff3 + 61cf85c commit a8c89e0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 30 deletions.
Expand Up @@ -26,8 +26,6 @@
import java.io.IOException;
import java.net.HttpURLConnection;
import java.time.Duration;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -78,21 +76,6 @@ public LeaderElectorTest(LockType lockType) {
} catch (IOException ex) {
throw new RuntimeException("Couldn't create ApiClient", ex);
}
// Lease resource requires special care with DateTime
if (lockType == LockType.Lease) {
// TODO: switch date-time library so that micro-sec timestamp can be serialized
// in RFC3339
// format w/ correct precision without the hacks

// This formatter is used for Lease resource spec's acquire/renewTime
DateTimeFormatter formatter =
new DateTimeFormatterBuilder()
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSSSS'Z'"))
.appendOptional(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'"))
.toFormatter();

apiClient.setOffsetDateTimeFormat(formatter);
}
this.lockType = lockType;
}

Expand Down
Expand Up @@ -135,15 +135,23 @@ private LeaderElectionRecord getRecordFromLease(V1LeaseSpec lease) {
}

private V1LeaseSpec getLeaseFromRecord(LeaderElectionRecord record) {
return new V1LeaseSpec()
.acquireTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getAcquireTime().getTime()), ZoneOffset.UTC))
.renewTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getRenewTime().getTime()), ZoneOffset.UTC))
.holderIdentity(record.getHolderIdentity())
.leaseDurationSeconds(record.getLeaseDurationSeconds())
.leaseTransitions(record.getLeaderTransitions());
V1LeaseSpec spec =
new V1LeaseSpec()
.holderIdentity(record.getHolderIdentity())
.leaseDurationSeconds(record.getLeaseDurationSeconds())
.leaseTransitions(record.getLeaderTransitions());
if (record.getAcquireTime() != null) {
spec =
spec.acquireTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getAcquireTime().getTime()), ZoneOffset.UTC));
}
if (record.getRenewTime() != null) {
spec =
spec.renewTime(
OffsetDateTime.ofInstant(
Instant.ofEpochMilli(record.getRenewTime().getTime()), ZoneOffset.UTC));
}
return spec;
}
}
23 changes: 21 additions & 2 deletions kubernetes/src/main/java/io/kubernetes/client/openapi/JSON.java
Expand Up @@ -32,6 +32,9 @@
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.Map;
import okio.ByteString;
Expand All @@ -42,11 +45,22 @@ public class JSON {

private boolean isLenientOnJson = false;

private static final DateTimeFormatter RFC3339MICRO_FORMATTER =
new DateTimeFormatterBuilder()
.parseDefaulting(ChronoField.OFFSET_SECONDS, 0)
.append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
.optionalStart()
.appendFraction(ChronoField.NANO_OF_SECOND, 6, 6, true)
.optionalEnd()
.appendLiteral("Z")
.toFormatter();

private DateTypeAdapter dateTypeAdapter = new DateTypeAdapter();

private SqlDateTypeAdapter sqlDateTypeAdapter = new SqlDateTypeAdapter();

private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter = new OffsetDateTimeTypeAdapter();
private OffsetDateTimeTypeAdapter offsetDateTimeTypeAdapter =
new OffsetDateTimeTypeAdapter(RFC3339MICRO_FORMATTER);

private LocalDateTypeAdapter localDateTypeAdapter = new LocalDateTypeAdapter();

Expand Down Expand Up @@ -231,7 +245,12 @@ public OffsetDateTime read(JsonReader in) throws IOException {
if (date.endsWith("+0000")) {
date = date.substring(0, date.length() - 5) + "Z";
}
return OffsetDateTime.parse(date, formatter);
try {
return OffsetDateTime.parse(date, formatter);
} catch (DateTimeParseException e) {
// backward-compatibility for ISO8601 timestamp format
return OffsetDateTime.parse(date, DateTimeFormatter.ISO_OFFSET_DATE_TIME);
}
}
}
}
Expand Down
Expand Up @@ -15,14 +15,16 @@
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;

import java.time.OffsetDateTime;
import okio.ByteString;
import org.junit.Test;

public class JSONTest {

private final JSON json = new JSON();

@Test
public void testSerializeByteArray() {
final JSON json = new JSON();
final String plainText = "string that contains '=' when encoded";
final String base64String = json.serialize(plainText.getBytes());
// serialize returns string surrounded by quotes: "\"[base64]\""
Expand All @@ -36,4 +38,39 @@ public void testSerializeByteArray() {
final String decodedText = new String(byteStr.toByteArray());
assertThat(decodedText, is(plainText));
}

@Test
public void testOffsetDateTime1e6Parse() {
String timeStr = "\"2018-04-03T11:32:26.123456Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
assertEquals(timeStr, serializedTsStr);
}

@Test
public void testOffsetDateTime1e4Parse() {
String timeStr = "\"2018-04-03T11:32:26.1234Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
String expectedStr = "\"2018-04-03T11:32:26.123400Z\"";
assertEquals(expectedStr, serializedTsStr);
}

@Test
public void testOffsetDateTime1e3Parse() {
String timeStr = "\"2018-04-03T11:32:26.123Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
String expectedStr = "\"2018-04-03T11:32:26.123000Z\"";
assertEquals(expectedStr, serializedTsStr);
}

@Test
public void testOffsetDateTimeNoFractionParse() {
String timeStr = "\"2018-04-03T11:32:26Z\"";
OffsetDateTime dateTime = json.deserialize(timeStr, OffsetDateTime.class);
String serializedTsStr = json.serialize(dateTime);
String expectedStr = "\"2018-04-03T11:32:26.000000Z\"";
assertEquals(expectedStr, serializedTsStr);
}
}

0 comments on commit a8c89e0

Please sign in to comment.