From cb285cbfddfc0b633d6b8cdb4dc0d2bd2b8b51ef Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Thu, 05 Jan 2012 17:34:05 -0500 Subject: [PATCH] Fixed bug in receive hook for repositories in subfolders --- src/com/gitblit/utils/TimeUtils.java | 249 ++++++++++++++++++++++++++++++++++++++++--------- 1 files changed, 203 insertions(+), 46 deletions(-) diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java index 60b525b..6cc4dcb 100644 --- a/src/com/gitblit/utils/TimeUtils.java +++ b/src/com/gitblit/utils/TimeUtils.java @@ -1,119 +1,276 @@ +/* + * Copyright 2011 gitblit.com. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package com.gitblit.utils; +import java.text.SimpleDateFormat; +import java.util.Calendar; import java.util.Date; +/** + * Utility class of time functions. + * + * @author James Moger + * + */ public class TimeUtils { - private final static long min = 1000 * 60l; + public static final long MIN = 1000 * 60L; - private final static long halfhour = min * 30l; + public static final long HALFHOUR = MIN * 30L; - private final static long onehour = halfhour * 2; + public static final long ONEHOUR = HALFHOUR * 2; - private final static long oneday = onehour * 24l; + public static final long ONEDAY = ONEHOUR * 24L; - @SuppressWarnings("deprecation") + public static final long ONEYEAR = ONEDAY * 365L; + + /** + * 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(new Date()); + cal.add(Calendar.DATE, -1); + SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd"); + return df.format(cal.getTime()).equals(df.format(date)); } + /** + * 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) { + int rem = days % 30; + return ((days / 30) + (rem >= 15 ? 1 : 0)) + " months"; + } else { + int years = days / 365; + int rem = days % 365; + String yearsString = years + (years > 1 ? " years" : " year"); + if (rem < 30) { + if (rem == 0) { + return yearsString; + } else { + return yearsString + (rem >= 15 ? ", 1 month" : ""); + } + } else { + int months = rem / 30; + int remDays = rem % 30; + if (remDays >= 15) { + months++; + } + 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); - if (roundup && (diff % min) >= 30) + int mins = (int) (diff / MIN); + if (roundup && (diff % MIN) >= 30) { mins++; + } 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); - if (roundup && (diff % onehour) >= halfhour) + int hours = (int) (diff / ONEHOUR); + if (roundup && (diff % ONEHOUR) >= HALFHOUR) { hours++; + } return hours; } - public static int daysAgo(Date date, boolean roundup) { - long diff = System.currentTimeMillis() - date.getTime(); - int days = (int) (diff / oneday); - if (roundup && (diff % oneday) > 0) - days++; + /** + * Return the difference in days between now and the date. + * + * @param date + * @return days ago + */ + 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); 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"; } int hours = hoursAgo(date, true); if (hours > 23) { - ago = "yesterday"; + return "yesterday"; } else { - ago = hours + " hour" + (hours > 1 ? "s" : "") + " ago"; + return hours + " hours ago"; } - } else { - if (css) { - return "age0"; - } - ago = mins + " min" + (mins > 1 ? "s" : "") + " ago"; } + if (css) { + return "age0"; + } + return mins + " min" + (mins > 1 ? "s" : "") + " ago"; } else { if (css) { return "age2"; } - int days = daysAgo(date, true); + int days = daysAgo(date); if (days < 365) { if (days <= 30) { - ago = days + " day" + (days > 1 ? "s" : "") + " ago"; + return days + " days ago"; } else if (days <= 90) { int weeks = days / 7; - if (weeks == 12) - ago = "3 months ago"; - else - ago = weeks + " weeks ago"; - } else if (days > 90) { - int months = days / 30; - int weeks = (days % 30) / 7; - if (weeks >= 2) - months++; - ago = months + " month" + (months > 1 ? "s" : "") + " ago"; - } else - ago = days + " day" + (days > 1 ? "s" : "") + " ago"; + if (weeks == 12) { + return "3 months ago"; + } else { + return weeks + " weeks ago"; + } + } + int months = days / 30; + int weeks = (days % 30) / 7; + if (weeks >= 2) { + months++; + } + return months + " months ago"; } else if (days == 365) { - ago = "1 year ago"; + return "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 yr + " years ago"; } else { - ago = months + " months ago"; + return months + " months ago"; } } } - return ago; + } + + /** + * 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; + } else if (frequency.indexOf("min") > -1) { + // default mins + } + return mins; } } -- Gitblit v1.9.1