Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/org/elasticsearch/common/joda/Joda.java
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static FormatDateTimeFormatter forPattern(String input, Locale locale) {
// in this case, we have a separate parser and printer since the dataOptionalTimeParser can't print
// this sucks we should use the root local by default and not be dependent on the node
return new FormatDateTimeFormatter(input,
ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC),
ISODateTimeFormat.dateOptionalTimeParser().withZone(DateTimeZone.UTC).withDefaultYear(1970),
ISODateTimeFormat.dateTime().withZone(DateTimeZone.UTC), locale);
} else if ("dateTime".equals(input) || "date_time".equals(input)) {
formatter = ISODateTimeFormat.dateTime();
Expand Down Expand Up @@ -160,7 +160,7 @@ public static FormatDateTimeFormatter forPattern(String input, Locale locale) {
}
}

return new FormatDateTimeFormatter(input, formatter.withZone(DateTimeZone.UTC), locale);
return new FormatDateTimeFormatter(input, formatter.withZone(DateTimeZone.UTC).withDefaultYear(1970), locale);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.elasticsearch.common.joda;

import org.elasticsearch.test.ElasticsearchTestCase;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Test;

import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -44,4 +46,13 @@ public void actualDateTests() {
assertThat(parser.parse("2013-03-03||/y", 0), equalTo(parser.parse("2013-01-01", 0)));
assertThat(parser.parseRoundCeil("2013-03-03||/y", 0), equalTo(parser.parse("2014-01-01", 0)));
}

@Test
public void partialDatesDefaultToYear1970() {
// when the year is not part of the format, it must default to the 1970 epoch base
// (rather than joda's default of 2000), consistent with how partial dates are indexed
DateMathParser parser = new DateMathParser(Joda.forPattern("MM-dd"), TimeUnit.MILLISECONDS);
assertThat(parser.parse("01-01", 0), equalTo(0l));
assertThat(parser.parse("06-15", 0), equalTo(new DateTime(1970, 6, 15, 0, 0, DateTimeZone.UTC).getMillis()));
}
}