James Moger
2011-07-11 30a127621c89ff6afef7ca2f2942fc9a6c64b102
src/com/gitblit/utils/TimeUtils.java
@@ -15,8 +15,15 @@
 */
package com.gitblit.utils;
import java.util.Calendar;
import java.util.Date;
/**
 * Utility class of time functions.
 *
 * @author James Moger
 *
 */
public class TimeUtils {
   public static final long MIN = 1000 * 60L;
@@ -28,26 +35,42 @@
   public static final long ONEYEAR = ONEDAY * 365L;
   @SuppressWarnings("deprecation")
   /**
    * Returns true if date is today.
    *
    * @param date
    * @return true if date is today
    */
   public static boolean isToday(Date date) {
      Date now = new Date();
      return now.getDate() == date.getDate() && now.getMonth() == date.getMonth()
            && now.getYear() == date.getYear();
      return (System.currentTimeMillis() - date.getTime()) < ONEDAY;
   }
   @SuppressWarnings("deprecation")
   /**
    * Returns true if date is yesterday.
    *
    * @param date
    * @return true if date is yesterday
    */
   public static boolean isYesterday(Date date) {
      Date now = new Date();
      return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth()
            && now.getYear() == date.getYear();
      Calendar cal = Calendar.getInstance();
      cal.setTime(date);
      cal.add(Calendar.DATE, 1);
      return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY;
   }
   /**
    * Returns the string representation of the duration as days, months and/or
    * years.
    *
    * @param days
    * @return duration as string in days, months, and/or years
    */
   public static String duration(int days) {
      if (days <= 60) {
         return days + (days > 1 ? " days" : " day");
      } else if (days <= 365) {
      } else if (days < 365) {
         int rem = days % 30;
         return (days / 30) + " months, " + rem + (rem > 1 ? " days" : " day");
         return ((days / 30) + (rem >= 15 ? 1 : 0)) + " months";
      } else {
         int years = days / 365;
         int rem = days % 365;
@@ -56,27 +79,30 @@
            if (rem == 0) {
               return yearsString;
            } else {
               return yearsString + ", " + rem + (rem > 1 ? " days" : " day");
               return yearsString + (rem >= 15 ? ", 1 month" : "");
            }
         } else {
            int months = rem / 30;
            int remDays = rem % 30;
            String monthsString;
            if (months == 0) {
               monthsString = yearsString;
            } else {
               monthsString = yearsString + ", " + months
                     + (months > 1 ? " months" : " month");
            if (remDays >= 15) {
               months++;
            }
            if (remDays == 0) {
               return monthsString;
            } else {
               return monthsString + ", " + remDays + (remDays > 1 ? " days" : " day");
            }
            String monthsString = yearsString + ", " + months
                  + (months > 1 ? " months" : " month");
            return monthsString;
         }
      }
   }
   /**
    * Returns the number of minutes ago between the start time and the end
    * time.
    *
    * @param date
    * @param endTime
    * @param roundup
    * @return difference in minutes
    */
   public static int minutesAgo(Date date, long endTime, boolean roundup) {
      long diff = endTime - date.getTime();
      int mins = (int) (diff / MIN);
@@ -86,10 +112,24 @@
      return mins;
   }
   /**
    * Return the difference in minutes between now and the date.
    *
    * @param date
    * @param roundup
    * @return minutes ago
    */
   public static int minutesAgo(Date date, boolean roundup) {
      return minutesAgo(date, System.currentTimeMillis(), roundup);
   }
   /**
    * Return the difference in hours between now and the date.
    *
    * @param date
    * @param roundup
    * @return hours ago
    */
   public static int hoursAgo(Date date, boolean roundup) {
      long diff = System.currentTimeMillis() - date.getTime();
      int hours = (int) (diff / ONEHOUR);
@@ -99,6 +139,13 @@
      return hours;
   }
   /**
    * Return the difference in days between now and the date.
    *
    * @param date
    * @param roundup
    * @return days ago
    */
   public static int daysAgo(Date date, boolean roundup) {
      long diff = System.currentTimeMillis() - date.getTime();
      int days = (int) (diff / ONEDAY);
@@ -108,19 +155,40 @@
      return days;
   }
   /**
    * Returns the string representation of the duration between now and the
    * date.
    *
    * @param date
    * @return duration as a string
    */
   public static String timeAgo(Date date) {
      return timeAgo(date, false);
   }
   /**
    * Returns the CSS class for the date based on its age from Now.
    *
    * @param date
    * @return the css class
    */
   public static String timeAgoCss(Date date) {
      return timeAgo(date, true);
   }
   /**
    * Returns the string representation of the duration OR the css class for
    * the duration.
    *
    * @param date
    * @param css
    * @return the string representation of the duration OR the css class
    */
   private static String timeAgo(Date date, boolean css) {
      String ago = null;
      if (isToday(date) || isYesterday(date)) {
         int mins = minutesAgo(date, true);
         if (mins > 120) {
         if (mins >= 120) {
            if (css) {
               return "age1";
            }
@@ -128,7 +196,7 @@
            if (hours > 23) {
               ago = "yesterday";
            } else {
               ago = hours + " hour" + (hours > 1 ? "s" : "") + " ago";
               ago = hours + " hours ago";
            }
         } else {
            if (css) {
@@ -143,7 +211,7 @@
         int days = daysAgo(date, true);
         if (days < 365) {
            if (days <= 30) {
               ago = days + " day" + (days > 1 ? "s" : "") + " ago";
               ago = days + " days ago";
            } else if (days <= 90) {
               int weeks = days / 7;
               if (weeks == 12) {
@@ -157,9 +225,7 @@
               if (weeks >= 2) {
                  months++;
               }
               ago = months + " month" + (months > 1 ? "s" : "") + " ago";
            } else {
               ago = days + " day" + (days > 1 ? "s" : "") + " ago";
               ago = months + " months ago";
            }
         } else if (days == 365) {
            ago = "1 year ago";