diff --git a/src/main/java/groovy/time/AgoDate.java b/src/main/java/groovy/time/AgoDate.java new file mode 100644 index 00000000000..0d9062fedf7 --- /dev/null +++ b/src/main/java/groovy/time/AgoDate.java @@ -0,0 +1,14 @@ +package groovy.time; + +import java.util.Date; + +/** + * @author Aliaksei Bialiauski (abialiauski@solvd.com) + */ +public interface AgoDate { + + /** + * @return mapped date by specs. + */ + Date toDateFormat(); +} diff --git a/src/main/java/groovy/time/TimeAgoFromData.java b/src/main/java/groovy/time/TimeAgoFromData.java new file mode 100644 index 00000000000..2e8f0ef3e4e --- /dev/null +++ b/src/main/java/groovy/time/TimeAgoFromData.java @@ -0,0 +1,36 @@ +package groovy.time; + +import java.util.Calendar; +import java.util.Date; + +/** + * Ago Date Result + * + * @author Aliaksei Bialiauski (abialiauski@solvd.com) + */ +public class TimeAgoFromData extends Date implements AgoDate { + + private final Calendar calendar; + private final Date date; + + public TimeAgoFromData(final int days, final int hours, + final int minutes, final int sec, final int millis) { + this.calendar = Calendar.getInstance(); + this.date = this.unbox(days, hours, minutes, sec, millis); + } + + @Override + public Date toDateFormat() { + return this.date; + } + + private Date unbox(final int days, final int hours, final int minutes, + final int sec, final int millis) { + this.calendar.add(Calendar.DAY_OF_YEAR, -days); + this.calendar.add(Calendar.HOUR_OF_DAY, -hours); + this.calendar.add(Calendar.MINUTE, -minutes); + this.calendar.add(Calendar.SECOND, -sec); + this.calendar.add(Calendar.MILLISECOND, -millis); + return this.calendar.getTime(); + } +} diff --git a/src/main/java/groovy/time/TimeDuration.java b/src/main/java/groovy/time/TimeDuration.java index 93bcfd1db53..58f232a7de3 100644 --- a/src/main/java/groovy/time/TimeDuration.java +++ b/src/main/java/groovy/time/TimeDuration.java @@ -16,6 +16,7 @@ * specific language governing permissions and limitations * under the License. */ + package groovy.time; import java.util.Calendar; @@ -38,54 +39,53 @@ public class TimeDuration extends Duration { public TimeDuration(final int hours, final int minutes, final int seconds, final int millis) { super(0, hours, minutes, seconds, millis); - } + } - public TimeDuration(final int days, final int hours, final int minutes, final int seconds, final int millis) { + public TimeDuration(final int days, final int hours, final int minutes, final int seconds, + final int millis) { super(days, hours, minutes, seconds, millis); - } + } @Override public Duration plus(final Duration rhs) { return new TimeDuration(this.getDays() + rhs.getDays(), this.getHours() + rhs.getHours(), - this.getMinutes() + rhs.getMinutes(), this.getSeconds() + rhs.getSeconds(), - this.getMillis() + rhs.getMillis()); + this.getMinutes() + rhs.getMinutes(), this.getSeconds() + rhs.getSeconds(), + this.getMillis() + rhs.getMillis()); } @Override public DatumDependentDuration plus(final DatumDependentDuration rhs) { return new TimeDatumDependentDuration(rhs.getYears(), rhs.getMonths(), - this.getDays() + rhs.getDays(), this.getHours() + rhs.getHours(), - this.getMinutes() + rhs.getMinutes(), this.getSeconds() + rhs.getSeconds(), - this.getMillis() + rhs.getMillis()); + this.getDays() + rhs.getDays(), this.getHours() + rhs.getHours(), + this.getMinutes() + rhs.getMinutes(), this.getSeconds() + rhs.getSeconds(), + this.getMillis() + rhs.getMillis()); } @Override public Duration minus(final Duration rhs) { return new TimeDuration(this.getDays() - rhs.getDays(), this.getHours() - rhs.getHours(), - this.getMinutes() - rhs.getMinutes(), this.getSeconds() - rhs.getSeconds(), - this.getMillis() - rhs.getMillis()); + this.getMinutes() - rhs.getMinutes(), this.getSeconds() - rhs.getSeconds(), + this.getMillis() - rhs.getMillis()); } @Override public DatumDependentDuration minus(final DatumDependentDuration rhs) { return new TimeDatumDependentDuration(-rhs.getYears(), -rhs.getMonths(), - this.getDays() - rhs.getDays(), this.getHours() - rhs.getHours(), - this.getMinutes() - rhs.getMinutes(), this.getSeconds() - rhs.getSeconds(), - this.getMillis() - rhs.getMillis()); + this.getDays() - rhs.getDays(), this.getHours() - rhs.getHours(), + this.getMinutes() - rhs.getMinutes(), this.getSeconds() - rhs.getSeconds(), + this.getMillis() - rhs.getMillis()); } @Override public Date getAgo() { - final Calendar cal = Calendar.getInstance(); - - cal.add(Calendar.DAY_OF_YEAR, -this.getDays()); - cal.add(Calendar.HOUR_OF_DAY, -this.getHours()); - cal.add(Calendar.MINUTE, -this.getMinutes()); - cal.add(Calendar.SECOND, -this.getSeconds()); - cal.add(Calendar.MILLISECOND, -this.getMillis()); - - return cal.getTime(); - } + return new TimeAgoFromData( + this.days, + this.hours, + this.minutes, + this.seconds, + this.millis) + .toDateFormat(); + } @Override public From getFrom() { @@ -93,13 +93,11 @@ public From getFrom() { @Override public Date getNow() { final Calendar cal = Calendar.getInstance(); - cal.add(Calendar.DAY_OF_YEAR, TimeDuration.this.getDays()); cal.add(Calendar.HOUR_OF_DAY, TimeDuration.this.getHours()); cal.add(Calendar.MINUTE, TimeDuration.this.getMinutes()); cal.add(Calendar.SECOND, TimeDuration.this.getSeconds()); cal.add(Calendar.MILLISECOND, TimeDuration.this.getMillis()); - return cal.getTime(); } };