Implement a date formatter for Last-Modified header

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Last-Modified
This commit is contained in:
Aurora Lahtela 2023-01-21 15:22:04 +02:00
parent 0bce7880cd
commit 0ddda27384
4 changed files with 97 additions and 1 deletions

View File

@ -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<Long> httpLastModifiedLong() {
return httpLastModifiedDateFormatter;
}
public Formatter<Long> javascriptDateFormatterLong() {
return javascriptDateFormatter;
}

View File

@ -47,8 +47,16 @@ public abstract class DateFormatter implements Formatter<Long> {
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);
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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")));
}
}

View File

@ -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 <https://www.gnu.org/licenses/>.
*/
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);
}
}