Skip to content
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

Leaderboard dates using format and language #3183

2 changes: 2 additions & 0 deletions .idea/runConfigurations/Main.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions src/main/java/com/faforever/client/util/DateFormatterUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.faforever.client.util;

import lombok.NoArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;

@Service
@NoArgsConstructor
public class DateFormatterUtil {

public static final DateTimeFormatter FORMATTER_AUTO_SHORT = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT);
public static final DateTimeFormatter FORMATTER_AUTO_MEDIUM = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
public static final DateTimeFormatter FORMATTER_AUTO_LONG = DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG);
public static final DateTimeFormatter FORMATTER_AUTO_FULL = DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL);

public static final DateTimeFormatter FORMATTER_DMY_SHORT = DateTimeFormatter.ofPattern("d/M/yy");
public static final DateTimeFormatter FORMATTER_DMY_MEDIUM = DateTimeFormatter.ofPattern("d MMM, yyyy");
public static final DateTimeFormatter FORMATTER_DMY_LONG = DateTimeFormatter.ofPattern("d MMMM, yyyy");
public static final DateTimeFormatter FORMATTER_DMY_FULL = DateTimeFormatter.ofPattern("EEEE, d MMMM, yyyy");

public static final DateTimeFormatter FORMATTER_MDY_SHORT = DateTimeFormatter.ofPattern("M/d/yy");
public static final DateTimeFormatter FORMATTER_MDY_MEDIUM = DateTimeFormatter.ofPattern("MMM d, yyyy");
public static final DateTimeFormatter FORMATTER_MDY_LONG = DateTimeFormatter.ofPattern("MMMM d, yyyy");
public static final DateTimeFormatter FORMATTER_MDY_FULL = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy");
sulikdan marked this conversation as resolved.
Show resolved Hide resolved


public DateTimeFormatter getAutoFormatter(FormatStyle style) {
return switch (style) {
case SHORT -> FORMATTER_AUTO_SHORT;
case MEDIUM -> FORMATTER_AUTO_MEDIUM;
case LONG -> FORMATTER_AUTO_LONG;
case FULL -> FORMATTER_AUTO_FULL;
};
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

public DateTimeFormatter getDMYFormatter(FormatStyle style) {
return switch (style) {
case SHORT -> FORMATTER_DMY_SHORT;
case MEDIUM -> FORMATTER_DMY_MEDIUM;
case LONG -> FORMATTER_DMY_LONG;
case FULL -> FORMATTER_DMY_FULL;
};
}

public DateTimeFormatter getMDYFormatter(FormatStyle style) {
return switch (style) {
case SHORT -> FORMATTER_MDY_SHORT;
case MEDIUM -> FORMATTER_MDY_MEDIUM;
case LONG -> FORMATTER_MDY_LONG;
case FULL -> FORMATTER_MDY_FULL;
};
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

}
28 changes: 18 additions & 10 deletions src/main/java/com/faforever/client/util/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@ public class TimeService {
private final I18n i18n;
private final ChatPrefs chatPrefs;
private final LocalizationPrefs localizationPrefs;
private final DateFormatterUtil dateFormatterUtil;

public String asDateTime(TemporalAccessor temporalAccessor) {
if (temporalAccessor == null) {
return i18n.get("noDateAvailable");
}
return DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)
.withLocale(getCurrentDateLocale())
.withZone(TimeZone.getDefault().toZoneId())
.format(temporalAccessor);
.withLocale(getCurrentDateLocale())
.withZone(TimeZone.getDefault().toZoneId())
.format(temporalAccessor);
}

public String asDate(TemporalAccessor temporalAccessor) {
Expand All @@ -47,20 +48,27 @@ public String asDate(TemporalAccessor temporalAccessor, FormatStyle formatStyle)
if (temporalAccessor == null) {
return i18n.get("noDateAvailable");
}
return DateTimeFormatter.ofLocalizedDate(formatStyle)
.withLocale(getCurrentDateLocale())
.withZone(TimeZone.getDefault().toZoneId())
.format(temporalAccessor);

DateInfo dateInfo = localizationPrefs.getDateFormat();
DateTimeFormatter formatter = switch (dateInfo) {
case AUTO -> dateFormatterUtil.getAutoFormatter(formatStyle);
case DAY_MONTH_YEAR -> dateFormatterUtil.getDMYFormatter(formatStyle);
case MONTH_DAY_YEAR -> dateFormatterUtil.getMDYFormatter(formatStyle);
};

return formatter.withLocale(i18n.getUserSpecificLocale())
.withZone(TimeZone.getDefault().toZoneId())
.format(temporalAccessor);
}


public String asShortTime(Temporal temporal) {
if (temporal == null) {
return "";
}
return DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)
.withLocale(getCurrentTimeLocale())
.format(ZonedDateTime.ofInstant(Instant.from(temporal), TimeZone.getDefault().toZoneId()));
.withLocale(getCurrentTimeLocale())
.format(ZonedDateTime.ofInstant(Instant.from(temporal), TimeZone.getDefault().toZoneId()));
}

