Refactored, sorry.

This commit is contained in:
Shintaro Murakami
2014-10-19 01:22:31 +09:00
parent e33dd9008b
commit 5b875d7c73

View File

@@ -45,15 +45,13 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
*/ */
def datetimeAgoRecentOnly(date: Date): String = { def datetimeAgoRecentOnly(date: Date): String = {
val duration = new Date().getTime - date.getTime val duration = new Date().getTime - date.getTime
val list = timeUnits.map(tuple => (duration / tuple._1, tuple._2)).filter(tuple => tuple._1 > 0) timeUnits.find(tuple => duration / tuple._1 > 0) match {
if (list.isEmpty) case Some((_, "month")) => s"on ${new SimpleDateFormat("d MMM", Locale.ENGLISH).format(date)}"
"just now" case Some((_, "year")) => s"on ${new SimpleDateFormat("d MMM yyyy", Locale.ENGLISH).format(date)}"
else { case Some((unitValue, unitString)) =>
list.head match { val value = duration / unitValue
case (_, "month") => s"on ${new SimpleDateFormat("d MMM", Locale.ENGLISH).format(date)}" s"${value} ${unitString}${if (value > 1) "s" else ""} ago"
case (_, "year") => s"on ${new SimpleDateFormat("d MMM yyyy", Locale.ENGLISH).format(date)}" case None => "just now"
case (value, unitString) => s"${value} ${unitString}${if (value > 1) "s" else ""} ago"
}
} }
} }