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.

49 changes: 46 additions & 3 deletions src/main/java/com/faforever/client/util/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,24 @@ public String asDate(TemporalAccessor temporalAccessor, FormatStyle formatStyle)
if (temporalAccessor == null) {
return i18n.get("noDateAvailable");
}
return DateTimeFormatter.ofLocalizedDate(formatStyle)
.withLocale(getCurrentDateLocale())

DateInfo dateInfo = localizationPrefs.getDateFormat();
DateTimeFormatter formatter;

if (dateInfo.equals(DateInfo.DAY_MONTH_YEAR)) {
formatter = DateTimeFormatter.ofPattern(getDatePatternForDayMonthYear(formatStyle));
} else if (dateInfo.equals(DateInfo.MONTH_DAY_YEAR)) {
formatter = DateTimeFormatter.ofPattern(getDatePatternForMonthDayYear(formatStyle));
} else {
formatter = DateTimeFormatter.ofLocalizedDate(formatStyle);
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved

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


public String asShortTime(Temporal temporal) {
if (temporal == null) {
return "";
Expand All @@ -80,6 +91,38 @@ private Locale getCurrentDateLocale() {

}

private String getDatePatternForDayMonthYear(FormatStyle style) {
switch (style) {
case SHORT -> {
return "d/M/yy";
}
case MEDIUM -> {
return "d MMM, yyyy";
}
default -> {
// for other styles extend this
return "d MMMM, yyyy";
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved
}
}


private String getDatePatternForMonthDayYear(FormatStyle style) {
switch (style) {
case SHORT -> {
return "M/d/yy";
}
case MEDIUM -> {
return "MMM d, yyyy";
}
default -> {
// for other styles extend this
return "MMMM d, yyyy";
}
sulikdan marked this conversation as resolved.
Show resolved Hide resolved
}
}


/**
* Returns the localized minutes and seconds (e.g. '20min 31s'), or hours and minutes (e.g. '1h 5min') of the
* specified duration.
Expand Down
89 changes: 89 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,89 @@
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;

@InjectMocks
TimeService service;

@BeforeEach
void setUp() {

}

@Test
void asDateAuto() {
var date = generateDate();
var dateInfo = DateInfo.AUTO;
var locale = Locale.of("en", "US");

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(i18n.getUserSpecificLocale()).thenReturn(locale);

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

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

@Test
void asDateMonthDayYear() {
var date = generateDate();
var dateInfo = DateInfo.MONTH_DAY_YEAR;
var locale = Locale.of("en", "US");

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(i18n.getUserSpecificLocale()).thenReturn(locale);

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

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

@Test
void asDateDayMonthYear() {
var date = generateDate();
var dateInfo = DateInfo.DAY_MONTH_YEAR;
var locale = Locale.of("en", "US");

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
when(i18n.getUserSpecificLocale()).thenReturn(locale);

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

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


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