From 27ae9095639bb228a1b7ff86a3ebe4264abf05be Mon Sep 17 00:00:00 2001
From: mschaefers <mschaefers@scoop-gmbh.de>
Date: Thu, 29 Nov 2012 12:33:09 -0500
Subject: [PATCH] feature: when using LdapUserService one can configure Gitblit to fetch all users from ldap that can possibly login. This allows to see newly generated LDAP users instantly in Gitblit. By now an LDAP user had to log in once to appear in GitBlit.

---
 src/com/gitblit/utils/TimeUtils.java |  184 +++++++++++++++++++++++++++++++++++-----------
 1 files changed, 140 insertions(+), 44 deletions(-)

diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java
index ad81463..ec8871c 100644
--- a/src/com/gitblit/utils/TimeUtils.java
+++ b/src/com/gitblit/utils/TimeUtils.java
@@ -15,8 +15,11 @@
  */
 package com.gitblit.utils;
 
+import java.text.MessageFormat;
+import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.ResourceBundle;
 
 /**
  * Utility class of time functions.
@@ -34,6 +37,16 @@
 	public static final long ONEDAY = ONEHOUR * 24L;
 
 	public static final long ONEYEAR = ONEDAY * 365L;
+	
+	private final ResourceBundle translation;
+	
+	public TimeUtils() {
+		this(null);
+	}
+	
+	public TimeUtils(ResourceBundle translation) {
+		this.translation = translation;
+	}
 
 	/**
 	 * Returns true if date is today.
@@ -53,9 +66,10 @@
 	 */
 	public static boolean isYesterday(Date date) {
 		Calendar cal = Calendar.getInstance();
-		cal.setTime(date);
-		cal.add(Calendar.DATE, 1);
-		return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY;
+		cal.setTime(new Date());
+		cal.add(Calendar.DATE, -1);
+		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
+		return df.format(cal.getTime()).equals(df.format(date));
 	}
 
 	/**
@@ -65,21 +79,21 @@
 	 * @param days
 	 * @return duration as string in days, months, and/or years
 	 */
