Skip to content

Add support for Asctime date format #1548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ public Instant convert(String value, SdkField<Instant> field) {
return safeParseDate(DateUtils::parseUnixTimestampMillisInstant).apply(value);
case RFC_822:
return DateUtils.parseRfc1123Date(value);
case ASC_TIME:
return DateUtils.parseAsctimeDate(value);
default:
throw SdkClientException.create("Unrecognized timestamp format - " + format);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public enum Format {
*/
RFC_822,

/**
* See {@link DateUtils#parseAsctimeDate(String)}
*/
ASC_TIME,

/**
* See {@link DateUtils#parseUnixTimestampInstant(String)}
*/
Expand All @@ -80,6 +85,8 @@ public static Format fromString(String strFormat) {
return ISO_8601;
case "rfc822":
return RFC_822;
case "asctime":
return ASC_TIME;
case "unixTimestamp":
return UNIX_TIMESTAMP;
// UNIX_TIMESTAMP_MILLIS does not have a defined string format so intentionally omitted here.
Expand Down
38 changes: 38 additions & 0 deletions utils/src/main/java/software/amazon/awssdk/utils/DateUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ public final class DateUtils {
.toFormatter()
.withZone(UTC);

/**
* Asctime format.
*/
static final DateTimeFormatter ASCTIME_DATE_FORMAT =
new DateTimeFormatterBuilder()
.appendPattern("EEE MMM dd HH:mm:ss 'UTC' yyyy")
.toFormatter()
.withZone(UTC);

private static final int MILLI_SECOND_PRECISION = 3;

private DateUtils() {
Expand Down Expand Up @@ -113,6 +122,35 @@ public static String formatRfc1123Date(Instant instant) {
return RFC_1123_DATE_TIME.format(ZonedDateTime.ofInstant(instant, UTC));
}

/**
* Parses the specified date string as an Asctime date and returns the Date
* object.
*
* @param dateString
* The date string to parse.
*
* @return The parsed Date object.
*/
public static Instant parseAsctimeDate(String dateString) {
if (dateString == null) {
return null;
}

return parseInstant(dateString, ASCTIME_DATE_FORMAT);
}

/**
* Formats the specified date as an Asctime string.
*
* @param instant
* The instant to format.
*
* @return The Asctime string representing the specified date.
*/
public static String formatAsctimeDate(Instant instant) {
return ASCTIME_DATE_FORMAT.format(ZonedDateTime.ofInstant(instant, UTC));
}

/**
* Returns the number of days since epoch with respect to the given number
* of milliseconds since epoch.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static software.amazon.awssdk.utils.DateUtils.ALTERNATE_ISO_8601_DATE_FORMAT;
import static software.amazon.awssdk.utils.DateUtils.ASCTIME_DATE_FORMAT;

import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand All @@ -42,10 +43,13 @@ public class DateUtilsTest {
new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
private static final SimpleDateFormat LONG_DATE_FORMAT =
new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z", Locale.US);
private static final SimpleDateFormat ASC_TIME_FORMAT =
new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);

static {
COMMON_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(UTC));
LONG_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone(UTC));
ASC_TIME_FORMAT.setTimeZone(TimeZone.getTimeZone(UTC));
}

private static final Instant INSTANT = Instant.ofEpochMilli(1400284606000L);
Expand Down Expand Up @@ -88,6 +92,14 @@ public void parseRfc822Date() throws ParseException {
assertEquals(expected, actual);
}

@Test
public void parseAsctimeDate() throws ParseException {
String formatted = LONG_DATE_FORMAT.format(Date.from(INSTANT));
Instant expected = LONG_DATE_FORMAT.parse(formatted).toInstant();
Instant actual = DateUtils.parseRfc1123Date(formatted);
assertEquals(expected, actual);
}

@Test
public void parseIso8601Date() throws ParseException {
checkParsing(DateTimeFormatter.ISO_INSTANT, COMMON_DATE_FORMAT);
Expand All @@ -98,6 +110,11 @@ public void parseIso8601Date_usingAlternativeFormat() throws ParseException {
checkParsing(ALTERNATE_ISO_8601_DATE_FORMAT, COMMON_DATE_FORMAT);
}

@Test
public void parseAsctimeFormat() throws ParseException {
checkAsctimeParsing(ASCTIME_DATE_FORMAT, ASC_TIME_FORMAT);
}

private void checkParsing(DateTimeFormatter dateTimeFormatter, SimpleDateFormat dateFormat) throws ParseException {
String formatted = dateFormat.format(Date.from(INSTANT));
String alternative = dateTimeFormatter.format(INSTANT);
Expand All @@ -107,6 +124,15 @@ private void checkParsing(DateTimeFormatter dateTimeFormatter, SimpleDateFormat
assertEquals(expected, actualDate);
}

private void checkAsctimeParsing(DateTimeFormatter dateTimeFormatter, SimpleDateFormat dateFormat) throws ParseException {
String formatted = dateFormat.format(Date.from(INSTANT));
String alternative = dateTimeFormatter.format(INSTANT);
assertEquals(formatted, alternative);
Instant expected = dateFormat.parse(formatted).toInstant();
Instant actualDate = DateUtils.parseAsctimeDate(formatted);
assertEquals(expected, actualDate);
}

@Test
public void alternateIso8601DateFormat() throws ParseException {
String expected = COMMON_DATE_FORMAT.format(Date.from(INSTANT));
Expand Down