use of android.icu.text.DateTimePatternGenerator in project j2objc by google.
the class DateTimeGeneratorTest method TestGetSkeletons.
/* Tests the method
* public Map<String, String> getSkeletons(Map<String, String> result)
*/
@Test
public void TestGetSkeletons() {
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getInstance();
// Tests when "if (result == null)" is true
try {
dtpg.getSkeletons(null);
} catch (Exception e) {
errln("DateTimePatternGenerator.getSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a null parameter.");
}
// Tests when "if (result == null)" is false
Map<String, String> mm = new LinkedHashMap<String, String>();
try {
dtpg.getSkeletons(mm);
} catch (Exception e) {
errln("DateTimePatternGenerator.getSkeletons(Map) was suppose to " + "return a new LinkedHashMap for a LinkedHashMap parameter.");
}
}
use of android.icu.text.DateTimePatternGenerator in project j2objc by google.
the class DateTimeGeneratorTest method TestGetFields.
/* Tests the method
* public String getFields(String pattern)
*/
@Test
public void TestGetFields() {
DateTimePatternGenerator dt = DateTimePatternGenerator.getInstance();
String[] cases = { "MMDDYY", "HHMMSS", "", "MM/DD/YYYY HH:MM:SS", "MMDDYY HHMMSS", "HHMMSS MMDDYYYY", "HMS MDY" };
String[] results = { "{Month:N}{Day_Of_Year:N}{Year:N}", "{Hour:N}{Month:N}{Fractional_Second:N}", "", "{Month:N}/{Day_Of_Year:N}/{Year:N} {Hour:N}:{Month:N}:{Fractional_Second:N}", "{Month:N}{Day_Of_Year:N}{Year:N} {Hour:N}{Month:N}{Fractional_Second:N}", "{Hour:N}{Month:N}{Fractional_Second:N} {Month:N}{Day_Of_Year:N}{Year:N}", "{Hour:N}{Month:N}{Fractional_Second:N} {Month:N}{Day_Of_Year:N}{Year:N}" };
for (int i = 0; i < cases.length; i++) {
if (!dt.getFields(cases[i]).equals(results[i])) {
errln("DateTimePatternGenerator.getFields(String) did not " + "not return an expected result when passing " + cases[i] + ". Got " + dt.getFields(cases[i]) + " but expected " + results[i]);
}
}
}
use of android.icu.text.DateTimePatternGenerator in project j2objc by google.
the class DateTimeGeneratorTest method TestEmptyInstance.
@Test
public void TestEmptyInstance() {
DateTimePatternGenerator dtpg = DateTimePatternGenerator.getEmptyInstance();
String skeleton = "GrMMd";
String message = "DTPG getEmptyInstance should not throw exceptions on basic operations and should conform to " + "the example in setAppendItemFormat";
assertEquals(message, "G ├'F7': d┤ ├'F3': MM┤ ├'F1': y┤", dtpg.getBestPattern(skeleton));
dtpg.addPattern("d-MM-yyyy", false, new DateTimePatternGenerator.PatternInfo());
assertEquals(message, "d-MM-y ├'F0': G┤", dtpg.getBestPattern(skeleton));
dtpg.setAppendItemFormat(DateTimePatternGenerator.ERA, "{0}, {1}");
assertEquals(message, "d-MM-y, G", dtpg.getBestPattern(skeleton));
}
use of android.icu.text.DateTimePatternGenerator in project j2objc by google.
the class DateTimeGeneratorTest method TestReplacingZoneString.
@Test
public void TestReplacingZoneString() {
Date testDate = new Date();
TimeZone testTimeZone = TimeZone.getTimeZone("America/New_York");
TimeZone bogusTimeZone = new SimpleTimeZone(1234, "Etc/Unknown");
Calendar calendar = Calendar.getInstance();
ParsePosition parsePosition = new ParsePosition(0);
ULocale[] locales = ULocale.getAvailableLocales();
int count = 0;
for (int i = 0; i < locales.length; ++i) {
// skip the country locales unless we are doing exhaustive tests
if (getExhaustiveness() < 6) {
if (locales[i].getCountry().length() > 0) {
continue;
}
}
count++;
// ticket#6503
if (getExhaustiveness() <= 5 && count % 3 != 0) {
continue;
}
logln(locales[i].toString());
DateTimePatternGenerator dtpgen = DateTimePatternGenerator.getInstance(locales[i]);
for (int style1 = DateFormat.FULL; style1 <= DateFormat.SHORT; ++style1) {
final SimpleDateFormat oldFormat = (SimpleDateFormat) DateFormat.getTimeInstance(style1, locales[i]);
String pattern = oldFormat.toPattern();
// replaceZoneString(pattern, "VVVV");
String newPattern = dtpgen.replaceFieldTypes(pattern, "VVVV");
if (newPattern.equals(pattern)) {
continue;
}
// verify that it roundtrips parsing
SimpleDateFormat newFormat = new SimpleDateFormat(newPattern, locales[i]);
newFormat.setTimeZone(testTimeZone);
String formatted = newFormat.format(testDate);
calendar.setTimeZone(bogusTimeZone);
parsePosition.setIndex(0);
newFormat.parse(formatted, calendar, parsePosition);
if (parsePosition.getErrorIndex() >= 0) {
errln("Failed parse with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted.substring(0, parsePosition.getErrorIndex()) + "{}" + formatted.substring(parsePosition.getErrorIndex()) + "\"");
} else if (!calendar.getTimeZone().getID().equals(testTimeZone.getID())) {
errln("Failed timezone roundtrip with VVVV:\t" + locales[i] + ",\t\"" + pattern + "\",\t\"" + newPattern + "\",\t\"" + formatted + "\",\t" + calendar.getTimeZone().getID() + " != " + testTimeZone.getID());
} else {
logln(locales[i] + ":\t\"" + pattern + "\" => \t\"" + newPattern + "\"\t" + formatted);
}
}
}
}
use of android.icu.text.DateTimePatternGenerator in project j2objc by google.
the class DateTimeGeneratorTest method TestSimple.
@Test
public void TestSimple() {
// some simple use cases
ULocale locale = ULocale.GERMANY;
TimeZone zone = TimeZone.getTimeZone("Europe/Paris");
// make from locale
DateTimePatternGenerator gen = DateTimePatternGenerator.getInstance(locale);
SimpleDateFormat format = new SimpleDateFormat(gen.getBestPattern("MMMddHmm"), locale);
format.setTimeZone(zone);
assertEquals("simple format: MMMddHmm", "14. Okt., 08:58", format.format(sampleDate));
// (a generator can be built from scratch, but that is not a typical use case)
// modify the generator by adding patterns
DateTimePatternGenerator.PatternInfo returnInfo = new DateTimePatternGenerator.PatternInfo();
gen.addPattern("d'. von' MMMM", true, returnInfo);
// the returnInfo is mostly useful for debugging problem cases
format.applyPattern(gen.getBestPattern("MMMMdHmm"));
assertEquals("modified format: MMMdHmm", "14. von Oktober, 08:58", format.format(sampleDate));
// get a pattern and modify it
format = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, locale);
format.setTimeZone(zone);
String pattern = format.toPattern();
assertEquals("full-date", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Sommerzeit", format.format(sampleDate));
// modify it to change the zone.
String newPattern = gen.replaceFieldTypes(pattern, "vvvv");
format.applyPattern(newPattern);
assertEquals("full-date: modified zone", "Donnerstag, 14. Oktober 1999 um 08:58:59 Mitteleurop\u00E4ische Zeit", format.format(sampleDate));
// add test of basic cases
// lang YYYYMMM MMMd MMMdhmm hmm hhmm Full Date-Time
// en Mar 2007 Mar 4 6:05 PM Mar 4 6:05 PM 06:05 PM Sunday, March 4, 2007 6:05:05 PM PT
DateTimePatternGenerator enGen = DateTimePatternGenerator.getInstance(ULocale.ENGLISH);
TimeZone enZone = TimeZone.getTimeZone("Etc/GMT");
SimpleDateFormat enFormat = (SimpleDateFormat) DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL, ULocale.ENGLISH);
enFormat.setTimeZone(enZone);
String[][] tests = { { "yyyyMMMdd", "Oct 14, 1999" }, { "yyyyqqqq", "4th quarter 1999" }, { "yMMMdd", "Oct 14, 1999" }, { "EyyyyMMMdd", "Thu, Oct 14, 1999" }, { "yyyyMMdd", "10/14/1999" }, { "yyyyMMM", "Oct 1999" }, { "yyyyMM", "10/1999" }, { "yyMM", "10/99" }, // narrow format
{ "yMMMMMd", "O 14, 1999" }, // narrow format
{ "EEEEEMMMMMd", "T, O 14" }, { "MMMd", "Oct 14" }, { "MMMdhmm", "Oct 14, 6:58 AM" }, { "EMMMdhmms", "Thu, Oct 14, 6:58:59 AM" }, { "MMdhmm", "10/14, 6:58 AM" }, { "EEEEMMMdhmms", "Thursday, Oct 14, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "yyyyMMMddhhmmss", "Oct 14, 1999, 6:58:59 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "EyyyyMMMddhhmmss", "Thu, Oct 14, 1999, 6:58:59 AM" }, { "hmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "hhmm", "6:58 AM" }, // (fixed expected result per ticket 6872<-7180)
{ "hhmmVVVV", "6:58 AM GMT" } };
for (int i = 0; i < tests.length; ++i) {
final String testSkeleton = tests[i][0];
String pat = enGen.getBestPattern(testSkeleton);
enFormat.applyPattern(pat);
String formattedDate = enFormat.format(sampleDate);
assertEquals("Testing skeleton '" + testSkeleton + "' with " + sampleDate, tests[i][1], formattedDate);
}
}
Aggregations