RelativeTimeFormat.java
11.1 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.i18n.I18n
* org.apache.commons.lang.StringUtils
* org.joda.time.Period
*/
package com.day.cq.commons.date;
import com.day.cq.i18n.I18n;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.ResourceBundle;
import org.apache.commons.lang.StringUtils;
import org.joda.time.Period;
public class RelativeTimeFormat {
private static final String PATTERN_CHARS = "dDhmMrsyY";
public static final String SHORT = "r";
public static final String FULL = "y M d h m s";
public static final String FACEBOOK = "r";
public static final String GMAIL = "Y";
private final ResourceBundle bundle;
private final char[] compiledPattern;
private final Calendar calendar;
private final Calendar now;
private final SimpleDateFormat timeFmt;
private final SimpleDateFormat shortFmt;
private final SimpleDateFormat longFmt;
private Locale locale;
private Period period;
private I18n i18n;
public RelativeTimeFormat(String pattern) {
this(pattern, null, null, null, null);
}
public RelativeTimeFormat(String pattern, ResourceBundle bundle) {
this(pattern, null, null, null, bundle);
}
public RelativeTimeFormat(String pattern, String timePattern, String shortPattern, String longPattern, ResourceBundle bundle) {
if (StringUtils.isBlank((String)pattern)) {
throw new IllegalArgumentException("pattern may not be null or empty");
}
this.bundle = bundle;
this.getLocale();
this.i18n = new I18n(bundle);
this.compiledPattern = this.compile(pattern);
this.calendar = Calendar.getInstance(this.locale);
this.now = Calendar.getInstance(this.locale);
this.timeFmt = new SimpleDateFormat(StringUtils.defaultIfEmpty((String)timePattern, (String)this.i18n.get("HH:mm", "Java date format for a time (http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html)")), this.locale);
this.shortFmt = new SimpleDateFormat(StringUtils.defaultIfEmpty((String)shortPattern, (String)this.i18n.get("MMM d", "Java date format for a date (http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html)")), this.locale);
this.longFmt = new SimpleDateFormat(StringUtils.defaultIfEmpty((String)longPattern, (String)this.i18n.get("MM/dd/yyyy", "Java date format for a date (http://java.sun.com/j2se/1.5.0/docs/api/java/text/SimpleDateFormat.html)")), this.locale);
}
public String format(long start) {
return this.format(start, System.currentTimeMillis(), false);
}
public String format(long start, boolean ago) {
return this.format(start, System.currentTimeMillis(), ago);
}
public String format(long start, long end) {
return this.format(start, end, false);
}
public String format(long start, long end, boolean ago) {
this.calendar.setTimeInMillis(start);
this.now.setTimeInMillis(end);
this.period = new Period(start, end);
StringBuilder buffer = new StringBuilder();
int i = 0;
for (char c : this.compiledPattern) {
int index = "dDhmMrsyY".indexOf(c);
if (index >= 0) {
if (i == this.compiledPattern.length - 1) {
this.fieldFormat(index, buffer, ago);
} else {
this.fieldFormat(index, buffer, false);
}
} else {
buffer.append(c);
}
++i;
}
return buffer.toString();
}
private void fieldFormat(int index, StringBuilder buffer, boolean ago) {
int years = this.period.getYears();
int months = this.period.getMonths();
int weeks = this.period.getWeeks();
int days = weeks > 0 ? this.period.getDays() + weeks * 7 : this.period.getDays();
int hours = this.period.getHours();
int minutes = this.period.getMinutes();
int seconds = this.period.getSeconds();
String result = "";
switch (index) {
case 0: {
if (days == 1) {
result = ago ? this.i18n.get("{0} day ago", null, new Object[]{days}) : this.i18n.get("{0} day", null, new Object[]{days});
break;
}
result = ago ? this.i18n.get("{0} days ago", null, new Object[]{days}) : this.i18n.get("{0} days", null, new Object[]{days});
break;
}
case 1: {
buffer.append(this.getDynamicDate(months, weeks, days));
break;
}
case 2: {
if (hours == 1) {
result = ago ? this.i18n.get("{0} hour ago", null, new Object[]{hours}) : this.i18n.get("{0} hour", null, new Object[]{hours});
break;
}
result = ago ? this.i18n.get("{0} hours ago", null, new Object[]{hours}) : this.i18n.get("{0} hours", null, new Object[]{hours});
break;
}
case 3: {
if (minutes == 1) {
result = ago ? this.i18n.get("{0} minute ago", null, new Object[]{minutes}) : this.i18n.get("{0} minute", null, new Object[]{minutes});
break;
}
result = ago ? this.i18n.get("{0} minutes ago", null, new Object[]{minutes}) : this.i18n.get("{0} minutes", null, new Object[]{minutes});
break;
}
case 4: {
if (months == 1) {
result = ago ? this.i18n.get("{0} month ago", null, new Object[]{months}) : this.i18n.get("{0} month", null, new Object[]{months});
break;
}
result = ago ? this.i18n.get("{0} months ago", null, new Object[]{months}) : this.i18n.get("{0} months", null, new Object[]{months});
break;
}
case 5: {
buffer.append(this.getRelative(years, months, days, hours, minutes, seconds, ago));
break;
}
case 6: {
if (seconds < 1) {
result = this.i18n.get("now", null, new Object[]{seconds});
break;
}
if (seconds == 1) {
result = ago ? this.i18n.get("{0} second ago", null, new Object[]{seconds}) : this.i18n.get("{0} second", null, new Object[]{seconds});
break;
}
result = ago ? this.i18n.get("{0} seconds ago", null, new Object[]{seconds}) : this.i18n.get("{0} seconds", null, new Object[]{seconds});
break;
}
case 7: {
if (years == 1) {
result = ago ? this.i18n.get("{0} year ago", null, new Object[]{years}) : this.i18n.get("{0} year", null, new Object[]{years});
break;
}
result = ago ? this.i18n.get("{0} years ago", null, new Object[]{years}) : this.i18n.get("{0} years", null, new Object[]{years});
break;
}
case 8: {
buffer.append(this.getDynamicDate(months, weeks, days));
if (this.calendar.get(1) != this.now.get(1) || months != 0 || weeks >= 2) break;
buffer.append(" (").append(this.i18n.get("{0}", null, new Object[]{this.getRelative(years, months, days, hours, minutes, seconds, true)})).append(")");
break;
}
}
buffer.append(result);
}
private String getRelative(int years, int months, int days, int hours, int minutes, int seconds, boolean ago) {
String label = "";
if (0 < years) {
label = years == 1 ? (ago ? this.i18n.get("{0} year ago", null, new Object[]{years}) : this.i18n.get("{0} year", null, new Object[]{years})) : (ago ? this.i18n.get("{0} years ago", null, new Object[]{years}) : this.i18n.get("{0} years", null, new Object[]{years}));
} else if (0 < months) {
label = months == 1 ? (ago ? this.i18n.get("{0} month ago", null, new Object[]{months}) : this.i18n.get("{0} month", null, new Object[]{months})) : (ago ? this.i18n.get("{0} months ago", null, new Object[]{months}) : this.i18n.get("{0} months", null, new Object[]{months}));
} else if (0 < days) {
int count = days;
label = count == 1 ? (ago ? this.i18n.get("{0} day ago", null, new Object[]{count}) : this.i18n.get("{0} day", null, new Object[]{count})) : (ago ? this.i18n.get("{0} days ago", null, new Object[]{count}) : this.i18n.get("{0} days", null, new Object[]{count}));
} else if (0 < hours) {
label = hours == 1 ? (ago ? this.i18n.get("{0} hour ago", null, new Object[]{hours}) : this.i18n.get("{0} hour", null, new Object[]{hours})) : (ago ? this.i18n.get("{0} hours ago", null, new Object[]{hours}) : this.i18n.get("{0} hours", null, new Object[]{hours}));
} else if (0 < minutes) {
label = minutes == 1 ? (ago ? this.i18n.get("{0} minute ago", null, new Object[]{minutes}) : this.i18n.get("{0} minute", null, new Object[]{minutes})) : (ago ? this.i18n.get("{0} minutes ago", null, new Object[]{minutes}) : this.i18n.get("{0} minutes", null, new Object[]{minutes}));
} else if (0 <= seconds) {
label = seconds < 1 ? this.i18n.get("now", null, new Object[]{seconds}) : (seconds == 1 ? (ago ? this.i18n.get("{0} second ago", null, new Object[]{seconds}) : this.i18n.get("{0} second", null, new Object[]{seconds})) : (ago ? this.i18n.get("{0} seconds ago", null, new Object[]{seconds}) : this.i18n.get("{0} seconds", null, new Object[]{seconds})));
}
return label;
}
private String getDynamicDate(int months, int weeks, int days) {
if (this.calendar.get(1) < this.now.get(1)) {
return this.longFmt.format(this.calendar.getTime());
}
if (months > 0 || weeks >= 2) {
return this.shortFmt.format(this.calendar.getTime());
}
if (days > 0) {
return this.shortFmt.format(this.calendar.getTime());
}
return this.timeFmt.format(this.calendar.getTime());
}
private char[] compile(String pattern) {
StringBuilder buffer = new StringBuilder();
int length = pattern.length();
for (int i = 0; i < length; ++i) {
char c = pattern.charAt(i);
if (!(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')) {
buffer.append(c);
continue;
}
if ("dDhmMrsyY".indexOf(c) >= 0) {
buffer.append(c);
continue;
}
throw new IllegalArgumentException("illegal pattern character \"'" + c + "'");
}
return buffer.toString().toCharArray();
}
private Locale getLocale() {
if (null == this.locale && null != this.bundle) {
this.locale = this.bundle.getLocale();
} else if (null != this.locale) {
return this.locale;
}
if (null == this.locale) {
this.locale = Locale.getDefault();
}
return this.locale;
}
}