private Locale getCurrentTimeLocale() {
Expand Down
180 changes: 180 additions & 0 deletions src/test/java/com/faforever/client/util/TimeServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
package com.faforever.client.util;

import com.faforever.client.i18n.I18n;
import com.faforever.client.preferences.ChatPrefs;
import com.faforever.client.preferences.DateInfo;
import com.faforever.client.preferences.LocalizationPrefs;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.format.FormatStyle;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

@Execution(ExecutionMode.CONCURRENT)
@ExtendWith({MockitoExtension.class})
class TimeServiceTest {
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

@Mock
I18n i18n;
@Mock
ChatPrefs chatPrefs;
@Mock
LocalizationPrefs localizationPrefs;

@Mock
DateFormatterUtil dateFormatterUtil;

@InjectMocks
TimeService service;

private final Locale localeUS = Locale.of("en", "US");
private final Locale localeFR = Locale.of("fr", "FR");
private final FormatStyle formatStyleMedium = FormatStyle.MEDIUM;

@BeforeEach
void setUp() {

}

@Test
void asDateAutoUS() {
var date = generateDate();
var dateInfo = DateInfo.AUTO;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getAutoFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_AUTO_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeUS);

var result = service.asDate(date, formatStyleMedium);

assertEquals("May 9, 2022", result);
}

@Test
void asDateMonthDayYearUS() {
var date = generateDate();
var dateInfo = DateInfo.MONTH_DAY_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getMDYFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_MDY_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeUS);

var result = service.asDate(date, formatStyleMedium);

assertEquals("May 9, 2022", result);
}

@Test
void asDateDayMonthYearUS() {
var date = generateDate();
var dateInfo = DateInfo.DAY_MONTH_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getDMYFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_DMY_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeUS);

var result = service.asDate(date, formatStyleMedium);

assertEquals("9 May, 2022", result);
}

@Test
void asDateAutoFR() {
var date = generateDate();
var dateInfo = DateInfo.AUTO;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getAutoFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_AUTO_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeFR);

var result = service.asDate(date, formatStyleMedium);

assertEquals("9 mai 2022", result);
}

@Test
void asDateMonthDayYearFR() {
var date = generateDate();
var dateInfo = DateInfo.MONTH_DAY_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getMDYFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_MDY_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeFR);

var result = service.asDate(date, formatStyleMedium);

assertEquals("mai 9, 2022", result);
}

@Test
void asDateDayMonthYearFR() {
var date = generateDate();
var dateInfo = DateInfo.DAY_MONTH_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getDMYFormatter(formatStyleMedium)).thenReturn(DateFormatterUtil.FORMATTER_DMY_MEDIUM);
when(i18n.getUserSpecificLocale()).thenReturn(localeFR);

var result = service.asDate(date, formatStyleMedium);

assertEquals("9 mai, 2022", result);
}

@Test
void asDateAutoFullUS() {
var date = generateDate();
var dateInfo = DateInfo.AUTO;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getAutoFormatter(FormatStyle.FULL)).thenReturn(DateFormatterUtil.FORMATTER_AUTO_FULL);
when(i18n.getUserSpecificLocale()).thenReturn(localeUS);

var result = service.asDate(date, FormatStyle.FULL);

assertEquals("Monday, May 9, 2022", result);
}

@Test
void asDateDMYFullUS() {
var date = generateDate();
var dateInfo = DateInfo.DAY_MONTH_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getDMYFormatter(FormatStyle.FULL)).thenReturn(DateFormatterUtil.FORMATTER_DMY_FULL);
when(i18n.getUserSpecificLocale()).thenReturn(localeUS);

var result = service.asDate(date, FormatStyle.FULL);

assertEquals("Monday, 9 May, 2022", result);
}

@Test
void asDateDMYFullFR() {
var date = generateDate();
var dateInfo = DateInfo.DAY_MONTH_YEAR;

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(dateFormatterUtil.getDMYFormatter(FormatStyle.FULL)).thenReturn(DateFormatterUtil.FORMATTER_DMY_FULL);
when(i18n.getUserSpecificLocale()).thenReturn(localeFR);

var result = service.asDate(date, FormatStyle.FULL);

assertEquals("lundi, 9 mai, 2022", result);
}


private OffsetDateTime generateDate() {
return OffsetDateTime.of(2022, 5, 9, 9, 9, 9, 9, ZoneOffset.UTC);
}
}