Skip to content

Commit

Permalink
Issue #114 - more work on DateParser
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Harrah (frizbog) committed Jul 17, 2016
1 parent b4c41c5 commit 5fcc6d3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/main/java/org/gedcom4j/parser/DateParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,20 +115,17 @@ public Date parse(String dateString, ImpreciseDatePreference pref) {
switch (pref) {
case FAVOR_EARLIEST:
// First day of year
c.set(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.MONTH, 1);
c.set(Calendar.DAY_OF_YEAR, 1);
return c.getTime();
case FAVOR_LATEST:
// Last day of year
c.set(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.MONTH, 1);
c.add(Calendar.YEAR, 1);
c.add(Calendar.DAY_OF_YEAR, -1);
c.set(Calendar.MONTH, Calendar.DECEMBER);
c.set(Calendar.DAY_OF_MONTH, 31);
return c.getTime();
case FAVOR_MIDPOINT:
// Middle day of year - go with July 1
// Middle day of year - go with July 1. Not precisely midpoint but feels midpointy.
c.set(Calendar.MONTH, Calendar.JULY);
c.set(Calendar.DAY_OF_MONTH, 1);
c.set(Calendar.JULY, 1);
return c.getTime();
case PRECISE:
return d;
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/org/gedcom4j/parser/DateParserGregorianTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,54 @@ public void testParseSingleDateNoDayPreferMidpoint() {
assertDate(dp.parse("FEB 1900", ImpreciseDatePreference.FAVOR_MIDPOINT), 1900, Calendar.FEBRUARY, 14);
}

/**
* Test parsing a single date with month and year but no day, using precise preference
*/
@Test
public void testParseSingleDateYearOnlyPrecise() {
DateParser dp = new DateParser();
assertDate(dp.parse("2016"), 2016, Calendar.JANUARY, 1);
assertDate(dp.parse("932"), 932, Calendar.JANUARY, 1);
assertDate(dp.parse("2016"), 2016, Calendar.JANUARY, 1);
assertDate(dp.parse("1900"), 1900, Calendar.JANUARY, 1);
}

/**
* Test parsing a single date with month and year but no day, using precise preference
*/
@Test
public void testParseSingleDateYearOnlyPreferEarliest() {
DateParser dp = new DateParser();
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_EARLIEST), 2016, Calendar.JANUARY, 1);
assertDate(dp.parse("932", ImpreciseDatePreference.FAVOR_EARLIEST), 932, Calendar.JANUARY, 1);
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_EARLIEST), 2016, Calendar.JANUARY, 1);
assertDate(dp.parse("1900", ImpreciseDatePreference.FAVOR_EARLIEST), 1900, Calendar.JANUARY, 1);
}

/**
* Test parsing a single date with month and year but no day, using precise preference
*/
@Test
public void testParseSingleDateYearOnlyPreferLatest() {
DateParser dp = new DateParser();
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_LATEST), 2016, Calendar.DECEMBER, 31);
assertDate(dp.parse("932", ImpreciseDatePreference.FAVOR_LATEST), 932, Calendar.DECEMBER, 31);
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_LATEST), 2016, Calendar.DECEMBER, 31);
assertDate(dp.parse("1900", ImpreciseDatePreference.FAVOR_LATEST), 1900, Calendar.DECEMBER, 31);
}

/**
* Test parsing a single date with month and year but no day, using precise preference
*/
@Test
public void testParseSingleDateYearOnlyPreferMidpoint() {
DateParser dp = new DateParser();
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_MIDPOINT), 2016, Calendar.JULY, 1);
assertDate(dp.parse("932", ImpreciseDatePreference.FAVOR_MIDPOINT), 932, Calendar.JULY, 1);
assertDate(dp.parse("2016", ImpreciseDatePreference.FAVOR_MIDPOINT), 2016, Calendar.JULY, 1);
assertDate(dp.parse("1900", ImpreciseDatePreference.FAVOR_MIDPOINT), 1900, Calendar.JULY, 1);
}

/**
* Test handling for dates that are not compliant with the GEDCOM spec in their format
*/
Expand Down

0 comments on commit 5fcc6d3

Please sign in to comment.