lemval
2012-01-31 1c30dad2115fc513791d8a5b292ad0f7d7b85749
commit | author | age
f13c4c 1 /*
JM 2  * Copyright 2011 gitblit.com.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
5fe7df 16 package com.gitblit.utils;
JM 17
29692a 18 import java.text.SimpleDateFormat;
793f76 19 import java.util.Calendar;
5fe7df 20 import java.util.Date;
JM 21
d9f687 22 /**
JM 23  * Utility class of time functions.
24  * 
25  * @author James Moger
26  * 
27  */
87cc1e 28 public class TimeUtils {
2a7306 29     public static final long MIN = 1000 * 60L;
5fe7df 30
2a7306 31     public static final long HALFHOUR = MIN * 30L;
5fe7df 32
2a7306 33     public static final long ONEHOUR = HALFHOUR * 2;
5fe7df 34
2a7306 35     public static final long ONEDAY = ONEHOUR * 24L;
JM 36
37     public static final long ONEYEAR = ONEDAY * 365L;
5fe7df 38
d9f687 39     /**
JM 40      * Returns true if date is today.
41      * 
42      * @param date
43      * @return true if date is today
44      */
5fe7df 45     public static boolean isToday(Date date) {
008322 46         return (System.currentTimeMillis() - date.getTime()) < ONEDAY;
5fe7df 47     }
JM 48
d9f687 49     /**
JM 50      * Returns true if date is yesterday.
51      * 
52      * @param date
53      * @return true if date is yesterday
54      */
5fe7df 55     public static boolean isYesterday(Date date) {
793f76 56         Calendar cal = Calendar.getInstance();
29692a 57         cal.setTime(new Date());
JM 58         cal.add(Calendar.DATE, -1);
59         SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
60         return df.format(cal.getTime()).equals(df.format(date));
5fe7df 61     }
JM 62
d9f687 63     /**
JM 64      * Returns the string representation of the duration as days, months and/or
65      * years.
66      * 
67      * @param days
68      * @return duration as string in days, months, and/or years
69      */
45c0d6 70     public static String duration(int days) {
JM 71         if (days <= 60) {
72             return days + (days > 1 ? " days" : " day");
28d6b2 73         } else if (days < 365) {
45c0d6 74             int rem = days % 30;
f1720c 75             return ((days / 30) + (rem >= 15 ? 1 : 0)) + " months";
45c0d6 76         } else {
JM 77             int years = days / 365;
78             int rem = days % 365;
79             String yearsString = years + (years > 1 ? " years" : " year");
80             if (rem < 30) {
81                 if (rem == 0) {
82                     return yearsString;
83                 } else {
28d6b2 84                     return yearsString + (rem >= 15 ? ", 1 month" : "");
45c0d6 85                 }
JM 86             } else {
87                 int months = rem / 30;
2a7306 88                 int remDays = rem % 30;
28d6b2 89                 if (remDays >= 15) {
JM 90                     months++;
45c0d6 91                 }
28d6b2 92                 String monthsString = yearsString + ", " + months
JM 93                         + (months > 1 ? " months" : " month");
94                 return monthsString;
45c0d6 95             }
JM 96         }
97     }
98
d9f687 99     /**
JM 100      * Returns the number of minutes ago between the start time and the end
101      * time.
102      * 
103      * @param date
104      * @param endTime
105      * @param roundup
106      * @return difference in minutes
107      */
5fe7df 108     public static int minutesAgo(Date date, long endTime, boolean roundup) {
JM 109         long diff = endTime - date.getTime();
2a7306 110         int mins = (int) (diff / MIN);
JM 111         if (roundup && (diff % MIN) >= 30) {
5fe7df 112             mins++;
2a7306 113         }
5fe7df 114         return mins;
JM 115     }
116
d9f687 117     /**
JM 118      * Return the difference in minutes between now and the date.
119      * 
120      * @param date
121      * @param roundup
122      * @return minutes ago
123      */
5fe7df 124     public static int minutesAgo(Date date, boolean roundup) {
JM 125         return minutesAgo(date, System.currentTimeMillis(), roundup);
126     }
127
d9f687 128     /**
JM 129      * Return the difference in hours between now and the date.
130      * 
131      * @param date
132      * @param roundup
133      * @return hours ago
134      */
5fe7df 135     public static int hoursAgo(Date date, boolean roundup) {
JM 136         long diff = System.currentTimeMillis() - date.getTime();
2a7306 137         int hours = (int) (diff / ONEHOUR);
JM 138         if (roundup && (diff % ONEHOUR) >= HALFHOUR) {
5fe7df 139             hours++;
2a7306 140         }
5fe7df 141         return hours;
JM 142     }
143
d9f687 144     /**
JM 145      * Return the difference in days between now and the date.
146      * 
147      * @param date
148      * @return days ago
149      */
712210 150     public static int daysAgo(Date date) {
JM 151         long today = ONEDAY * (System.currentTimeMillis()/ONEDAY);
152         long day = ONEDAY * (date.getTime()/ONEDAY);
153         long diff = today - day;
2a7306 154         int days = (int) (diff / ONEDAY);
5fe7df 155         return days;
JM 156     }
157
d9f687 158     /**
JM 159      * Returns the string representation of the duration between now and the
160      * date.
161      * 
162      * @param date
163      * @return duration as a string
164      */
5fe7df 165     public static String timeAgo(Date date) {
JM 166         return timeAgo(date, false);
167     }
168
d9f687 169     /**
JM 170      * Returns the CSS class for the date based on its age from Now.
171      * 
172      * @param date
173      * @return the css class
174      */
5fe7df 175     public static String timeAgoCss(Date date) {
JM 176         return timeAgo(date, true);
177     }
178
d9f687 179     /**
JM 180      * Returns the string representation of the duration OR the css class for
181      * the duration.
182      * 
183      * @param date
184      * @param css
185      * @return the string representation of the duration OR the css class
186      */
5fe7df 187     private static String timeAgo(Date date, boolean css) {
JM 188         if (isToday(date) || isYesterday(date)) {
189             int mins = minutesAgo(date, true);
28d6b2 190             if (mins >= 120) {
5fe7df 191                 if (css) {
JM 192                     return "age1";
193                 }
194                 int hours = hoursAgo(date, true);
195                 if (hours > 23) {
88598b 196                     return "yesterday";
5fe7df 197                 } else {
88598b 198                     return hours + " hours ago";
5fe7df 199                 }
JM 200             }
88598b 201             if (css) {
JM 202                 return "age0";
203             }
204             return mins + " min" + (mins > 1 ? "s" : "") + " ago";
5fe7df 205         } else {
JM 206             if (css) {
207                 return "age2";
208             }
712210 209             int days = daysAgo(date);
5fe7df 210             if (days < 365) {
JM 211                 if (days <= 30) {
88598b 212                     return days + " days ago";
5fe7df 213                 } else if (days <= 90) {
JM 214                     int weeks = days / 7;
2a7306 215                     if (weeks == 12) {
88598b 216                         return "3 months ago";
2a7306 217                     } else {
88598b 218                         return weeks + " weeks ago";
2a7306 219                     }
JM 220                 }
88598b 221                 int months = days / 30;
JM 222                 int weeks = (days % 30) / 7;
223                 if (weeks >= 2) {
224                     months++;
225                 }
226                 return months + " months ago";
5fe7df 227             } else if (days == 365) {
88598b 228                 return "1 year ago";
5fe7df 229             } else {
JM 230                 int yr = days / 365;
231                 days = days % 365;
232                 int months = (yr * 12) + (days / 30);
233                 if (months > 23) {
88598b 234                     return yr + " years ago";
5fe7df 235                 } else {
88598b 236                     return months + " months ago";
5fe7df 237                 }
JM 238             }
239         }
608ece 240     }
831469 241
JM 242     /**
243      * Convert a frequency string into minutes.
244      * 
245      * @param frequency
246      * @return minutes
247      */
248     public static int convertFrequencyToMinutes(String frequency) {
249         // parse the frequency
250         frequency = frequency.toLowerCase();
251         int mins = 60;
252         if (!StringUtils.isEmpty(frequency)) {
253             try {
143fc9 254                 String str = frequency.trim();
831469 255                 if (frequency.indexOf(' ') > -1) {
143fc9 256                     str = str.substring(0, str.indexOf(' ')).trim();
831469 257                 }
JM 258                 mins = (int) Float.parseFloat(str);
259             } catch (NumberFormatException e) {
260             }
261             if (mins < 5) {
262                 mins = 5;
263             }
264         }
265         if (frequency.indexOf("day") > -1) {
266             // convert to minutes
143fc9 267             mins *= 1440;
831469 268         } else if (frequency.indexOf("hour") > -1) {
JM 269             // convert to minutes
270             mins *= 60;
271         }
272         return mins;
273     }
5fe7df 274 }