Fixes date/time formatting unit tests

This commit is contained in:
konsoletyper 2014-09-09 20:40:12 +04:00
parent c478e965e4
commit 48faf88cc6
2 changed files with 43 additions and 46 deletions

View File

@ -32,41 +32,41 @@ public class DateFormatTest {
@Test @Test
public void shortDateFormatHandled() throws ParseException { public void shortDateFormatHandled() throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH); DateFormat format = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ENGLISH);
assertEquals("6/24/14", format.format(getDateWithZoneOffset(1403539200000L))); assertEquals("6/23/14", format.format(getDateWithZoneOffset(1403481600000L)));
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("6/24/14"))); assertEquals(1403481600000L, getTimeWithoutZoneOffset(format.parse("6/23/14")));
} }
@Test @Test
public void mediumDateFormatHandled() throws ParseException { public void mediumDateFormatHandled() throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH); DateFormat format = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.ENGLISH);
assertEquals("Jun 24, 2014", format.format(getDateWithZoneOffset(1403539200000L))); assertEquals("Jun 23, 2014", format.format(getDateWithZoneOffset(1403481600000L)));
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("Jun 24, 2014"))); assertEquals(1403481600000L, getTimeWithoutZoneOffset(format.parse("Jun 23, 2014")));
} }
@Test @Test
public void longDateFormatHandled() throws ParseException { public void longDateFormatHandled() throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH); DateFormat format = DateFormat.getDateInstance(DateFormat.LONG, Locale.ENGLISH);
assertEquals("June 24, 2014", format.format(getDateWithZoneOffset(1403539200000L))); assertEquals("June 23, 2014", format.format(getDateWithZoneOffset(1403481600000L)));
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("June 24, 2014"))); assertEquals(1403481600000L, getTimeWithoutZoneOffset(format.parse("June 23, 2014")));
} }
@Test @Test
public void fullDateFormatHandled() throws ParseException { public void fullDateFormatHandled() throws ParseException {
DateFormat format = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH); DateFormat format = DateFormat.getDateInstance(DateFormat.FULL, Locale.ENGLISH);
assertEquals("Tuesday, June 24, 2014", format.format(getDateWithZoneOffset(1403539200000L))); assertEquals("Monday, June 23, 2014", format.format(getDateWithZoneOffset(1403481600000L)));
assertEquals(1403539200000L, getTimeWithoutZoneOffset(format.parse("Tuesday, June 24, 2014"))); assertEquals(1403481600000L, getTimeWithoutZoneOffset(format.parse("Monday, June 23, 2014")));
} }
private Date getDateWithZoneOffset(long milliseconds) { private Date getDateWithZoneOffset(long milliseconds) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTimeInMillis(milliseconds); calendar.setTimeInMillis(milliseconds);
milliseconds += calendar.get(Calendar.ZONE_OFFSET); milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
return new Date(milliseconds); return new Date(milliseconds);
} }
private long getTimeWithoutZoneOffset(Date date) { private long getTimeWithoutZoneOffset(Date date) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTime(date); calendar.setTime(date);
return calendar.getTimeInMillis() - calendar.get(Calendar.ZONE_OFFSET); return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET);
} }
} }

View File

