From 0ddda27384ac206c5cea69dbf1d2e2e1486b129d Mon Sep 17 00:00:00 2001 From: Aurora Lahtela <24460436+AuroraLS3@users.noreply.github.com> Date: Sat, 21 Jan 2023 15:22:04 +0200 Subject: [PATCH] Implement a date formatter for Last-Modified header https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified --- .../plan/delivery/formatting/Formatters.java | 6 +++ .../formatting/time/DateFormatter.java | 10 ++++- .../time/HttpLastModifiedDateFormatter.java | 42 +++++++++++++++++++ .../HttpLastModifiedDateFormatterTest.java | 40 ++++++++++++++++++ 4 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatter.java create mode 100644 Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatterTest.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java index 074ebc5fb..93ac6208e 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/Formatters.java @@ -43,6 +43,7 @@ public class Formatters { private final DayFormatter dayLongFormatter; private final SecondFormatter secondLongFormatter; private final ClockFormatter clockLongFormatter; + private final HttpLastModifiedDateFormatter httpLastModifiedDateFormatter; private final JavascriptDateFormatter javascriptDateFormatter; private final ISO8601NoClockFormatter iso8601NoClockLongFormatter; private final ISO8601NoClockTZIndependentFormatter iso8601NoClockTZIndependentFormatter; @@ -59,6 +60,7 @@ public class Formatters { dayLongFormatter = new DayFormatter(config, locale); clockLongFormatter = new ClockFormatter(config, locale); secondLongFormatter = new SecondFormatter(config, locale); + httpLastModifiedDateFormatter = new HttpLastModifiedDateFormatter(config, locale); javascriptDateFormatter = new JavascriptDateFormatter(config, locale); iso8601NoClockLongFormatter = new ISO8601NoClockFormatter(config, locale); iso8601NoClockTZIndependentFormatter = new ISO8601NoClockTZIndependentFormatter(); @@ -122,6 +124,10 @@ public class Formatters { return iso8601NoClockFormatter; } + public Formatter httpLastModifiedLong() { + return httpLastModifiedDateFormatter; + } + public Formatter javascriptDateFormatterLong() { return javascriptDateFormatter; } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/DateFormatter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/DateFormatter.java index 6d2af7d94..347b634c0 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/DateFormatter.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/DateFormatter.java @@ -47,8 +47,16 @@ public abstract class DateFormatter implements Formatter { java.util.Locale usedLocale = "default".equalsIgnoreCase(localeSetting) ? java.util.Locale.ENGLISH : java.util.Locale.forLanguageTag(localeSetting); - SimpleDateFormat dateFormat = new SimpleDateFormat(format, usedLocale); + return format(epochMs, format, usedLocale); + } + + protected String format(long epochMs, String format, java.util.Locale usedLocale) { TimeZone timeZone = config.getTimeZone(); + return format(epochMs, format, usedLocale, timeZone); + } + + protected String format(long epochMs, String format, java.util.Locale usedLocale, TimeZone timeZone) { + SimpleDateFormat dateFormat = new SimpleDateFormat(format, usedLocale); dateFormat.setTimeZone(timeZone); return dateFormat.format(epochMs); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatter.java b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatter.java new file mode 100644 index 000000000..7ccf6034e --- /dev/null +++ b/Plan/common/src/main/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatter.java @@ -0,0 +1,42 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.formatting.time; + +import com.djrapitops.plan.settings.config.PlanConfig; +import com.djrapitops.plan.settings.locale.Locale; + +import java.time.ZoneId; +import java.util.TimeZone; + +/** + * Formats timestamps to the Last-Modified header time format. + * https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified + * + * @author AuroraLS3 + */ +public class HttpLastModifiedDateFormatter extends DateFormatter { + + public HttpLastModifiedDateFormatter(PlanConfig config, Locale locale) { + super(config, locale); + } + + @Override + public String apply(Long epochMs) { + return format(epochMs, "E, dd MMM yyyy HH:mm:ss 'GMT'", java.util.Locale.ENGLISH, TimeZone.getTimeZone(ZoneId.of("UTC"))); + } + +} diff --git a/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatterTest.java b/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatterTest.java new file mode 100644 index 000000000..b14b42a97 --- /dev/null +++ b/Plan/common/src/test/java/com/djrapitops/plan/delivery/formatting/time/HttpLastModifiedDateFormatterTest.java @@ -0,0 +1,40 @@ +/* + * This file is part of Player Analytics (Plan). + * + * Plan is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License v3 as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Plan is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Plan. If not, see . + */ +package com.djrapitops.plan.delivery.formatting.time; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class HttpLastModifiedDateFormatterTest { + + HttpLastModifiedDateFormatter underTest = new HttpLastModifiedDateFormatter(null, null); + + @ParameterizedTest + @CsvSource( + value = { + "0;Thu, 01 Jan 1970 00:00:00 GMT", + "1674306986180;Sat, 21 Jan 2023 13:16:26 GMT", + "214704830647000;Sat, 22 Sep 8773 14:44:07 GMT" + }, delimiterString = ";" + ) + void formatIsCorrect(long date, String expected) { + String result = underTest.apply(date); + assertEquals(expected, result); + } +} \ No newline at end of file