]> source.dussan.org Git - gitblit.git/commitdiff
TimeUtils: Change daysAgo to calculate difference in calendar days 1382/head
authorFlorian Zschocke <f.zschocke+git@gmail.com>
Sat, 23 Oct 2021 22:44:18 +0000 (00:44 +0200)
committerFlorian Zschocke <f.zschocke+git@gmail.com>
Sat, 23 Oct 2021 22:44:18 +0000 (00:44 +0200)
The `daysAgo` method seemed to want to normalize on a calendar day? I
can't really tell what it was trying to do, but the problem is that it
does not take into account any time shift due to time zones so it never
really worked outside of GMT.
So instead a new `calendarDaysAgo` method is added (because I am unsure
on what the `daysAgo` method is trying to do. It can probably be removed).
The new method cleanly calculates difference in calendar days because it
normalizes the two given time stamps on the same time zone.

The `timeAgo` method now used the new method. This fixes #1248.

src/main/java/com/gitblit/utils/TimeUtils.java
src/test/java/com/gitblit/utils/TimeUtilsTest.java

index 1273704706e02d5592bd4a2b72af8ab76df1e66d..6e0d3534a6a1f2822d41b7da76c2fef825e27633 100644 (file)
@@ -197,6 +197,57 @@ public class TimeUtils {
                return days;\r
        }\r
 \r
+\r
+\r
+       /**\r
+        * Return the difference in calendar days between a given timestamp and the date.\r
+        * Calendar days means that the difference is calculated between\r
+        * calendar days, not 24 hour increments.\r
+        *\r
+        * This means the result is dependent on the timezone. Only the local\r
+        * time's time zone is used, i.e. both time stamps are interpreted in\r
+        * the given time zone.\r
+        *\r
+        * E.g. if now is 10:00 on 20.10.2020 GMT and the date given is for\r
+        * either 6:00 or 20:00 on 18.10.2020 GMT then the result is two days\r
+        * in both cases.\r
+        *\r
+        *\r
+        * @param date\r
+        *                      Date in the past\r
+        * @param now\r
+        *                      Timestamp representing current time (used for unit tests)\r
+        * @return calendar days ago\r
+        */\r
+       static int calendarDaysAgo(Date date, TimeZone timezone, long now) {\r
+               Calendar cal;\r
+               if (timezone == null) {\r
+                       cal = Calendar.getInstance();\r
+               } else {\r
+                       cal = Calendar.getInstance(timezone);\r
+               }\r
+\r
+               cal.setTimeInMillis(now);\r
+               cal.set(Calendar.MILLISECOND, 0);\r
+               cal.set(Calendar.SECOND, 0);\r
+               cal.set(Calendar.MINUTE, 0);\r
+               cal.set(Calendar.HOUR_OF_DAY, 12);\r
+               long today = cal.getTime().getTime();\r
+\r
+               cal.clear();\r
+               cal.setTime(date);\r
+               cal.set(Calendar.MILLISECOND, 0);\r
+               cal.set(Calendar.SECOND, 0);\r
+               cal.set(Calendar.MINUTE, 0);\r
+               cal.set(Calendar.HOUR_OF_DAY, 12);\r
+               long day = cal.getTime().getTime();\r
+\r
+               long diff = today - day;\r
+               int days = (int) (diff / ONEDAY);\r
+               return days;\r
+       }\r
+\r
+\r
        public String today() {\r
                return translate("gb.time.today", "today");\r
        }\r