@ -18,10 +18,7 @@ package org.teavm.classlib.java.text;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.Calendar; import java.util.*;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.junit.Test; import org.junit.Test;
/** /**
@ -38,114 +35,114 @@ public class SimpleDateFormatTest {
@Test @Test
public void fieldsFormatted() { public void fieldsFormatted() {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
} }
@Test @Test
public void fieldsParsed() throws ParseException { public void fieldsParsed() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 09:33:49")));
} }
@Test @Test
public void eraHandled() throws ParseException { public void eraHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("G yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("G yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("AD 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("AD 2014-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("AD 2014-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("AD 2014-06-24 09:33:49")));
} }
@Test @Test
public void shortYearHandled() throws ParseException { public void shortYearHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("14-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("14-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("14-06-24 09:33:49")));
} }
@Test @Test
public void weekInYearHandled() throws ParseException { public void weekInYearHandled() throws ParseException {
long day = 24 * 3600 * 1000; long day = 24 * 3600 * 1000;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss www", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss www", Locale.ENGLISH);
assertEquals("2014-06-24 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 09:33:49 026", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals("2014-06-28 17:33:49 026", format.format(getDateWithZoneOffset(1403602429504L + day * 4))); assertEquals("2014-06-28 09:33:49 026", format.format(getDateWithZoneOffset(1403602429504L + day * 4)));
assertEquals("2014-06-29 17:33:49 027", format.format(getDateWithZoneOffset(1403602429504L + day * 5))); assertEquals("2014-06-29 09:33:49 027", format.format(getDateWithZoneOffset(1403602429504L + day * 5)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 026"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 09:33:49 026")));
assertEquals(1403602429000L + day * 4, getTimeWithoutZoneOffset(format.parse("2014-06-28 17:33:49 026"))); assertEquals(1403602429000L + day * 4, getTimeWithoutZoneOffset(format.parse("2014-06-28 09:33:49 026")));
assertEquals(1403602429000L + day * 5, getTimeWithoutZoneOffset(format.parse("2014-06-29 17:33:49 027"))); assertEquals(1403602429000L + day * 5, getTimeWithoutZoneOffset(format.parse("2014-06-29 09:33:49 027")));
} }
@Test @Test
public void weekInMonthHandled() throws ParseException { public void weekInMonthHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss WW", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss WW", Locale.ENGLISH);
assertEquals("2014-06-24 17:33:49 04", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 09:33:49 04", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 04"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 09:33:49 04")));
} }
@Test @Test
public void dayInYearHandled() throws ParseException { public void dayInYearHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss DD", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss DD", Locale.ENGLISH);
assertEquals("2014-06-24 17:33:49 175", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 09:33:49 175", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 175"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 09:33:49 175")));
} }
@Test @Test
public void weekdayInMonthHandled() throws ParseException { public void weekdayInMonthHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss F", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss F", Locale.ENGLISH);
assertEquals("2014-06-24 17:33:49 4", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 09:33:49 4", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 17:33:49 4"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 09:33:49 4")));
} }
@Test @Test
public void shortWeekdayHandled() throws ParseException { public void shortWeekdayHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("E yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("E yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("Tue 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("Tue 2014-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tue 2014-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tue 2014-06-24 09:33:49")));
} }
@Test @Test
public void longWeekdayHandled() throws ParseException { public void longWeekdayHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("EEEE, yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("EEEE, yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("Tuesday, 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("Tuesday, 2014-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tuesday, 2014-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Tuesday, 2014-06-24 09:33:49")));
} }
@Test @Test
public void numericWeekdayHandled() throws ParseException { public void numericWeekdayHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("u yyyy-MM-dd HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("u yyyy-MM-dd HH:mm:ss", Locale.ENGLISH);
assertEquals("2 2014-06-24 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2 2014-06-24 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2 2014-06-24 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2 2014-06-24 09:33:49")));
} }
@Test @Test
public void amPmHandled() throws ParseException { public void amPmHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 'at' hh:mm:ss a", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd 'at' hh:mm:ss a", Locale.ENGLISH);
assertEquals("2014-06-24 at 05:33:49 PM", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("2014-06-24 at 09:33:49 AM", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 at 05:33:49 PM"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("2014-06-24 at 09:33:49 AM")));
} }
@Test @Test
public void shortMonthHandled() throws ParseException { public void shortMonthHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("MMM, dd yyyy HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("MMM, dd yyyy HH:mm:ss", Locale.ENGLISH);
assertEquals("Jun, 24 2014 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("Jun, 24 2014 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Jun, 24 2014 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("Jun, 24 2014 09:33:49")));
} }
@Test @Test
public void longMonthHandled() throws ParseException { public void longMonthHandled() throws ParseException {
SimpleDateFormat format = new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss", Locale.ENGLISH); SimpleDateFormat format = new SimpleDateFormat("MMMM, dd yyyy HH:mm:ss", Locale.ENGLISH);
assertEquals("June, 24 2014 17:33:49", format.format(getDateWithZoneOffset(1403602429504L))); assertEquals("June, 24 2014 09:33:49", format.format(getDateWithZoneOffset(1403602429504L)));
assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("June, 24 2014 17:33:49"))); assertEquals(1403602429000L, getTimeWithoutZoneOffset(format.parse("June, 24 2014 09:33:49")));
} }
private Date getDateWithZoneOffset(long milliseconds) { private Date getDateWithZoneOffset(long milliseconds) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTimeInMillis(milliseconds); calendar.setTimeInMillis(milliseconds);
milliseconds += calendar.get(Calendar.ZONE_OFFSET); milliseconds -= calendar.get(Calendar.ZONE_OFFSET);
return new Date(milliseconds); return new Date(milliseconds);
} }
private long getTimeWithoutZoneOffset(Date date) { private long getTimeWithoutZoneOffset(Date date) {
Calendar calendar = new GregorianCalendar(Locale.ENGLISH); Calendar calendar = new GregorianCalendar(Locale.ENGLISH);
calendar.setTime(date); calendar.setTime(date);
return calendar.getTimeInMillis() - calendar.get(Calendar.ZONE_OFFSET); return calendar.getTimeInMillis() + calendar.get(Calendar.ZONE_OFFSET);
} }
} }