-	public static String duration(int days) {
+	public String duration(int days) {
 		if (days <= 60) {
-			return days + (days > 1 ? " days" : " day");
+			return (days > 1 ? translate(days, "gb.duration.days", "{0} days") : translate("gb.duration.oneDay", "1 day"));
 		} else if (days < 365) {
 			int rem = days % 30;
-			return ((days / 30) + (rem >= 15 ? 1 : 0)) + " months";
+			return translate(((days / 30) + (rem >= 15 ? 1 : 0)), "gb.duration.months", "{0} months");
 		} else {
 			int years = days / 365;
 			int rem = days % 365;
-			String yearsString = years + (years > 1 ? " years" : " year");
+			String yearsString = (years > 1 ? translate(years, "gb.duration.years", "{0} years") : translate("gb.duration.oneYear", "1 year"));
 			if (rem < 30) {
 				if (rem == 0) {
 					return yearsString;
 				} else {
-					return yearsString + (rem >= 15 ? ", 1 month" : "");
+					return yearsString + (rem >= 15 ? (", " + translate("gb.duration.oneMonth", "1 month")): "");
 				}
 			} else {
 				int months = rem / 30;
@@ -87,8 +101,8 @@
 				if (remDays >= 15) {
 					months++;
 				}
-				String monthsString = yearsString + ", " + months
-						+ (months > 1 ? " months" : " month");
+				String monthsString = yearsString + ", "
+						+ (months > 1 ? translate(months, "gb.duration.months", "{0} months") : translate("gb.duration.oneMonth", "1 month"));
 				return monthsString;
 			}
 		}
@@ -143,16 +157,22 @@
 	 * 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();
+	public static int daysAgo(Date date) {
+		long today = ONEDAY * (System.currentTimeMillis()/ONEDAY);
+		long day = ONEDAY * (date.getTime()/ONEDAY);
+		long diff = today - day;
 		int days = (int) (diff / ONEDAY);
-		if (roundup && (diff % ONEDAY) > 0) {
-			days++;
-		}
 		return days;
+	}
+
+	public String today() {
+		return translate("gb.time.today", "today");
+	}
+
+	public String yesterday() {
+		return translate("gb.time.yesterday", "yesterday");
 	}
 
 	/**
@@ -162,7 +182,7 @@
 	 * @param date
 	 * @return duration as a string
 	 */
-	public static String timeAgo(Date date) {
+	public String timeAgo(Date date) {
 		return timeAgo(date, false);
 	}
 
@@ -172,7 +192,7 @@
 	 * @param date
 	 * @return the css class
 	 */
-	public static String timeAgoCss(Date date) {
+	public String timeAgoCss(Date date) {
 		return timeAgo(date, true);
 	}
 
@@ -184,8 +204,7 @@
 	 * @param css
 	 * @return the string representation of the duration OR the css class
 	 */
-	private static String timeAgo(Date date, boolean css) {
-		String ago = null;
+	private String timeAgo(Date date, boolean css) {
 		if (isToday(date) || isYesterday(date)) {
 			int mins = minutesAgo(date, true);
 			if (mins >= 120) {
@@ -194,52 +213,129 @@
 				}
 				int hours = hoursAgo(date, true);
 				if (hours > 23) {
-					ago = "yesterday";
+					return yesterday();
 				} else {
-					ago = hours + " hours ago";
+					return translate(hours, "gb.time.hoursAgo", "{0} hours ago");
 				}
-			} else {
-				if (css) {
-					return "age0";
-				}
-				ago = mins + " min" + (mins > 1 ? "s" : "") + " ago";
 			}
-		} else {
 			if (css) {
-				return "age2";
+				return "age0";
 			}
-			int days = daysAgo(date, true);
+			if (mins > 2) {
+				return translate(mins, "gb.time.minsAgo", "{0} mins ago");
+			}
+			return translate("gb.time.justNow", "just now");
+		} else {
+			int days = daysAgo(date);
+			if (css) {
+				if (days <= 7) {
+					return "age2";
+				} if (days <= 30) {
+					return "age3";
+				} else {
+					return "age4";
+				}
+			}
 			if (days < 365) {
 				if (days <= 30) {
-					ago = days + " days ago";
+					return translate(days, "gb.time.daysAgo", "{0} days ago");
 				} else if (days <= 90) {
 					int weeks = days / 7;
 					if (weeks == 12) {
-						ago = "3 months ago";
+						return translate(3, "gb.time.monthsAgo", "{0} months ago");
 					} else {
-						ago = weeks + " weeks ago";
+						return translate(weeks, "gb.time.weeksAgo", "{0} weeks ago");
 					}
-				} else if (days > 90) {
-					int months = days / 30;
-					int weeks = (days % 30) / 7;
-					if (weeks >= 2) {
-						months++;
-					}
-					ago = months + " months ago";
 				}
+				int months = days / 30;
+				int weeks = (days % 30) / 7;
+				if (weeks >= 2) {
+					months++;
+				}
+				return translate(months, "gb.time.monthsAgo", "{0} months ago");
 			} else if (days == 365) {
-				ago = "1 year ago";
+				return translate("gb.time.oneYearAgo", "1 year ago");
 			} else {
 				int yr = days / 365;
 				days = days % 365;
 				int months = (yr * 12) + (days / 30);
 				if (months > 23) {
-					ago = yr + " years ago";
+					return translate(yr, "gb.time.yearsAgo", "{0} years ago");
 				} else {
-					ago = months + " months ago";
+					return translate(months, "gb.time.monthsAgo", "{0} months ago");
 				}
 			}
 		}
-		return ago;
+	}
+	
+	public String inFuture(Date date) {
+		long diff = date.getTime() - System.currentTimeMillis();
+		if (diff > ONEDAY) {
+			double days = ((double) diff)/ONEDAY;
+			return translate((int) Math.round(days), "gb.time.inDays", "in {0} days");
+		} else {
+			double hours = ((double) diff)/ONEHOUR;
+			if (hours > 2) {
+				return translate((int) Math.round(hours), "gb.time.inHours", "in {0} hours");
+			} else {
+				int mins = (int) (diff/MIN);
+				return translate(mins, "gb.time.inMinutes", "in {0} minutes");
+			}
+		}
+	}
+	
+	private String translate(String key, String defaultValue) {
+		String value = defaultValue;
+		if (translation != null && translation.containsKey(key)) {
+			String aValue = translation.getString(key);
+			if (!StringUtils.isEmpty(aValue)) {
+				value = aValue;
+			}
+		}
+		return value;
+	}
+	
+	private String translate(int val, String key, String defaultPattern) {
+		String pattern = defaultPattern;
+		if (translation != null && translation.containsKey(key)) {
+			String aValue = translation.getString(key);
+			if (!StringUtils.isEmpty(aValue)) {
+				pattern = aValue;
+			}
+		}
+		return MessageFormat.format(pattern, val);
+	}
+
+	/**
+	 * Convert a frequency string into minutes.
+	 * 
+	 * @param frequency
+	 * @return minutes
+	 */
+	public static int convertFrequencyToMinutes(String frequency) {
+		// parse the frequency
+		frequency = frequency.toLowerCase();
+		int mins = 60;
+		if (!StringUtils.isEmpty(frequency)) {
+			try {
+				String str = frequency.trim();
+				if (frequency.indexOf(' ') > -1) {
+					str = str.substring(0, str.indexOf(' ')).trim();
+				}
+				mins = (int) Float.parseFloat(str);
+			} catch (NumberFormatException e) {
+			}
+			if (mins < 5) {
+				mins = 5;
+			}
+		}
+		if (frequency.indexOf("day") > -1) {
+			// convert to minutes
+			mins *= 1440;
+		} else if (frequency.indexOf("hour") > -1) {
+			// convert to minutes
+			mins *= 60;
+		}
+		return mins;
 	}
 }

--
Gitblit v1.9.1