diff --git a/src/main/scala/view/helpers.scala b/src/main/scala/view/helpers.scala index 78f658f..6bb1d0c 100644 --- a/src/main/scala/view/helpers.scala +++ b/src/main/scala/view/helpers.scala @@ -1,5 +1,5 @@ package view -import java.util.{Date, TimeZone} +import java.util.{Locale, Date, TimeZone} import java.text.SimpleDateFormat import play.twirl.api.Html import util.StringUtil @@ -15,6 +15,49 @@ */ def datetime(date: Date): String = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date) + val timeUnits = List( + (1000L, "second"), + (1000L * 60, "minute"), + (1000L * 60 * 60, "hour"), + (1000L * 60 * 60 * 24, "day"), + (1000L * 60 * 60 * 24 * 30, "month"), + (1000L * 60 * 60 * 24 * 365, "year") + ).reverse + + /** + * Format java.util.Date to "x {seconds/minutes/hours/days/months/years} ago" + */ + def datetimeAgo(date: Date): String = { + val duration = new Date().getTime - date.getTime + timeUnits.find(tuple => duration / tuple._1 > 0) match { + case Some((unitValue, unitString)) => + val value = duration / unitValue + s"${value} ${unitString}${if (value > 1) "s" else ""} ago" + case None => "just now" + } + } + + /** + * + * Format java.util.Date to "x {seconds/minutes/hours/days} ago" + * If duration over 1 month, format to "d MMM (yyyy)" + * + */ + def datetimeAgoRecentOnly(date: Date): String = { + val duration = new Date().getTime - date.getTime + val list = timeUnits.map(tuple => (duration / tuple._1, tuple._2)).filter(tuple => tuple._1 > 0) + if (list.isEmpty) + "just now" + else { + list.head match { + case (_, "month") => s"on ${new SimpleDateFormat("d MMM", Locale.ENGLISH).format(date)}" + case (_, "year") => s"on ${new SimpleDateFormat("d MMM yyyy", Locale.ENGLISH).format(date)}" + case (value, unitString) => s"${value} ${unitString}${if (value > 1) "s" else ""} ago" + } + } + } + + /** * Format java.util.Date to "yyyy-MM-dd'T'hh:mm:ss'Z'". */ diff --git a/src/main/twirl/account/repositories.scala.html b/src/main/twirl/account/repositories.scala.html index 1ad52df..8a3706b 100644 --- a/src/main/twirl/account/repositories.scala.html +++ b/src/main/twirl/account/repositories.scala.html @@ -25,7 +25,7 @@ @if(repository.repository.description.isDefined){
@repository.repository.description
} -
Last updated: @datetime(repository.repository.lastActivityDate)
+
Updated @helper.html.datetimeago(repository.repository.lastActivityDate)
} diff --git a/src/main/twirl/dashboard/issueslist.scala.html b/src/main/twirl/dashboard/issueslist.scala.html index 4f27ea8..186eb98 100644 --- a/src/main/twirl/dashboard/issueslist.scala.html +++ b/src/main/twirl/dashboard/issueslist.scala.html @@ -165,7 +165,7 @@ #@issue.issueId
- Opened by @user(issue.openedUserName, styleClass="username") @datetime(issue.registeredDate)  + Opened by @user(issue.openedUserName, styleClass="username") @helper.html.datetimeago(issue.registeredDate)  @if(commentCount > 0){ @commentCount @plural(commentCount, "comment") } diff --git a/src/main/twirl/dashboard/pullslist.scala.html b/src/main/twirl/dashboard/pullslist.scala.html index 46343b4..dc90d9e 100644 --- a/src/main/twirl/dashboard/pullslist.scala.html +++ b/src/main/twirl/dashboard/pullslist.scala.html @@ -86,7 +86,7 @@ }
- @avatarLink(issue.openedUserName, 20) by @user(issue.openedUserName, styleClass="username") @datetime(issue.registeredDate)  + @avatarLink(issue.openedUserName, 20) by @user(issue.openedUserName, styleClass="username") @helper.html.datetimeago(issue.registeredDate)  @if(commentCount > 0){ @commentCount @plural(commentCount, "comment") } diff --git a/src/main/twirl/helper/activities.scala.html b/src/main/twirl/helper/activities.scala.html index 980eaf5..9bb3f8d 100644 --- a/src/main/twirl/helper/activities.scala.html +++ b/src/main/twirl/helper/activities.scala.html @@ -62,7 +62,7 @@ @detailActivity(activity: model.Activity, image: String) = {
-
@datetime(activity.activityDate)
+
@helper.html.datetimeago(activity.activityDate)
@avatar(activity.activityUserName, 16) @activityMessage(activity.message) @@ -76,7 +76,7 @@ @customActivity(activity: model.Activity, image: String)(additionalInfo: Any) = {
-
@datetime(activity.activityDate)
+
@helper.html.datetimeago(activity.activityDate)
@avatar(activity.activityUserName, 16) @activityMessage(activity.message) @@ -91,7 +91,7 @@
@avatar(activity.activityUserName, 16) @activityMessage(activity.message) - @datetime(activity.activityDate) + @helper.html.datetimeago(activity.activityDate)
} diff --git a/src/main/twirl/helper/datetimeago.scala.html b/src/main/twirl/helper/datetimeago.scala.html new file mode 100644 index 0000000..3b68f34 --- /dev/null +++ b/src/main/twirl/helper/datetimeago.scala.html @@ -0,0 +1,10 @@ +@(latestUpdatedDate: java.util.Date, + recentOnly: Boolean = true) +@import view.helpers._ + + @if(recentOnly){ + @datetimeAgoRecentOnly(latestUpdatedDate) + }else{ + @datetimeAgo(latestUpdatedDate) + } + diff --git a/src/main/twirl/issues/commentlist.scala.html b/src/main/twirl/issues/commentlist.scala.html index 979062f..32532d6 100644 --- a/src/main/twirl/issues/commentlist.scala.html +++ b/src/main/twirl/issues/commentlist.scala.html @@ -8,7 +8,7 @@
@avatar(issue.openedUserName, 48)
- @user(issue.openedUserName, styleClass="username strong") commented on @datetime(issue.registeredDate) + @user(issue.openedUserName, styleClass="username strong") commented @helper.html.datetimeago(issue.registeredDate) @if(hasWritePermission || loginAccount.map(_.userName == issue.openedUserName).getOrElse(false)){ @@ -32,7 +32,7 @@ } else { @if(pullreq.isEmpty){ referenced the issue } else { referenced the pull request } } - on @datetime(comment.registeredDate) + @helper.html.datetimeago(comment.registeredDate) @if(comment.action != "commit" && comment.action != "merge" && comment.action != "refer" && @@ -70,7 +70,7 @@ } else { @pullreq.map(_.userName):@pullreq.map(_.branch) to @pullreq.map(_.requestUserName):@pullreq.map(_.requestBranch) } - @datetime(comment.registeredDate) + @helper.html.datetimeago(comment.registeredDate)
} @if(comment.action == "close" || comment.action == "close_comment"){ @@ -78,9 +78,9 @@ Closed @avatar(comment.commentedUserName, 20) @if(issue.isPullRequest){ - @user(comment.commentedUserName, styleClass="username strong") closed the pull request @datetime(comment.registeredDate) + @user(comment.commentedUserName, styleClass="username strong") closed the pull request @helper.html.datetimeago(comment.registeredDate) } else { - @user(comment.commentedUserName, styleClass="username strong") closed the issue @datetime(comment.registeredDate) + @user(comment.commentedUserName, styleClass="username strong") closed the issue @helper.html.datetimeago(comment.registeredDate) }
} @@ -88,14 +88,14 @@
Reopened @avatar(comment.commentedUserName, 20) - @user(comment.commentedUserName, styleClass="username strong") reopened the issue @datetime(comment.registeredDate) + @user(comment.commentedUserName, styleClass="username strong") reopened the issue @helper.html.datetimeago(comment.registeredDate)
} @if(comment.action == "delete_branch"){
Deleted @avatar(comment.commentedUserName, 20) - @user(comment.commentedUserName, styleClass="username strong") deleted the @pullreq.map(_.requestBranch) branch @datetime(comment.registeredDate) + @user(comment.commentedUserName, styleClass="username strong") deleted the @pullreq.map(_.requestBranch) branch @helper.html.datetimeago(comment.registeredDate)
} } diff --git a/src/main/twirl/issues/issue.scala.html b/src/main/twirl/issues/issue.scala.html index 3b45ebf..3b7f125 100644 --- a/src/main/twirl/issues/issue.scala.html +++ b/src/main/twirl/issues/issue.scala.html @@ -28,7 +28,7 @@ Open } - @user(issue.openedUserName, styleClass="username strong") opened this issue on @datetime(issue.registeredDate) - @defining( + @user(issue.openedUserName, styleClass="username strong") opened this issue @helper.html.datetimeago(issue.registeredDate) - @defining( comments.filter( _.action.contains("comment") ).size ){ count => @count @plural(count, "comment") diff --git a/src/main/twirl/issues/listparts.scala.html b/src/main/twirl/issues/listparts.scala.html index d3b5a11..ab9cbad 100644 --- a/src/main/twirl/issues/listparts.scala.html +++ b/src/main/twirl/issues/listparts.scala.html @@ -203,7 +203,7 @@ }
- #@issue.issueId opened by @user(issue.openedUserName, styleClass="username") @datetime(issue.registeredDate) + #@issue.issueId opened @helper.html.datetimeago(issue.registeredDate) by @user(issue.openedUserName, styleClass="username") @milestone.map { milestone => @milestone } diff --git a/src/main/twirl/issues/milestones/list.scala.html b/src/main/twirl/issues/milestones/list.scala.html index 8770832..84a03a4 100644 --- a/src/main/twirl/issues/milestones/list.scala.html +++ b/src/main/twirl/issues/milestones/list.scala.html @@ -34,7 +34,7 @@ @milestone.title
@if(milestone.closedDate.isDefined){ - Closed @datetime(milestone.closedDate.get) + Closed @helper.html.datetimeago(milestone.closedDate.get) } else { @milestone.dueDate.map { dueDate => @if(isPast(dueDate)){ diff --git a/src/main/twirl/pulls/pullreq.scala.html b/src/main/twirl/pulls/pullreq.scala.html index 35cebb3..495379b 100644 --- a/src/main/twirl/pulls/pullreq.scala.html +++ b/src/main/twirl/pulls/pullreq.scala.html @@ -20,7 +20,7 @@ Merged @user(comment.commentedUserName, styleClass="username strong") merged @commits.size @plural(commits.size, "commit") into @pullreq.userName:@pullreq.branch from @pullreq.requestUserName:@pullreq.requestBranch - at @datetime(comment.registeredDate) + @helper.html.datetimeago(comment.registeredDate) }.getOrElse { Closed @user(issue.openedUserName, styleClass="username strong") wants to merge @commits.size @plural(commits.size, "commit") diff --git a/src/main/twirl/repo/blob.scala.html b/src/main/twirl/repo/blob.scala.html index 2c895ac..68d3870 100644 --- a/src/main/twirl/repo/blob.scala.html +++ b/src/main/twirl/repo/blob.scala.html @@ -34,7 +34,7 @@
@avatar(latestCommit, 20) @user(latestCommit.authorName, latestCommit.authorEmailAddress, "username strong") - @datetime(latestCommit.commitTime) + @helper.html.datetimeago(latestCommit.commitTime) @link(latestCommit.summary, repository)
diff --git a/src/main/twirl/repo/branches.scala.html b/src/main/twirl/repo/branches.scala.html index 214c37c..9857684 100644 --- a/src/main/twirl/repo/branches.scala.html +++ b/src/main/twirl/repo/branches.scala.html @@ -22,7 +22,7 @@ } - @datetime(latestUpdateDate) + @helper.html.datetimeago(latestUpdateDate, false) @if(repository.repository.defaultBranch == branchName){ diff --git a/src/main/twirl/repo/commit.scala.html b/src/main/twirl/repo/commit.scala.html index a022236..9b7c2f0 100644 --- a/src/main/twirl/repo/commit.scala.html +++ b/src/main/twirl/repo/commit.scala.html @@ -68,13 +68,13 @@
@avatar(commit, 20) @user(commit.authorName, commit.authorEmailAddress, "username strong") - authored on @datetime(commit.authorTime) + authored @helper.html.datetimeago(commit.authorTime)
@if(commit.isDifferentFromAuthor) {
@user(commit.committerName, commit.committerEmailAddress, "username strong") - committed on @datetime(commit.commitTime) + committed @helper.html.datetimeago(commit.commitTime)
}
diff --git a/src/main/twirl/repo/commits.scala.html b/src/main/twirl/repo/commits.scala.html index be2b02f..f0aaf3b 100644 --- a/src/main/twirl/repo/commits.scala.html +++ b/src/main/twirl/repo/commits.scala.html @@ -58,11 +58,11 @@ }
@user(commit.authorName, commit.authorEmailAddress, "username") - authored @datetime(commit.authorTime) + authored @helper.html.datetimeago(commit.authorTime) @if(commit.isDifferentFromAuthor) { @user(commit.committerName, commit.committerEmailAddress, "username") - committed @datetime(commit.authorTime) + committed @helper.html.datetimeago(commit.authorTime) }
diff --git a/src/main/twirl/repo/files.scala.html b/src/main/twirl/repo/files.scala.html index 2235d6e..ab14364 100644 --- a/src/main/twirl/repo/files.scala.html +++ b/src/main/twirl/repo/files.scala.html @@ -47,13 +47,13 @@
@avatar(latestCommit, 20) @user(latestCommit.authorName, latestCommit.authorEmailAddress, "username strong") - authored on @datetime(latestCommit.authorTime) + authored @helper.html.datetimeago(latestCommit.authorTime)
@if(latestCommit.isDifferentFromAuthor) {
@user(latestCommit.committerName, latestCommit.committerEmailAddress, "username strong") - committed on @datetime(latestCommit.commitTime) + committed @helper.html.datetimeago(latestCommit.commitTime)
}
@@ -106,7 +106,7 @@ @link(file.message, repository) [@user(file.author, file.mailAddress)] - @datetime(file.time) + @helper.html.datetimeago(file.time, false) } diff --git a/src/main/twirl/repo/tags.scala.html b/src/main/twirl/repo/tags.scala.html index e5b0fa9..cd611fb 100644 --- a/src/main/twirl/repo/tags.scala.html +++ b/src/main/twirl/repo/tags.scala.html @@ -14,7 +14,7 @@ @repository.tags.reverse.map { tag => @tag.name - @datetime(tag.time) + @helper.html.datetimeago(tag.time, false) @tag.id.substring(0, 10) ZIP diff --git a/src/main/twirl/search/code.scala.html b/src/main/twirl/search/code.scala.html index 20b08a6..04d0a19 100644 --- a/src/main/twirl/search/code.scala.html +++ b/src/main/twirl/search/code.scala.html @@ -16,7 +16,7 @@ @files.drop((page - 1) * CodeLimit).take(CodeLimit).map { file =>
@file.path
-
Latest commit at @datetime(file.lastModified)
+
Last commited @helper.html.datetimeago(file.lastModified)
@Html(file.highlightText)
} diff --git a/src/main/twirl/search/issues.scala.html b/src/main/twirl/search/issues.scala.html index d381091..7a9c17a 100644 --- a/src/main/twirl/search/issues.scala.html +++ b/src/main/twirl/search/issues.scala.html @@ -22,7 +22,7 @@ }
Opened by @issue.openedUserName - at @datetime(issue.registeredDate) + @helper.html.datetimeago(issue.registeredDate) @if(issue.commentCount > 0){   @issue.commentCount @plural(issue.commentCount, "comment") } diff --git a/src/main/twirl/wiki/history.scala.html b/src/main/twirl/wiki/history.scala.html index bd2ba49..2052f4c 100644 --- a/src/main/twirl/wiki/history.scala.html +++ b/src/main/twirl/wiki/history.scala.html @@ -36,7 +36,7 @@ @avatar(commit, 20) @user(commit.authorName, commit.authorEmailAddress) - @datetime(commit.authorTime): @commit.shortMessage + @helper.html.datetimeago(commit.authorTime): @commit.shortMessage } diff --git a/src/main/twirl/wiki/page.scala.html b/src/main/twirl/wiki/page.scala.html index 8d103d8..74dabb2 100644 --- a/src/main/twirl/wiki/page.scala.html +++ b/src/main/twirl/wiki/page.scala.html @@ -12,7 +12,7 @@
  • @pageName

    - @page.committer edited this page at @datetime(page.time) + @page.committer edited this page @helper.html.datetimeago(page.time)