Skip to content

Commit

Permalink
Merge branch 'develop' into bugfix/#3156_mods_map_saving_create_game
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheikah45 authored May 31, 2024
2 parents 670889f + 5e27dbb commit 6546b8c
Show file tree
Hide file tree
Showing 5 changed files with 212 additions and 13 deletions.
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.

4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ tasks.register('buildInstall4jMediaFiles', Install4jTask) {

tasks.register('downloadIceAdapter', Download) {
group "native dependencies"
src "https://github.com/FAForever/java-ice-adapter/releases/download/${iceAdapterVersion}/faf-ice-adapter-${iceAdapterVersion}-nojfx.jar"
src "https://github.com/FAForever/java-ice-adapter/releases/download/${iceAdapterVersion}/faf-ice-adapter.jar"
dest file("${buildDir}/resources/native/faf-ice-adapter.jar")
onlyIfNewer true
}
Expand Down Expand Up @@ -245,7 +245,7 @@ dependencies {
implementation("com.neovisionaries:nv-i18n:1.29")
implementation("com.nativelibs4java:bridj:0.7.0")
implementation("org.luaj:luaj-jse:3.0.1")
implementation("commons-validator:commons-validator:1.8.0") {
implementation("commons-validator:commons-validator:1.9.0") {
exclude module: 'commons-logging'
}
implementation("com.github.micheljung:JJsonRpc:01a7fba5f4")
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
version=unspecified
javafxPlatform=unspecified
iceAdapterVersion=v3.3.8
iceAdapterVersion=v2.4.1
55 changes: 45 additions & 10 deletions src/main/java/com/faforever/client/util/TimeService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@ public class TimeService {
private final ChatPrefs chatPrefs;
private final LocalizationPrefs localizationPrefs;

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

private static final DateTimeFormatter FORMATTER_MDY_SHORT = DateTimeFormatter.ofPattern("M/d/yy");
private static final DateTimeFormatter FORMATTER_MDY_MEDIUM = DateTimeFormatter.ofPattern("MMM d, yyyy");
private static final DateTimeFormatter FORMATTER_MDY_LONG = DateTimeFormatter.ofPattern("MMMM d, yyyy");
private static final DateTimeFormatter FORMATTER_MDY_FULL = DateTimeFormatter.ofPattern("EEEE, MMMM d, yyyy");

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 +57,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 -> DateTimeFormatter.ofLocalizedDate(formatStyle);
case DAY_MONTH_YEAR -> getDMYFormatter(formatStyle);
case MONTH_DAY_YEAR -> 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 All @@ -80,6 +97,24 @@ private Locale getCurrentDateLocale() {

}

private 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;
};
}

private 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;
};
}

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

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

@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;

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

when(localizationPrefs.getDateFormat()).thenReturn(dateInfo);
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(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(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(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(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(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(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(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(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);
}
}

0 comments on commit 6546b8c

Please sign in to comment.