* @return true if date is today\r
*/\r
public static boolean isToday(Date date, TimeZone timezone) {\r
- Date now = new Date();\r
+ return isToday(date, timezone, new Date());\r
+ }\r
+\r
+\r
+ public static boolean isToday(Date date, TimeZone timezone, Date now) {\r
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");\r
if (timezone != null) {\r
df.setTimeZone(timezone);\r
* @return true if date is yesterday\r
*/\r
public static boolean isYesterday(Date date, TimeZone timezone) {\r
+ return isYesterday(date, timezone, new Date());\r
+ }\r
+\r
+\r
+ public static boolean isYesterday(Date date, TimeZone timezone, Date now) {\r
Calendar cal = Calendar.getInstance();\r
- cal.setTime(new Date());\r
+ cal.setTime(now);\r
cal.add(Calendar.DATE, -1);\r
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");\r
if (timezone != null) {\r
* @return hours ago\r
*/\r
public static int hoursAgo(Date date, boolean roundup) {\r
- long diff = System.currentTimeMillis() - date.getTime();\r
+ return hoursAgo(date, System.currentTimeMillis(), roundup);\r
+ }\r
+\r
+\r
+ public static int hoursAgo(Date date, long now, boolean roundup) {\r
+ long diff = now - date.getTime();\r
int hours = (int) (diff / ONEHOUR);\r
if (roundup && (diff % ONEHOUR) >= HALFHOUR) {\r
hours++;\r
* @return days ago\r
*/\r
public static int daysAgo(Date date) {\r
- long today = ONEDAY * (System.currentTimeMillis()/ONEDAY);\r
+ return daysAgo(date, System.currentTimeMillis());\r
+ }\r
+\r
+ public static int daysAgo(Date date, long now) {\r
+ long today = ONEDAY * (now/ONEDAY);\r
long day = ONEDAY * (date.getTime()/ONEDAY);\r
long diff = today - day;\r
int days = (int) (diff / ONEDAY);\r
* @return the string representation of the duration OR the css class\r
*/\r
private String timeAgo(Date date, boolean css) {\r
- if (isToday(date, timezone) || isYesterday(date, timezone)) {\r
- int mins = minutesAgo(date, true);\r
+ return timeAgo(date, css, System.currentTimeMillis());\r
+ }\r
+\r
+\r
+ public String timeAgo(Date date, boolean css, long now) {\r
+ Date dNow = new Date(now);\r
+ if (isToday(date, timezone, dNow) || isYesterday(date, timezone, dNow)) {\r
+ int mins = minutesAgo(date, now, true);\r
if (mins >= 120) {\r
if (css) {\r
return "age1";\r
}\r
- int hours = hoursAgo(date, true);\r
+ int hours = hoursAgo(date, now, true);\r
if (hours > 23) {\r
return yesterday();\r
} else {\r
}\r
return translate("gb.time.justNow", "just now");\r
} else {\r
- int days = daysAgo(date);\r
+ int days = daysAgo(date, now);\r
if (css) {\r
if (days <= 7) {\r
return "age2";\r
*/\r
package com.gitblit.tests;\r
\r
+import java.util.Calendar;\r
import java.util.Date;\r
+import java.util.TimeZone;\r
\r
import org.junit.Test;\r
\r
return new Date(System.currentTimeMillis() - subtract);\r
}\r
\r
+ private Date offset(long now, long subtract) {\r
+ return new Date(now - subtract);\r
+ }\r
+\r
@Test\r
public void testBasicTimeFunctions() throws Exception {\r
assertEquals(2, TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false));\r
assertEquals("1 year ago", timeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)));\r
assertEquals("13 months ago", timeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)));\r
assertEquals("2 years ago", timeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)));\r
+ }\r
\r
+ @Test\r
+ public void testTimeAgoCss() throws Exception {\r
// css class tests\r
+ TimeUtils timeUtils = new TimeUtils();\r
assertEquals("age0", timeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)));\r
assertEquals("age0", timeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)));\r
assertEquals("age1", timeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)));\r
assertEquals("age2", timeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)));\r
}\r
\r
+\r
+ @Test\r
+ public void testTimeAgoYesterday() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("GMT");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 12);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,35 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,37 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,44 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
+ @Test\r
+ public void testTimeAgoYesterdayCET() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("CET");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 22);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,46 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,47 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,56 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
+\r
+ @Test\r
+ public void testTimeAgoYesterdayPST() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("PST");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 8);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,30 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,32 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,33 * TimeUtils.ONEHOUR), false, now));\r
+ assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
@Test\r
public void testFrequency() {\r
assertEquals(5, TimeUtils.convertFrequencyToMinutes("2 mins", 5));\r
assertEquals(600, TimeUtils.convertFrequencyToMinutes("10 hours", 5));\r
assertEquals(14400, TimeUtils.convertFrequencyToMinutes(" 10 days ", 5));\r
}\r
+\r
+\r
+ @Test\r
+ public void testTimeAgoDaysAgo() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("GMT");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 12);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,37 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,60 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,61 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,84 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("4 days ago", timeUtils.timeAgo(offset(now,85 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
+\r
+\r
+ @Test\r
+ public void testTimeAgoDaysAgoCET() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("CET");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 8);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,32 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,33 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,56 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,57 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,80 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("4 days ago", timeUtils.timeAgo(offset(now,81 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
+\r
+\r
+ @Test\r
+ public void testTimeAgoDaysAgoPST() throws Exception {\r
+ TimeZone myTimezone = TimeZone.getTimeZone("PST");\r
+ TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+ Calendar myCal = Calendar.getInstance(myTimezone);\r
+ myCal.set(Calendar.HOUR_OF_DAY, 22);\r
+ myCal.set(Calendar.MINUTE, 0);\r
+ long now = myCal.getTime().getTime();\r
+\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(now,46 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,47 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(now,70 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,71 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+ assertEquals("3 days ago", timeUtils.timeAgo(offset(now,94 * TimeUtils.ONEHOUR), false, now));\r
+\r
+ assertEquals("4 days ago", timeUtils.timeAgo(offset(now,95 * TimeUtils.ONEHOUR), false, now));\r
+ }\r
+\r
}\r