forked from unicode-org/conformance
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'unicode-org:main' into main
- Loading branch information
Showing
28 changed files
with
1,161 additions
and
321 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
...n/java/org/unicode/conformance/testtype/datetimeformatter/DateTimeFormatterDateStyle.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package org.unicode.conformance.testtype.datetimeformatter; | ||
|
||
public enum DateTimeFormatterDateStyle { | ||
FULL, | ||
LONG, | ||
MEDIUM, | ||
SHORT; | ||
|
||
public static org.unicode.conformance.testtype.datetimeformatter.DateTimeFormatterDateStyle DEFAULT = MEDIUM; | ||
|
||
public static org.unicode.conformance.testtype.datetimeformatter.DateTimeFormatterDateStyle getFromString( | ||
String s) { | ||
try { | ||
return org.unicode.conformance.testtype.datetimeformatter.DateTimeFormatterDateStyle.valueOf( | ||
s.toUpperCase()); | ||
} catch (Exception e) { | ||
return DEFAULT; | ||
} | ||
} | ||
|
||
} |
37 changes: 37 additions & 0 deletions
37
...n/java/org/unicode/conformance/testtype/datetimeformatter/DateTimeFormatterInputJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.unicode.conformance.testtype.datetimeformatter; | ||
|
||
import java.util.Date; | ||
import com.ibm.icu.util.Calendar; | ||
|
||
import org.unicode.conformance.testtype.ITestTypeInputJson; | ||
|
||
public class DateTimeFormatterInputJson implements ITestTypeInputJson { | ||
|
||
public String testType; | ||
|
||
public String label; | ||
|
||
public String locale; | ||
|
||
// UTC formatted time | ||
public String inputString; | ||
|
||
public Date myDate; | ||
|
||
public String skeleton; | ||
|
||
public DateTimeFormatterDateStyle dateStyle; | ||
|
||
public DateTimeFormatterTimeStyle timeStyle; | ||
|
||
// TODO!!! | ||
public String calendarString; | ||
// Set calendar from calendarString! | ||
public Calendar calendar; | ||
|
||
public String numberingSystem; | ||
|
||
public String timeZone; | ||
|
||
public String timeZoneName; | ||
} |
16 changes: 16 additions & 0 deletions
16
.../java/org/unicode/conformance/testtype/datetimeformatter/DateTimeFormatterOutputJson.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.unicode.conformance.testtype.datetimeformatter; | ||
|
||
import org.unicode.conformance.testtype.ITestTypeOutputJson; | ||
|
||
public class DateTimeFormatterOutputJson implements ITestTypeOutputJson { | ||
|
||
public String test_type; | ||
|
||
public String label; | ||
|
||
public String result; | ||
|
||
public String error; | ||
|
||
public String error_message; | ||
} |
167 changes: 167 additions & 0 deletions
167
...main/java/org/unicode/conformance/testtype/datetimeformatter/DateTimeFormatterTester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
package org.unicode.conformance.testtype.datetimeformatter; | ||
|
||
import java.text.ParseException; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneId; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
import java.util.Date; | ||
|
||
import com.ibm.icu.util.Calendar; | ||
import com.ibm.icu.text.DateFormat; | ||
import com.ibm.icu.util.ULocale; | ||
|
||
import io.lacuna.bifurcan.IMap; | ||
import io.lacuna.bifurcan.Map; | ||
|
||
|
||
import org.unicode.conformance.ExecutorUtils; | ||
import org.unicode.conformance.testtype.ITestType; | ||
import org.unicode.conformance.testtype.ITestTypeInputJson; | ||
import org.unicode.conformance.testtype.ITestTypeOutputJson; | ||
|
||
public class DateTimeFormatterTester implements ITestType { | ||
|
||
public static DateTimeFormatterTester INSTANCE = new DateTimeFormatterTester(); | ||
|
||
@Override | ||
public ITestTypeInputJson inputMapToJson(Map<String, Object> inputMapData) { | ||
DateTimeFormatterInputJson result = new DateTimeFormatterInputJson(); | ||
|
||
result.label = (String) inputMapData.get("label", null); | ||
result.locale = (String) inputMapData.get("locale", null); | ||
result.skeleton = (String) inputMapData.get("skeleton", null); | ||
|
||
result.inputString = (String) inputMapData.get("input_string", null); | ||
|
||
java.util.Map<String, Object> inputOptions = | ||
(java.util.Map<String, Object>) inputMapData.get("options", null); | ||
|
||
result.timeZone = (String) inputOptions.get("timeZone"); | ||
ZoneId thisZoneId; | ||
if (result.timeZone == null) { | ||
thisZoneId = ZoneId.systemDefault(); | ||
} else { | ||
thisZoneId = ZoneId.of(result.timeZone); | ||
} | ||
|
||
// Extract ISO part of the input string to parse. | ||
String inputStringDateTime = result.inputString.substring(0, 25); | ||
|
||
// For parsing the input string and converting to java.util.date | ||
LocalDateTime parsedLocalDateTime = | ||
LocalDateTime.parse(inputStringDateTime, DateTimeFormatter.ISO_OFFSET_DATE_TIME); | ||
result.myDate = | ||
java.util.Date.from(parsedLocalDateTime.atZone(thisZoneId) | ||
.toInstant()); | ||
|
||
result.dateStyle = DateTimeFormatterDateStyle.getFromString( | ||
"" + inputOptions.get("dateStyle") | ||
); | ||
|
||
result.timeStyle = DateTimeFormatterTimeStyle.getFromString( | ||
"" + inputOptions.get("timeStyle") | ||
); | ||
|
||
result.calendarString = (String) inputOptions.get("calendar"); | ||
|
||
// TODO!!! Get calendar object. Depends on timezone and locale. | ||
// Just a placeholder for now. | ||
result.calendar = Calendar.getInstance(); | ||
|
||
result.numberingSystem = (String) inputOptions.get("numberingSystem"); | ||
|
||
result.timeZoneName = (String) inputOptions.get("timeZoneName"); | ||
|
||
return result; | ||
} | ||
|
||
public ITestTypeOutputJson execute(ITestTypeInputJson inputJson) { | ||
DateTimeFormatterInputJson input = (DateTimeFormatterInputJson) inputJson; | ||
|
||
// partially construct output | ||
DateTimeFormatterOutputJson output = (DateTimeFormatterOutputJson) getDefaultOutputJson(); | ||
output.label = input.label; | ||
|
||
try { | ||
output.result = getDateTimeFormatterResultString(input); | ||
} catch (Exception e) { | ||
output.error = e.getMessage(); | ||
output.error_message = e.getMessage(); | ||
return output; | ||
} | ||
|
||
// If we get here, it's a pass/fail result (supported options and no runtime errors/exceptions) | ||
return output; | ||
} | ||
|
||
@Override | ||
public ITestTypeOutputJson getDefaultOutputJson() { | ||
return new DateTimeFormatterOutputJson(); | ||
} | ||
|
||
public IMap<String, Object> convertOutputToMap(ITestTypeOutputJson outputJson) { | ||
DateTimeFormatterOutputJson output = (DateTimeFormatterOutputJson) outputJson; | ||
return new io.lacuna.bifurcan.Map<String, Object>() | ||
.put("label", output.label) | ||
.put("result", output.result); | ||
} | ||
|
||
@Override | ||
public String formatOutputJson(ITestTypeOutputJson outputJson) { | ||
return ExecutorUtils.GSON.toJson((DateTimeFormatterOutputJson) outputJson); | ||
} | ||
|
||
public String getDateTimeFormatterResultString(DateTimeFormatterInputJson input) { | ||
|
||
ULocale locale = ULocale.forLanguageTag(input.locale); | ||
|
||
int dateStyle; | ||
switch (input.dateStyle) { | ||
case FULL: | ||
dateStyle = DateFormat.FULL; | ||
break; | ||
case LONG: | ||
dateStyle = DateFormat.LONG; | ||
break; | ||
default: | ||
case MEDIUM: | ||
dateStyle = DateFormat.MEDIUM; | ||
break; | ||
case SHORT: | ||
dateStyle = DateFormat.SHORT; | ||
break; | ||
} | ||
|
||
int timeStyle; | ||
switch (input.timeStyle) { | ||
case FULL: | ||
timeStyle = DateFormat.FULL; | ||
break; | ||
case LONG: | ||
timeStyle = DateFormat.LONG; | ||
break; | ||
default: | ||
case MEDIUM: | ||
timeStyle = DateFormat.MEDIUM; | ||
break; | ||
case SHORT: | ||
timeStyle = DateFormat.SHORT; | ||
break; | ||
|
||
} | ||
|
||
// Get calendar and timezone as needed. | ||
Calendar cal = input.calendar; | ||
|
||
DateFormat dtf; | ||
if (input.skeleton != null) { | ||
dtf = DateFormat.getInstanceForSkeleton(cal, input.skeleton, locale); | ||
} else { | ||
dtf = DateFormat.getDateTimeInstance(cal, dateStyle, timeStyle, locale); | ||
} | ||
|
||
return dtf.format(input.myDate); | ||
} | ||
|
||
} |
Oops, something went wrong.