summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/utils/TimeUtils.java
blob: 6cc4dcb5390eab61e4ee2d72b888bb789135f9e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
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 {
	public static final long MIN = 1000 * 60L;

	public static final long HALFHOUR = MIN * 30L;

	public static final long ONEHOUR = HALFHOUR * 2;

	public static final long ONEDAY = ONEHOUR * 24L;

	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) {
		return (System.currentTimeMillis() - date.getTime()) < ONEDAY;
	}

	/**
	 * Returns true if date is yesterday.
	 * 
	 * @param date
	 * @return true if date is yesterday
	 */
	public static boolean isYesterday(Date date) {
		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) {
			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) {
			hours++;
		}
		return hours;
	}

	/**
	 * 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) {
		if (isToday(date) || isYesterday(date)) {
			int mins = minutesAgo(date, true);
			if (mins >= 120) {
				if (css) {
					return "age1";
				}
				int hours = hoursAgo(date, true);
				if (hours > 23) {
					return "yesterday";
				} else {
					return hours + " hours ago";
				}
			}
			if (css) {
				return "age0";
			}
			return mins + " min" + (mins > 1 ? "s" : "") + " ago";
		} else {
			if (css) {
				return "age2";
			}
			int days = daysAgo(date);
			if (days < 365) {
				if (days <= 30) {
					return days + " days ago";
				} else if (days <= 90) {
					int weeks = days / 7;
					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) {
				return "1 year ago";
			} else {
				int yr = days / 365;
				days = days % 365;
				int months = (yr * 12) + (days / 30);
				if (months > 23) {
					return yr + " years ago";
				} else {
					return months + " months 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;
	}
}