MAILBOX-266 ZonedDateTime does not support truncate using Month and Year units
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/b446bffd Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/b446bffd Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/b446bffd Branch: refs/heads/master Commit: b446bffdc630da8eedc75fc010c0891758c4f94c Parents: 0ea8bc6 Author: Benoit Tellier <btell...@linagora.com> Authored: Mon Mar 28 14:07:55 2016 +0700 Committer: Benoit Tellier <btell...@linagora.com> Committed: Wed Apr 6 16:18:45 2016 +0700 ---------------------------------------------------------------------- .../query/DateResolutionFormater.java | 11 +++++++++-- .../query/DateResolutionFormaterTest.java | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/james-project/blob/b446bffd/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java index 30574f3..70746b5 100644 --- a/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java +++ b/mailbox/elasticsearch/src/main/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormater.java @@ -34,11 +34,18 @@ public class DateResolutionFormater { public static DateTimeFormatter DATE_TIME_FOMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ"); public static ZonedDateTime computeUpperDate(ZonedDateTime date, SearchQuery.DateResolution resolution) { - return date.truncatedTo(convertDateResolutionField(resolution)).plus(1,convertDateResolutionField(resolution)); + return computeLowerDate(date, resolution).plus(1, convertDateResolutionField(resolution)); } public static ZonedDateTime computeLowerDate(ZonedDateTime date, SearchQuery.DateResolution resolution) { - return date.truncatedTo(convertDateResolutionField(resolution)); + switch (resolution) { + case Year: + return date.truncatedTo(ChronoUnit.DAYS).withDayOfYear(1); + case Month: + return date.truncatedTo(ChronoUnit.DAYS).withDayOfMonth(1); + default: + return date.truncatedTo(convertDateResolutionField(resolution)); + } } private static TemporalUnit convertDateResolutionField(SearchQuery.DateResolution resolution) { http://git-wip-us.apache.org/repos/asf/james-project/blob/b446bffd/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java ---------------------------------------------------------------------- diff --git a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java index fed35c1..07cbe4f 100644 --- a/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java +++ b/mailbox/elasticsearch/src/test/java/org/apache/james/mailbox/elasticsearch/query/DateResolutionFormaterTest.java @@ -52,6 +52,24 @@ public class DateResolutionFormaterTest { } @Test + public void calculateUpperDateShouldReturnDateUpToTheNextMonthUsingMonthUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeUpperDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Month) + ) + ).isEqualTo("2014-02-01T00:00:00Z"); + } + + @Test + public void calculateUpperDateShouldReturnDateUpToTheNextYearUsingYearUnit() throws ParseException { + assertThat( + ISO_OFFSET_DATE_TIME.format( + DateResolutionFormater.computeUpperDate(ZonedDateTime.parse(dateString, ISO_OFFSET_DATE_TIME), SearchQuery.DateResolution.Year) + ) + ).isEqualTo("2015-01-01T00:00:00Z"); + } + + @Test public void calculateUpperDateShouldReturnDateUpToTheNextDayUsingDayUnit() throws ParseException { assertThat( ISO_OFFSET_DATE_TIME.format( --------------------------------------------------------------------- To unsubscribe, e-mail: server-dev-unsubscr...@james.apache.org For additional commands, e-mail: server-dev-h...@james.apache.org