@@ -262,7 +313,7 @@ public class TimeUtils {
                        }\r
                        return translate("gb.time.justNow", "just now");\r
                } else {\r
-                       int days = daysAgo(date, now);\r
+                       int days = calendarDaysAgo(date, timezone, now);\r
                        if (css) {\r
                                if (days <= 7) {\r
                                        return "age2";\r
index 77710e064efc6c74b942e3e4c8db6371f080beb4..0e58a364efe2060c93bb48f729e707bcd9c4e414 100644 (file)
@@ -263,4 +263,428 @@ public class TimeUtilsTest extends GitblitUnitTest
                assertEquals("4 days ago", timeUtils.timeAgo(offset(now,95 * TimeUtils.ONEHOUR), false, now));\r
        }\r
 \r
+\r
+\r
+       /*\r
+        * Test if time difference is correctly calculated in full calendar days relative to GMT.\r
+        */\r
+       @Test\r
+       public void testCalendarDaysAgoToGmt() {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("GMT");\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(2021, Calendar.AUGUST, 19, 12, 0, 5);\r
+               long now = myCal.getTime().getTime();\r
+               // Date from the same time zone\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now, 11 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now, 12 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 12 * TimeUtils.ONEHOUR + 1 * TimeUtils.MIN), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 24 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 36 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  36 * TimeUtils.ONEHOUR + 10 * 1000), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  48 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  60 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(offset(now,  61 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+\r
+               // What if we get passed a date created from a UTC timestamp that came from a different time zone?\r
+               // CET in August is +2 hours from GMT (CEST). So the day border is shifted two hours forward\r
+\r
+               Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("CET"));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 19, 8, 0, 5);\r
+               Date date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 19, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 19, 1, 30, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 0, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 23, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 3, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 1, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               // Now we travel westwards.\r
+               // PST in August is -7 hours from GMT (PDT). So the day border is shifted seven hours back\r
+\r
+               cal = Calendar.getInstance(TimeZone.getTimeZone("PST"));\r
+               cal.clear();\r
+\r
+               cal.set(2021, Calendar.AUGUST, 19, 5, 0, 0);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 19, 0, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 17, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 16, 55, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 18, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 17, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 16, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 17, 1, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2021, Calendar.AUGUST, 16, 17, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2021, Calendar.AUGUST, 16, 14, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+       }\r
+\r
+\r
+       /*\r
+        * Test if time difference is correctly calculated in full calendar days relative to CET.\r
+        */\r
+       @Test\r
+       public void testCalendarDaysAgoToCet() {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("CET");\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(2020, Calendar.JUNE, 5, 12, 0, 5);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               // Date from the same time zone\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now, 11 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now, 12 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 12 * TimeUtils.ONEHOUR + 1 * TimeUtils.MIN), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 24 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now, 36 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now, 36 * TimeUtils.ONEHOUR + 10 * 1000), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now, 48 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now, 60 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(offset(now, 61 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+\r
+               // What if we get passed a date created from a UTC timestamp that came from a different time zone?\r
+               // IST in June is +3:30 hours from CEST. So the day border is shifted three and a half hours forward\r
+\r
+               Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("IST"));\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 13, 0, 5);\r
+               Date date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 3, 30, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 3, 29, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 0, 0, 0);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 10, 0, 0);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 4, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 3, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 4, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 3, 20, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               // Now we travel westwards to New York.\r
+               // EST in June is -6 hours from CEST (EDT). So the day border is shifted six hours back\r
+\r
+               cal = Calendar.getInstance(TimeZone.getTimeZone("EST5EDT"));\r
+               cal.clear();\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 5, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 5, 0, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 18, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 17, 59, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 4, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 19, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 17, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 3, 8, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2020, Calendar.JUNE, 2, 18, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2020, Calendar.JUNE, 2, 17, 20, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+       }\r
+\r
+\r
+       /*\r
+        * Test if time difference is correctly calculated in full calendar days relative to AET (Australia).\r
+        */\r
+       @Test\r
+       public void testCalendarDaysAgoToAet() {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("AET");\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(2022, Calendar.FEBRUARY, 22, 12, 0, 5);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               // Date from the same time zone\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now, 11 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(offset(now,  12 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now,  12 * TimeUtils.ONEHOUR + 1 * TimeUtils.MIN), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now,  24 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(offset(now,  36 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  36 * TimeUtils.ONEHOUR + 10 * 1000), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  48 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(offset(now,  60 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(offset(now,  61 * TimeUtils.ONEHOUR ), myTimezone, now));\r
+\r
+\r
+               // What if we get passed a date created from a UTC timestamp that came from a different time zone?\r
+               // NZ in February is +2 hours from AEDT. So the day border is shifted two hours forward\r
+\r
+               Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("NZ"));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 22, 12, 0, 5);\r
+               Date date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 22, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 22, 1, 45, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 22, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 1, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 10, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 1, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               // Now we travel westwards to Europe.\r
+               // CET in February is -10 hours from AEDT. So the day border is shifted ten hours back\r
+\r
+               cal = Calendar.getInstance(TimeZone.getTimeZone("CET"));\r
+               cal.clear();\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 22, 2, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 22, 0, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 14, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 13, 30, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 7, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 15, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 13, 59, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 1, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 19, 14, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 19, 9, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               // Lets continue even further west.\r
+               // AST in February is -15 hours from AEDT. So the day border is shifted fifteen hours back\r
+\r
+               cal = Calendar.getInstance(TimeZone.getTimeZone("America/Curacao"));\r
+               cal.clear();\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 21, 0, 0);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 12, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 9, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(0, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 21, 8, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 19, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 10, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(1, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 20, 8, 50, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 19, 17, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 19, 9, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(2, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+\r
+\r
+               cal.set(2022, Calendar.FEBRUARY, 19, 7, 0, 5);\r
+               date = cal.getTime();\r
+               assertEquals(3, TimeUtils.calendarDaysAgo(date, myTimezone, now));\r
+       }\r
+\r
 }\r