Skip to content

Commit 765dd89

Browse files
bpcreechBen Creechgcf-owl-bot[bot]
authored
fix: Replace Date with LocalDateTime (#1012)
* Switch from Date to LocalDateTime * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Ben Creech <bpcreech@google.com> Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 1256db2 commit 765dd89

File tree

3 files changed

+38
-42
lines changed

3 files changed

+38
-42
lines changed

google-cloud-logging/src/main/java/com/google/cloud/logging/TimestampDefaultFilter.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,22 @@
1616

1717
package com.google.cloud.logging;
1818

19-
import java.text.DateFormat;
20-
import java.text.SimpleDateFormat;
21-
import java.util.Calendar;
22-
import java.util.Date;
23-
import java.util.TimeZone;
19+
import static java.time.ZoneOffset.UTC;
20+
import static java.util.Locale.US;
21+
22+
import java.time.Duration;
23+
import java.time.LocalDateTime;
24+
import java.time.format.DateTimeFormatter;
2425

2526
public class TimestampDefaultFilter implements ITimestampDefaultFilter {
2627
@Override
2728
public String createDefaultTimestampFilter() {
28-
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
29-
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
29+
DateTimeFormatter rfcDateFormat =
30+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", US);
3031
return "timestamp>=\"" + rfcDateFormat.format(yesterday()) + "\"";
3132
}
3233

33-
private Date yesterday() {
34-
TimeZone timeZone = TimeZone.getTimeZone("UTC");
35-
Calendar calendar = Calendar.getInstance(timeZone);
36-
calendar.add(Calendar.DATE, -1);
37-
38-
return calendar.getTime();
34+
private LocalDateTime yesterday() {
35+
return LocalDateTime.now(UTC).minus(Duration.ofDays(1));
3936
}
4037
}

google-cloud-logging/src/test/java/com/google/cloud/logging/BaseSystemTest.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
package com.google.cloud.logging;
1818

19+
import static java.time.ZoneOffset.UTC;
20+
1921
import com.google.api.gax.paging.Page;
2022
import com.google.cloud.MonitoredResource;
2123
import com.google.cloud.logging.testing.RemoteLoggingHelper;
22-
import com.google.common.collect.Iterators;
24+
import com.google.common.collect.Iterables;
2325
import com.google.logging.v2.LogName;
24-
import java.text.DateFormat;
25-
import java.text.SimpleDateFormat;
26+
import java.time.format.DateTimeFormatter;
2627
import java.util.Calendar;
2728
import java.util.Iterator;
2829
import org.junit.AfterClass;
@@ -38,7 +39,8 @@ public class BaseSystemTest {
3839

3940
@Rule public Timeout globalTimeout = Timeout.seconds(600);
4041

41-
private static DateFormat RFC_3339 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
42+
private static final DateTimeFormatter RFC_3339 =
43+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
4244

4345
protected static Logging logging;
4446

@@ -60,7 +62,7 @@ public static void afterClass() throws Exception {
6062
* Filters Documentation</a>
6163
*/
6264
protected static <V> String createEqualityFilter(String name, V value) {
63-
return name + "=" + "\"" + value.toString() + "\"";
65+
return name + "=" + "\"" + value + "\"";
6466
}
6567

6668
protected static boolean cleanupLog(String logName) throws InterruptedException {
@@ -84,7 +86,9 @@ protected static boolean cleanupLog(String logName) throws InterruptedException
8486
protected static String createTimestampFilter(int hoursAgo) {
8587
Calendar calendar = Calendar.getInstance();
8688
calendar.add(Calendar.HOUR, -1 * hoursAgo);
87-
return "timestamp>=\"" + RFC_3339.format(calendar.getTime()) + "\"";
89+
return "timestamp>=\""
90+
+ calendar.getTime().toInstant().atZone(UTC).toLocalDateTime().format(RFC_3339)
91+
+ "\"";
8892
}
8993

9094
protected static String appendResourceTypeFilter(
@@ -133,7 +137,7 @@ protected static Iterator<LogEntry> waitForLogs(LogName logName) throws Interrup
133137
protected static Iterator<LogEntry> waitForLogs(Logging.EntryListOption[] options, int minLogs)
134138
throws InterruptedException {
135139
Page<LogEntry> page = logging.listLogEntries(options);
136-
while (Iterators.size(page.iterateAll().iterator()) < minLogs) {
140+
while (Iterables.size(page.iterateAll()) < minLogs) {
137141
Thread.sleep(500);
138142
page = logging.listLogEntries(options);
139143
}

google-cloud-logging/src/test/java/com/google/cloud/logging/TimestampDefaultFilterTest.java

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

1717
package com.google.cloud.logging;
1818

19-
import static org.junit.Assert.assertTrue;
20-
import static org.junit.Assert.fail;
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static java.time.ZoneOffset.UTC;
21+
import static java.util.Locale.US;
2122

22-
import java.text.DateFormat;
23-
import java.text.SimpleDateFormat;
24-
import java.util.Calendar;
25-
import java.util.Date;
26-
import java.util.TimeZone;
27-
import javax.management.timer.Timer;
23+
import java.time.Duration;
24+
import java.time.LocalDateTime;
25+
import java.time.format.DateTimeFormatter;
2826
import org.junit.Test;
2927

3028
public class TimestampDefaultFilterTest {
@@ -33,23 +31,20 @@ public class TimestampDefaultFilterTest {
3331
public void DefaultTimestampFilterTest() {
3432
ITimestampDefaultFilter filter = new TimestampDefaultFilter();
3533

36-
TimeZone timeZone = TimeZone.getTimeZone("UTC");
37-
Calendar calendar = Calendar.getInstance(timeZone);
38-
calendar.add(Calendar.DATE, -1);
39-
Date expected = calendar.getTime();
40-
4134
// Timestamp filter exists
4235
String defaultFilter = filter.createDefaultTimestampFilter();
43-
assertTrue(defaultFilter.contains("timestamp>="));
36+
assertThat(defaultFilter).contains("timestamp>=");
4437

4538
// Time is last 24 hours
46-
try {
47-
DateFormat rfcDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
48-
rfcDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
49-
Date actual = rfcDateFormat.parse(defaultFilter.substring(12, defaultFilter.length() - 1));
50-
assertTrue(Math.abs(expected.getTime() - actual.getTime()) < Timer.ONE_MINUTE);
51-
} catch (java.text.ParseException ex) {
52-
fail(); // Just fail if exception is thrown
53-
}
39+
DateTimeFormatter rfcDateFormat =
40+
DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", US);
41+
LocalDateTime actual =
42+
LocalDateTime.parse(defaultFilter.substring(12, defaultFilter.length() - 1), rfcDateFormat);
43+
assertThat(
44+
Duration.between(actual, LocalDateTime.now(UTC))
45+
.minus(Duration.ofDays(1))
46+
.abs()
47+
.compareTo(Duration.ofMinutes(1)))
48+
.isLessThan(0);
5449
}
5550
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy