summaryrefslogtreecommitdiffstats
path: root/src/com/gitblit/utils/TimeUtils.java
blob: c4e5b59377aa0136479687ca196e3d3ed9caf6b4 (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
package com.gitblit.utils;

import java.util.Date;

public class TimeUtils {
	private final static long min = 1000 * 60l;

	private final static long halfhour = min * 30l;

	private final static long onehour = halfhour * 2;

	private final static long oneday = onehour * 24l;

	@SuppressWarnings("deprecation")
	public static boolean isToday(Date date) {
		Date now = new Date();
		return now.getDate() == date.getDate() && now.getMonth() == date.getMonth() && now.getYear() == date.getYear();
	}

	@SuppressWarnings("deprecation")
	public static boolean isYesterday(Date date) {
		Date now = new Date();
		return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth() && now.getYear() == date.getYear();
	}

	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) + " months, " + rem + (rem > 1 ? " days" : " day");
		} 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 + (rem > 1 ? " days" : " day");
				}
			} else {
				int months = rem / 30;
				int remDays = (rem % 30);
				String monthsString;
				if (months == 0) {
					monthsString = yearsString;
				} else {
					monthsString = yearsString + ", " + months + (months > 1 ? " months" : " month");
				}
				if (remDays == 0) {
					return  monthsString;
				} else {
					return monthsString + ", " + remDays + (remDays > 1 ? " days":" day");
				}
			}
		}
	}

	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;
	}

	public static int minutesAgo(Date date, boolean roundup) {
		return minutesAgo(date, System.currentTimeMillis(), roundup);
	}

	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;
	}

	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 days;
	}

	public static String timeAgo(Date date) {
		return timeAgo(date, false);
	}

	public static String timeAgoCss(Date date) {
		return timeAgo(date, true);
	}

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