}\r
\r
\r
- public static boolean isToday(Date date, TimeZone timezone, Date now) {\r
+ static boolean isToday(Date date, TimeZone timezone, Date now) {\r
SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");\r
if (timezone != null) {\r
df.setTimeZone(timezone);\r
}\r
\r
\r
- public static boolean isYesterday(Date date, TimeZone timezone, Date now) {\r
+ static boolean isYesterday(Date date, TimeZone timezone, Date now) {\r
Calendar cal = Calendar.getInstance();\r
cal.setTime(now);\r
cal.add(Calendar.DATE, -1);\r
}\r
\r
\r
- public static int hoursAgo(Date date, long now, boolean roundup) {\r
+ 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
return daysAgo(date, System.currentTimeMillis());\r
}\r
\r
- public static int daysAgo(Date date, long now) {\r
+ 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
}\r
\r
\r
- public String timeAgo(Date date, boolean css, long now) {\r
+ 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
import java.util.zip.ZipEntry;\r
import java.util.zip.ZipInputStream;\r
\r
+import com.gitblit.utils.TimeUtilsTest;\r
import org.eclipse.jgit.api.Git;\r
import org.eclipse.jgit.lib.Repository;\r
import org.eclipse.jgit.lib.RepositoryCache;\r
+++ /dev/null
-/*\r
- * Copyright 2011 gitblit.com.\r
- *\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\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
-import com.gitblit.utils.TimeUtils;\r
-\r
-public class TimeUtilsTest extends GitblitUnitTest {\r
-\r
- private Date offset(long subtract) {\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(3, TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true));\r
-\r
- assertEquals(2, TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false));\r
- assertEquals(3, TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true));\r
-\r
- assertEquals(4, TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY)));\r
- }\r
-\r
- @Test\r
- public void testToday() throws Exception {\r
- assertTrue(TimeUtils.isToday(new Date(), null));\r
- }\r
-\r
- @Test\r
- public void testYesterday() throws Exception {\r
- assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY), null));\r
- }\r
-\r
- @Test\r
- public void testDurations() throws Exception {\r
- TimeUtils timeUtils = new TimeUtils();\r
- assertEquals("1 day", timeUtils.duration(1));\r
- assertEquals("5 days", timeUtils.duration(5));\r
- assertEquals("3 months", timeUtils.duration(75));\r
- assertEquals("12 months", timeUtils.duration(364));\r
- assertEquals("1 year", timeUtils.duration(365 + 0));\r
- assertEquals("1 year", timeUtils.duration(365 + 10));\r
- assertEquals("1 year, 1 month", timeUtils.duration(365 + 15));\r
- assertEquals("1 year, 1 month", timeUtils.duration(365 + 30));\r
- assertEquals("1 year, 1 month", timeUtils.duration(365 + 44));\r
- assertEquals("1 year, 2 months", timeUtils.duration(365 + 45));\r
- assertEquals("1 year, 2 months", timeUtils.duration(365 + 60));\r
-\r
- assertEquals("2 years", timeUtils.duration(2 * 365 + 0));\r
- assertEquals("2 years", timeUtils.duration(2 * 365 + 10));\r
- assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 15));\r
- assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 30));\r
- assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 44));\r
- assertEquals("2 years, 2 months", timeUtils.duration(2 * 365 + 45));\r
- assertEquals("2 years, 2 months", timeUtils.duration(2 * 365 + 60));\r
- }\r
-\r
- @Test\r
- public void testTimeAgo() throws Exception {\r
- // standard time ago tests\r
- TimeUtils timeUtils = new TimeUtils();\r
- assertEquals("just now", timeUtils.timeAgo(offset(1 * TimeUtils.MIN)));\r
- assertEquals("60 mins ago", timeUtils.timeAgo(offset(60 * TimeUtils.MIN)));\r
- assertEquals("2 hours ago", timeUtils.timeAgo(offset(120 * TimeUtils.MIN)));\r
- assertEquals("15 hours ago", timeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)));\r
- assertEquals("yesterday", timeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)));\r
- assertEquals("2 days ago", timeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)));\r
- assertEquals("5 weeks ago", timeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)));\r
- assertEquals("3 months ago", timeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)));\r
- assertEquals("3 months ago", timeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)));\r
- assertEquals("4 months ago", timeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)));\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("age1", timeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)));\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(10, TimeUtils.convertFrequencyToMinutes("10 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
--- /dev/null
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.utils;\r
+\r
+import java.util.Calendar;\r
+import java.util.Date;\r
+import java.util.TimeZone;\r
+\r
+import com.gitblit.tests.GitblitUnitTest;\r
+import org.junit.Test;\r
+\r
+\r
+public class TimeUtilsTest extends GitblitUnitTest\r
+{\r
+\r
+ private Date offset(long subtract) {\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(3, TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true));\r
+\r
+ assertEquals(2, TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false));\r
+ assertEquals(3, TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true));\r
+\r
+ assertEquals(4, TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY)));\r
+ }\r
+\r
+ @Test\r
+ public void testToday() throws Exception {\r
+ assertTrue(TimeUtils.isToday(new Date(), null));\r
+ }\r
+\r
+ @Test\r
+ public void testYesterday() throws Exception {\r
+ assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY), null));\r
+ }\r
+\r
+ @Test\r
+ public void testDurations() throws Exception {\r
+ TimeUtils timeUtils = new TimeUtils();\r
+ assertEquals("1 day", timeUtils.duration(1));\r
+ assertEquals("5 days", timeUtils.duration(5));\r
+ assertEquals("3 months", timeUtils.duration(75));\r
+ assertEquals("12 months", timeUtils.duration(364));\r
+ assertEquals("1 year", timeUtils.duration(365 + 0));\r
+ assertEquals("1 year", timeUtils.duration(365 + 10));\r
+ assertEquals("1 year, 1 month", timeUtils.duration(365 + 15));\r
+ assertEquals("1 year, 1 month", timeUtils.duration(365 + 30));\r
+ assertEquals("1 year, 1 month", timeUtils.duration(365 + 44));\r
+ assertEquals("1 year, 2 months", timeUtils.duration(365 + 45));\r
+ assertEquals("1 year, 2 months", timeUtils.duration(365 + 60));\r
+\r
+ assertEquals("2 years", timeUtils.duration(2 * 365 + 0));\r
+ assertEquals("2 years", timeUtils.duration(2 * 365 + 10));\r
+ assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 15));\r
+ assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 30));\r
+ assertEquals("2 years, 1 month", timeUtils.duration(2 * 365 + 44));\r
+ assertEquals("2 years, 2 months", timeUtils.duration(2 * 365 + 45));\r
+ assertEquals("2 years, 2 months", timeUtils.duration(2 * 365 + 60));\r
+ }\r
+\r
+ @Test\r
+ public void testTimeAgo() throws Exception {\r
+ // standard time ago tests\r
+ TimeUtils timeUtils = new TimeUtils();\r
+ assertEquals("just now", timeUtils.timeAgo(offset(1 * TimeUtils.MIN)));\r
+ assertEquals("60 mins ago", timeUtils.timeAgo(offset(60 * TimeUtils.MIN)));\r
+ assertEquals("2 hours ago", timeUtils.timeAgo(offset(120 * TimeUtils.MIN)));\r
+ assertEquals("15 hours ago", timeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)));\r
+ assertEquals("yesterday", timeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)));\r
+ assertEquals("2 days ago", timeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)));\r
+ assertEquals("5 weeks ago", timeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)));\r
+ assertEquals("3 months ago", timeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)));\r
+ assertEquals("3 months ago", timeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)));\r
+ assertEquals("4 months ago", timeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)));\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("age1", timeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)));\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(10, TimeUtils.convertFrequencyToMinutes("10 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