diff options
author | Florian Zschocke <f.zschocke+git@gmail.com> | 2021-10-23 23:20:47 +0200 |
---|---|---|
committer | Florian Zschocke <f.zschocke+git@gmail.com> | 2021-10-23 23:20:47 +0200 |
commit | b3f35278cead1c4647a10ae615258555618bd087 (patch) | |
tree | 4197ed94312ae894f57e6662c40487c7e5510e17 /src/main/java | |
parent | 4f04ab8ec402dd51bbdb7121d89da2d9a385f1a8 (diff) | |
download | gitblit-b3f35278cead1c4647a10ae615258555618bd087.tar.gz gitblit-b3f35278cead1c4647a10ae615258555618bd087.zip |
TimeUtils: Increase testability and add tests
Add tests for `timeAgo` to analyse issue #1248.
The tests are dependent on when they run as they time functions use the
current date and time. To make them testable in a reproducible way, we
need the ability to pass in what we think is "now". So add overloaded
methods that take a `now` parameter so that we can pass in the current
time.
Diffstat (limited to 'src/main/java')
-rw-r--r-- | src/main/java/com/gitblit/utils/TimeUtils.java | 40 |
1 files changed, 32 insertions, 8 deletions
diff --git a/src/main/java/com/gitblit/utils/TimeUtils.java b/src/main/java/com/gitblit/utils/TimeUtils.java index c0e98e5d..001e6be6 100644 --- a/src/main/java/com/gitblit/utils/TimeUtils.java +++ b/src/main/java/com/gitblit/utils/TimeUtils.java @@ -59,7 +59,11 @@ public class TimeUtils { * @return true if date is today
*/
public static boolean isToday(Date date, TimeZone timezone) {
- Date now = new Date();
+ return isToday(date, timezone, new Date());
+ }
+
+
+ public static boolean isToday(Date date, TimeZone timezone, Date now) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
if (timezone != null) {
df.setTimeZone(timezone);
@@ -74,8 +78,13 @@ public class TimeUtils { * @return true if date is yesterday
*/
public static boolean isYesterday(Date date, TimeZone timezone) {
+ return isYesterday(date, timezone, new Date());
+ }
+
+
+ public static boolean isYesterday(Date date, TimeZone timezone, Date now) {
Calendar cal = Calendar.getInstance();
- cal.setTime(new Date());
+ cal.setTime(now);
cal.add(Calendar.DATE, -1);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
if (timezone != null) {
@@ -157,7 +166,12 @@ public class TimeUtils { * @return hours ago
*/
public static int hoursAgo(Date date, boolean roundup) {
- long diff = System.currentTimeMillis() - date.getTime();
+ return hoursAgo(date, System.currentTimeMillis(), roundup);
+ }
+
+
+ public static int hoursAgo(Date date, long now, boolean roundup) {
+ long diff = now - date.getTime();
int hours = (int) (diff / ONEHOUR);
if (roundup && (diff % ONEHOUR) >= HALFHOUR) {
hours++;
@@ -172,7 +186,11 @@ public class TimeUtils { * @return days ago
*/
public static int daysAgo(Date date) {
- long today = ONEDAY * (System.currentTimeMillis()/ONEDAY);
+ return daysAgo(date, System.currentTimeMillis());
+ }
+
+ public static int daysAgo(Date date, long now) {
+ long today = ONEDAY * (now/ONEDAY);
long day = ONEDAY * (date.getTime()/ONEDAY);
long diff = today - day;
int days = (int) (diff / ONEDAY);
@@ -217,13 +235,19 @@ public class TimeUtils { * @return the string representation of the duration OR the css class
*/
private String timeAgo(Date date, boolean css) {
- if (isToday(date, timezone) || isYesterday(date, timezone)) {
- int mins = minutesAgo(date, true);
+ return timeAgo(date, css, System.currentTimeMillis());
+ }
+
+
+ public String timeAgo(Date date, boolean css, long now) {
+ Date dNow = new Date(now);
+ if (isToday(date, timezone, dNow) || isYesterday(date, timezone, dNow)) {
+ int mins = minutesAgo(date, now, true);
if (mins >= 120) {
if (css) {
return "age1";
}
- int hours = hoursAgo(date, true);
+ int hours = hoursAgo(date, now, true);
if (hours > 23) {
return yesterday();
} else {
@@ -238,7 +262,7 @@ public class TimeUtils { }
return translate("gb.time.justNow", "just now");
} else {
- int days = daysAgo(date);
+ int days = daysAgo(date, now);
if (css) {
if (days <= 7) {
return "age2";
|