Skip to content
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
14 changes: 14 additions & 0 deletions src/main/java/groovy/time/AgoDate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package groovy.time;

import java.util.Date;

/**
* @author Aliaksei Bialiauski ([email protected])
*/
public interface AgoDate {

/**
* @return mapped date by specs.
*/
Date toDateFormat();
}
36 changes: 36 additions & 0 deletions src/main/java/groovy/time/TimeAgoFromData.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package groovy.time;

import java.util.Calendar;
import java.util.Date;

/**
* Ago Date Result
*
* @author Aliaksei Bialiauski ([email protected])
*/
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();
}
}
48 changes: 23 additions & 25 deletions src/main/java/groovy/time/TimeDuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* specific language governing permissions and limitations
* under the License.
*/

package groovy.time;

import java.util.Calendar;
Expand All @@ -38,68 +39,65 @@
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() {
return new From() {
@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();
}
};
Expand Down