Skip to content

Commit

Permalink
Adding tests for .format()
Browse files Browse the repository at this point in the history
  • Loading branch information
joakime committed Apr 23, 2024
1 parent 20660bc commit 2b67f05
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@

import java.nio.charset.StandardCharsets;
import java.time.DateTimeException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
Expand All @@ -30,6 +28,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.time.ZoneOffset.UTC;

/**
* HTTP Date/Time parsing and formatting.
*
Expand All @@ -47,8 +47,6 @@
public class HttpDateTime
{
private static final Logger LOG = LoggerFactory.getLogger(HttpDateTime.class);
private static final ZoneId GMT = ZoneId.of("GMT");
private static final ZoneId UTC = ZoneOffset.UTC;
private static final Index<Integer> MONTH_CACHE = new Index.Builder<Integer>()
.caseSensitive(false)
// Note: Calendar.Month fields are zero based.
Expand Down Expand Up @@ -244,7 +242,7 @@ else if (token.length() == 4)
public static String format(TemporalAccessor datetime)
{
return DateTimeFormatter.RFC_1123_DATE_TIME
.withZone(GMT)
.withZone(UTC)
.format(datetime);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.eclipse.jetty.http;

import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
Expand Down Expand Up @@ -190,4 +192,23 @@ public void testParseToEpochBeforeEpoch()
long epoch = HttpDateTime.parseToEpoch("Fri, 13 Mar 1964 11:22:33 GMT");
assertThat(epoch, is(-183127047000L));
}

@Test
public void testFormatZonedDateTime()
{
// When "Back to the Future" released
ZonedDateTime zonedDateTime = ZonedDateTime.of(1984, 7, 3, 8, 10, 30, 0, ZoneId.of("US/Pacific"));
String actual = HttpDateTime.format(zonedDateTime);
assertThat(actual, is("Tue, 3 Jul 1984 15:10:30 GMT"));
}

@Test
public void testFormatInstant()
{
// When "Tron" released
long epochMillis = 395054120000L;
Instant instant = Instant.ofEpochMilli(epochMillis);
String actual = HttpDateTime.format(instant);
assertThat(actual, is("Fri, 9 Jul 1982 09:15:20 GMT"));
}
}

0 comments on commit 2b67f05

Please sign in to comment.