Skip to content

Commit

Permalink
fix: Replace Date with LocalDateTime (#1012)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Aug 18, 2022
1 parent 1256db2 commit 765dd89
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 42 deletions.
Expand Up @@ -16,25 +16,22 @@

package com.google.cloud.logging;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import static java.time.ZoneOffset.UTC;
import static java.util.Locale.US;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

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

private Date yesterday() {
TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);

return calendar.getTime();
private LocalDateTime yesterday() {
return LocalDateTime.now(UTC).minus(Duration.ofDays(1));
}
}
Expand Up @@ -16,13 +16,14 @@

package com.google.cloud.logging;

import static java.time.ZoneOffset.UTC;

import com.google.api.gax.paging.Page;
import com.google.cloud.MonitoredResource;
import com.google.cloud.logging.testing.RemoteLoggingHelper;
import com.google.common.collect.Iterators;
import com.google.common.collect.Iterables;
import com.google.logging.v2.LogName;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Iterator;
import org.junit.AfterClass;
Expand All @@ -38,7 +39,8 @@ public class BaseSystemTest {

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

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

protected static Logging logging;

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

protected static boolean cleanupLog(String logName) throws InterruptedException {
Expand All @@ -84,7 +86,9 @@ protected static boolean cleanupLog(String logName) throws InterruptedException
protected static String createTimestampFilter(int hoursAgo) {
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.HOUR, -1 * hoursAgo);
return "timestamp>=\"" + RFC_3339.format(calendar.getTime()) + "\"";
return "timestamp>=\""
+ calendar.getTime().toInstant().atZone(UTC).toLocalDateTime().format(RFC_3339)
+ "\"";
}

protected static String appendResourceTypeFilter(
Expand Down Expand Up @@ -133,7 +137,7 @@ protected static Iterator<LogEntry> waitForLogs(LogName logName) throws Interrup
protected static Iterator<LogEntry> waitForLogs(Logging.EntryListOption[] options, int minLogs)
throws InterruptedException {
Page<LogEntry> page = logging.listLogEntries(options);
while (Iterators.size(page.iterateAll().iterator()) < minLogs) {
while (Iterables.size(page.iterateAll()) < minLogs) {
Thread.sleep(500);
page = logging.listLogEntries(options);
}
Expand Down
Expand Up @@ -16,15 +16,13 @@

package com.google.cloud.logging;

import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static com.google.common.truth.Truth.assertThat;
import static java.time.ZoneOffset.UTC;
import static java.util.Locale.US;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import javax.management.timer.Timer;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.Test;

public class TimestampDefaultFilterTest {
Expand All @@ -33,23 +31,20 @@ public class TimestampDefaultFilterTest {
public void DefaultTimestampFilterTest() {
ITimestampDefaultFilter filter = new TimestampDefaultFilter();

TimeZone timeZone = TimeZone.getTimeZone("UTC");
Calendar calendar = Calendar.getInstance(timeZone);
calendar.add(Calendar.DATE, -1);
Date expected = calendar.getTime();

// Timestamp filter exists
String defaultFilter = filter.createDefaultTimestampFilter();
assertTrue(defaultFilter.contains("timestamp>="));
assertThat(defaultFilter).contains("timestamp>=");

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

0 comments on commit 765dd89

Please sign in to comment.