Added an ISO-8601 Timezone independent formatter

Server calendar had an issue with days going on
the wrong date if timezone was negative GMT.

Fixed the issue by implementing a timezone
independent ISO-8601 formatter to use with the dates on
the session calendar. The date given to ServerCalendar is already
set to midnight on each day.

Affects issues:
- Fixed #1239
This commit is contained in:
Rsl1122 2019-12-04 20:13:40 +02:00
parent 934090947f
commit cbcd2c7996
4 changed files with 58 additions and 12 deletions

View File

@ -43,6 +43,7 @@ public class Formatters {
private final SecondFormatter secondLongFormatter;
private final ClockFormatter clockLongFormatter;
private final ISO8601NoClockFormatter iso8601NoClockLongFormatter;
private final ISO8601NoClockTZIndependentFormatter iso8601NoClockTZIndependentFormatter;
private final TimeAmountFormatter timeAmountFormatter;
@ -56,6 +57,7 @@ public class Formatters {
clockLongFormatter = new ClockFormatter(config, locale);
secondLongFormatter = new SecondFormatter(config, locale);
iso8601NoClockLongFormatter = new ISO8601NoClockFormatter(config, locale);
iso8601NoClockTZIndependentFormatter = new ISO8601NoClockTZIndependentFormatter();
yearFormatter = new DateHolderFormatter(yearLongFormatter);
dayFormatter = new DateHolderFormatter(dayLongFormatter);
@ -110,6 +112,10 @@ public class Formatters {
return iso8601NoClockLongFormatter;
}
public Formatter<Long> iso8601NoClockTZIndependentLong() {
return iso8601NoClockTZIndependentFormatter;
}
public Formatter<Long> timeAmount() {
return timeAmountFormatter;
}

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 com.djrapitops.plan.delivery.formatting.Formatter;
import java.text.SimpleDateFormat;
/**
* Formatter for a timestamp in ISO-8601 format without the clock, without applying timezone offset.
*
* @author Rsl1122
*/
public class ISO8601NoClockTZIndependentFormatter implements Formatter<Long> {
@Override
public String apply(Long date) {
return date > 0 ? format(date) : "-";
}
private String format(Long date) {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
return dateFormat.format(date);
}
}

View File

@ -68,7 +68,7 @@ public class CalendarFactory {
) {
return new ServerCalendar(
uniquePerDay, newPerDay, playtimePerDay, sessionsPerDay,
formatters.iso8601NoClockLong(), formatters.timeAmount(), theme, locale
formatters.iso8601NoClockTZIndependentLong(), formatters.timeAmount(), theme, locale
);
}
}

View File

@ -38,8 +38,8 @@ public class ServerCalendar {
private final SortedMap<Long, Integer> sessionsPerDay;
private final SortedMap<Long, Long> playtimePerDay;
private final Formatter<Long> iso8601Formatter;
private final Formatter<Long> timeAmountFormatter;
private final Formatter<Long> iso8601TZIndependent;
private final Formatter<Long> timeAmount;
private final Theme theme;
private final Locale locale;
@ -48,15 +48,15 @@ public class ServerCalendar {
SortedMap<Long, Integer> newPerDay,
SortedMap<Long, Long> playtimePerDay,
NavigableMap<Long, Integer> sessionsPerDay,
Formatter<Long> iso8601Formatter,
Formatter<Long> timeAmountFormatter,
Formatter<Long> iso8601TZIndependent,
Formatter<Long> timeAmount,
Theme theme,
Locale locale
) {
this.uniquePerDay = uniquePerDay;
this.newPerDay = newPerDay;
this.iso8601Formatter = iso8601Formatter;
this.timeAmountFormatter = timeAmountFormatter;
this.iso8601TZIndependent = iso8601TZIndependent;
this.timeAmount = timeAmount;
this.sessionsPerDay = sessionsPerDay;
this.playtimePerDay = playtimePerDay;
this.theme = theme;
@ -87,7 +87,7 @@ public class ServerCalendar {
}
Long key = entry.getKey();
String day = iso8601Formatter.apply(key);
String day = iso8601TZIndependent.apply(key);
series.append(",{\"title\": \"").append(locale.get(HtmlLang.NEW_CALENDAR)).append(" ").append(newPlayers)
.append("\",\"start\":\"").append(day)
@ -104,7 +104,7 @@ public class ServerCalendar {
}
Long key = entry.getKey();
String day = iso8601Formatter.apply(key);
String day = iso8601TZIndependent.apply(key);
series.append(",{\"title\": \"").append(locale.get(HtmlLang.UNIQUE_CALENDAR)).append(" ").append(uniquePlayers)
.append("\",\"start\":\"").append(day)
@ -120,9 +120,9 @@ public class ServerCalendar {
continue;
}
Long key = entry.getKey();
String day = iso8601Formatter.apply(key);
String day = iso8601TZIndependent.apply(key);
series.append(",{\"title\": \"").append(locale.get(HtmlLang.LABEL_PLAYTIME)).append(": ").append(timeAmountFormatter.apply(playtime))
series.append(",{\"title\": \"").append(locale.get(HtmlLang.LABEL_PLAYTIME)).append(": ").append(timeAmount.apply(playtime))
.append("\",\"start\":\"").append(day)
.append("\",\"color\": \"").append(theme.getValue(ThemeVal.GREEN)).append('"')
.append("}");
@ -136,7 +136,7 @@ public class ServerCalendar {
continue;
}
Long key = entry.getKey();
String day = iso8601Formatter.apply(key);
String day = iso8601TZIndependent.apply(key);
series.append(",{\"title\": \"").append(locale.get(HtmlLang.SIDE_SESSIONS)).append(": ").append(sessionCount)
.append("\",\"start\":\"").append(day)