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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
|
/*
* 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.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.ResourceBundle;
import java.util.TimeZone;
/**
* 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;
private final ResourceBundle translation;
private final TimeZone timezone;
public TimeUtils() {
this(null, null);
}
public TimeUtils(ResourceBundle translation, TimeZone timezone) {
this.translation = translation;
this.timezone = timezone;
}
/**
* Returns true if date is today.
*
* @param date
* @return true if date is today
*/
public static boolean isToday(Date date, TimeZone timezone) {
return isToday(date, timezone, new Date());
}
static boolean isToday(Date date, TimeZone timezone, Date now) {
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
if (timezone != null) {
df.setTimeZone(timezone);
}
return df.format(now).equals(df.format(date));
}
/**
* Returns true if date is yesterday.
*
* @param date
* @return true if date is yesterday
*/
public static boolean isYesterday(Date date, TimeZone timezone) {
return isYesterday(date, timezone, new Date());
}
static boolean isYesterday(Date date, TimeZone timezone, Date now) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.add(Calendar.DATE, -1);
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
if (timezone != null) {
df.setTimeZone(timezone);
}
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 String duration(int days) {
if (days <= 60) {
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 translate(((days / 30) + (rem >= 15 ? 1 : 0)), "gb.duration.months", "{0} months");
} else {
int years = days / 365;
int rem = days % 365;
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 ? (", " + translate("gb.duration.oneMonth", "1 month")): "");
}
} else {
int months = rem / 30;
int remDays = rem % 30;
if (remDays >= 15) {
months++;
}
String monthsString = yearsString + ", "
+ (months > 1 ? translate(months, "gb.duration.months", "{0} months") : translate("gb.duration.oneMonth", "1 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) {
return hoursAgo(date, System.currentTimeMillis(), roundup);
}
static int hoursAgo(Date date, long now, boolean roundup) {
long diff = now - 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) {
return daysAgo(date, System.currentTimeMillis());
}
static int daysAgo(Date date, long now) {
long today = ONEDAY * (now/ONEDAY);
long day = ONEDAY * (date.getTime()/ONEDAY);
long diff = today - day;
int days = (int) (diff / ONEDAY);
return days;
}
/**
* Return the difference in calendar days between a given timestamp and the date.
* Calendar days means that the difference is calculated between
* calendar days, not 24 hour increments.
*
* This means the result is dependent on the timezone. Only the local
* time's time zone is used, i.e. both time stamps are interpreted in
* the given time zone.
*
* E.g. if now is 10:00 on 20.10.2020 GMT and the date given is for
* either 6:00 or 20:00 on 18.10.2020 GMT then the result is two days
* in both cases.
*
*
* @param date
* Date in the past
* @param now
* Timestamp representing current time (used for unit tests)
* @return calendar days ago
*/
static int calendarDaysAgo(Date date, TimeZone timezone, long now) {
Calendar cal;
if (timezone == null) {
cal = Calendar.getInstance();
} else {
cal = Calendar.getInstance(timezone);
}
cal.setTimeInMillis(now);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 12);
long today = cal.getTime().getTime();
cal.clear();
cal.setTime(date);
cal.set(Calendar.MILLISECOND, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.HOUR_OF_DAY, 12);
long day = cal.getTime().getTime();
long diff = today - day;
int days = (int) (diff / ONEDAY);
return days;
}
public String today() {
return translate("gb.time.today", "today");
}
public String yesterday() {
return translate("gb.time.yesterday", "yesterday");
}
/**
* Returns the string representation of the duration between now and the
* date.
*
* @param date
* @return duration as a string
*/
public 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 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 String timeAgo(Date date, boolean css) {
return timeAgo(date, css, System.currentTimeMillis());
}
String timeAgo(Date date, boolean css, long now) {
Date dNow = new Date(now);
if (isToday(date, timezone, dNow) || isYesterday(date, timezone, dNow)) {
int mins = minutesAgo(date, now, true);
if (mins >= 120) {
if (css) {
return "age1";
}
int hours = hoursAgo(date, now, true);
if (hours > 23) {
return yesterday();
} else {
return translate(hours, "gb.time.hoursAgo", "{0} hours ago");
}
}
if (css) {
return "age0";
}
if (mins > 2) {
return translate(mins, "gb.time.minsAgo", "{0} mins ago");
}
return translate("gb.time.justNow", "just now");
} else {
int days = calendarDaysAgo(date, timezone, now);
if (css) {
if (days <= 7) {
return "age2";
} if (days <= 30) {
return "age3";
} else {
return "age4";
}
}
if (days < 365) {
if (days <= 30) {
return translate(days, "gb.time.daysAgo", "{0} days ago");
} else if (days <= 90) {
int weeks = days / 7;
if (weeks == 12) {
return translate(3, "gb.time.monthsAgo", "{0} months ago");
} else {
return translate(weeks, "gb.time.weeksAgo", "{0} weeks 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) {
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) {
return translate(yr, "gb.time.yearsAgo", "{0} years ago");
} else {
return translate(months, "gb.time.monthsAgo", "{0} months 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
* @param minimumMins
* @return minutes
*/
public static int convertFrequencyToMinutes(String frequency, int minimumMins) {
// parse the frequency
frequency = frequency.toLowerCase();
int mins = minimumMins;
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 < minimumMins) {
mins = minimumMins;
}
if (frequency.indexOf("day") > -1) {
// convert to minutes
mins *= 1440;
} else if (frequency.indexOf("hour") > -1) {
// convert to minutes
mins *= 60;
}
}
return mins;
}
}
|