ALF-10204 Support for specifying the timezone (rather than just offset) for calendar entries when creating/editing, with tests, and use this to improve all day event storage/detection

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@30436 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Nick Burch
2011-09-12 15:38:38 +00:00
parent 0902840a2b
commit a48be26668
2 changed files with 34 additions and 16 deletions

View File

@@ -279,7 +279,7 @@ public class CalendarServiceImplTest
c.set(Calendar.SECOND, 0); c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0); c.set(Calendar.MILLISECOND, 0);
// Neither start nor end are at midnight to start // Neither start nor end are at midnight to start with
assertEquals(false, CalendarEntryDTO.isAllDay(entry)); assertEquals(false, CalendarEntryDTO.isAllDay(entry));
// Set the start to midnight // Set the start to midnight

View File

@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.TimeZone;
import org.alfresco.service.cmr.repository.NodeRef; import org.alfresco.service.cmr.repository.NodeRef;
@@ -35,6 +36,7 @@ import org.alfresco.service.cmr.repository.NodeRef;
public class CalendarEntryDTO implements CalendarEntry, Serializable public class CalendarEntryDTO implements CalendarEntry, Serializable
{ {
private static final long serialVersionUID = -7997650453677545845L; private static final long serialVersionUID = -7997650453677545845L;
private static final TimeZone UTC = TimeZone.getTimeZone("UTC");
private NodeRef nodeRef; private NodeRef nodeRef;
private NodeRef containerNodeRef; private NodeRef containerNodeRef;
@@ -275,10 +277,9 @@ public class CalendarEntryDTO implements CalendarEntry, Serializable
* on a day, and ending at midnight. * on a day, and ending at midnight.
* *
* For a single day event, the start and end dates should be * For a single day event, the start and end dates should be
* the same, and the times for both are midnight. * the same, and the times for both are UTC midnight.
* For a multi day event, the start and end times are midnight, * For a multi day event, the start and end times are UTC midnight,
* for the first and last days respectively. * for the first and last days respectively.
* TODO Timezones!
*/ */
public static boolean isAllDay(CalendarEntry entry) public static boolean isAllDay(CalendarEntry entry)
{ {
@@ -288,11 +289,27 @@ public class CalendarEntryDTO implements CalendarEntry, Serializable
return false; return false;
} }
Calendar start = Calendar.getInstance(); // As of 4.0, all-day events use UTC midnight for consistency
Calendar end = Calendar.getInstance(); Calendar startUTC = Calendar.getInstance();
start.setTime(entry.getStart()); Calendar endUTC = Calendar.getInstance();
end.setTime(entry.getEnd()); startUTC.setTime(entry.getStart());
endUTC.setTime(entry.getEnd());
startUTC.setTimeZone(UTC);
endUTC.setTimeZone(UTC);
// Pre-4.0, the midnights were local time...
Calendar startLocal = Calendar.getInstance();
Calendar endLocal = Calendar.getInstance();
startLocal.setTime(entry.getStart());
endLocal.setTime(entry.getEnd());
// Check for midnight, first in UTC then again in Local time
Calendar[] starts = new Calendar[] { startUTC, startLocal };
Calendar[] ends = new Calendar[] { endUTC, endLocal };
for(int i=0; i<starts.length; i++)
{
Calendar start = starts[i];
Calendar end = ends[i];
if (start.get(Calendar.HOUR_OF_DAY) == 0 && if (start.get(Calendar.HOUR_OF_DAY) == 0 &&
start.get(Calendar.MINUTE) == 0 && start.get(Calendar.MINUTE) == 0 &&
start.get(Calendar.SECOND) == 0 && start.get(Calendar.SECOND) == 0 &&
@@ -300,9 +317,10 @@ public class CalendarEntryDTO implements CalendarEntry, Serializable
end.get(Calendar.MINUTE) == 0 && end.get(Calendar.MINUTE) == 0 &&
end.get(Calendar.SECOND) == 0) end.get(Calendar.SECOND) == 0)
{ {
// Both midnight, counts as all day // Both at midnight, counts as all day
return true; return true;
} }
}
// In any other case, it isn't an all-day // In any other case, it isn't an all-day
return false; return false;