Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added TemporalAdjuster for rounding to nearest part of an hour #196

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions src/main/java/org/threeten/extra/RoundingMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.threeten.extra;

/**
* A time clock rounding mode.
* <p>
* {@code RoundingMode} specifies a time clock rounding strategy.
* <p>
* Rounding mode defines the following strategies:
* <li> DOWN - rounds the time towards the start of a fraction
* <li> HALF_UP - rounds towards the "nearest neighbor". UP, if the distance is of equal length
* <li> UP - rounds the time towards the end of a fraction
*/
public enum RoundingMode {
DOWN,
HALF_UP,
UP
}
65 changes: 65 additions & 0 deletions src/main/java/org/threeten/extra/Temporals.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@
package org.threeten.extra;

import static java.time.temporal.ChronoField.DAY_OF_WEEK;
import static java.time.temporal.ChronoField.MINUTE_OF_HOUR;
import static java.time.temporal.ChronoField.SECOND_OF_MINUTE;
import static java.time.temporal.ChronoField.NANO_OF_SECOND;
import static java.time.temporal.ChronoUnit.DAYS;
import static java.time.temporal.ChronoUnit.ERAS;
import static java.time.temporal.ChronoUnit.FOREVER;
import static java.time.temporal.ChronoUnit.WEEKS;

import java.text.ParsePosition;
import java.time.Duration;
import java.time.DateTimeException;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand All @@ -59,6 +63,7 @@
* <li>adjusters that ignore Saturday/Sunday weekends
* <li>conversion between {@code TimeUnit} and {@code ChronoUnit}
* <li>converting an amount to another unit
* <li>adjusters that round time
* </ul>
*
* <h3>Implementation Requirements:</h3>
Expand Down Expand Up @@ -126,6 +131,66 @@ public static TemporalAdjuster previousWorkingDayOrSame() {
return Adjuster.PREVIOUS_WORKING_OR_SAME;
}

/**
* Returns an adjuster that offers time clock rounding.
* <p>
* Time clock rounding divides the minutes of the hour into equal fractions of the same length. Every fraction has
* a start-inclusive and end-exclusive minute of the hour. A rounded result is either at the lower or upper end of
* one of these fractions.
* <p>
* Time clock rounding views time as hour-minute. Therefore it always truncates to minutes.
*
* @param duration the fraction of the hour, must be a divisor of 60
* @param roundingMode the time clock rounding mode
* @return time rounded to a fraction of the hour
*/
public static TemporalAdjuster roundTime(Duration duration, RoundingMode roundingMode) {
Objects.requireNonNull(duration, "duration");
Objects.requireNonNull(roundingMode, "mode");

long minutes = duration.toMinutes();
if (minutes < 1 || 60 % minutes != 0) {
throw new DateTimeException("duration is not a divisor of 60");
}

int divisor = (int) minutes;

return temporal -> {
int minuteOfHour = temporal.get(MINUTE_OF_HOUR);

temporal = temporal.with(SECOND_OF_MINUTE, 0)
.with(NANO_OF_SECOND, 0);

if (minuteOfHour % divisor == 0) {
return temporal;
}

int down = minuteOfHour / divisor * divisor;
int up = down + divisor;
RoundingMode mode = roundingMode;

if (roundingMode == RoundingMode.HALF_UP) {
mode = (minuteOfHour - down) < (up - minuteOfHour) ? RoundingMode.DOWN : RoundingMode.UP;
}

if (mode == RoundingMode.DOWN) {
temporal = temporal.with(MINUTE_OF_HOUR, down);
}

if (mode == RoundingMode.UP) {
if (up == 60) {
if (temporal.isSupported(ChronoUnit.HOURS)) {
temporal = temporal.plus(1, ChronoUnit.HOURS);
}
up = 0;
}
temporal = temporal.with(MINUTE_OF_HOUR, up);
}

return temporal;
};
}

//-----------------------------------------------------------------------
/**
* Enum implementing the adjusters.
Expand Down
51 changes: 51 additions & 0 deletions src/test/java/org/threeten/extra/TestTemporals.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.time.DateTimeException;
import java.time.LocalTime;
import java.time.LocalDate;
import java.time.Duration;
import java.time.Month;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
Expand Down Expand Up @@ -670,4 +672,53 @@ public void test_convertAmountInvalidUnsupported(TemporalUnit fromUnit, Temporal
assertThrows(UnsupportedTemporalTypeException.class, () -> Temporals.convertAmount(1, fromUnit, resultUnit));
}


//-----------------------------------------------------------------------
// roundTime()
//-----------------------------------------------------------------------
@Test
public void test_roundTime_dateTimeException() {
assertThrows(DateTimeException.class, () -> Temporals.roundTime(Duration.ofMinutes(7), RoundingMode.DOWN));
}

@Test
public void test_roundTime_down_localTime() {
TemporalAdjuster down = Temporals.roundTime(Duration.ofMinutes(6), RoundingMode.DOWN);

assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 12).with(down));
assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 13).with(down));
assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 14).with(down));
assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 15).with(down));
assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 16).with(down));
assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 17).with(down));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 18).with(down));

}

@Test
public void test_roundTime_up_localTime() {
TemporalAdjuster up = Temporals.roundTime(Duration.ofMinutes(6), RoundingMode.UP);

assertEquals(LocalTime.of(14, 12), LocalTime.of(14, 12, 7, 1).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 13, 7, 1).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 14).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 15).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 16).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 17).with(up));
assertEquals(LocalTime.of(14, 18), LocalTime.of(14, 18).with(up));
}

@Test
public void test_roundTime_halfUp_localTime() {
TemporalAdjuster halfUp = Temporals.roundTime(Duration.ofMinutes(6), RoundingMode.HALF_UP);

assertEquals(LocalTime.of(14, 54), LocalTime.of(14, 54, 7, 1).with(halfUp));
assertEquals(LocalTime.of(14, 54), LocalTime.of(14, 55, 7, 1).with(halfUp));
assertEquals(LocalTime.of(14, 54), LocalTime.of(14, 56).with(halfUp));
assertEquals(LocalTime.of(15, 0), LocalTime.of(14, 57).with(halfUp));
assertEquals(LocalTime.of(15, 0), LocalTime.of(14, 58).with(halfUp));
assertEquals(LocalTime.of(15, 0), LocalTime.of(14, 59).with(halfUp));
assertEquals(LocalTime.of(15, 0), LocalTime.of(15, 0).with(halfUp));
}

}