[CalendarServer-changes] [5177] CalendarServer/trunk/twext

source_changes at macosforge.org source_changes at macosforge.org
Mon Feb 22 13:04:44 PST 2010


Revision: 5177
          http://trac.macosforge.org/projects/calendarserver/changeset/5177
Author:   wsanchez at apple.com
Date:     2010-02-22 13:04:44 -0800 (Mon, 22 Feb 2010)
Log Message:
-----------
del(patches)

Modified Paths:
--------------
    CalendarServer/trunk/twext/__init__.py
    CalendarServer/trunk/twext/patches.py

Added Paths:
-----------
    CalendarServer/trunk/twext/python/datetime.py

Modified: CalendarServer/trunk/twext/__init__.py
===================================================================
--- CalendarServer/trunk/twext/__init__.py	2010-02-22 03:32:54 UTC (rev 5176)
+++ CalendarServer/trunk/twext/__init__.py	2010-02-22 21:04:44 UTC (rev 5177)
@@ -19,4 +19,4 @@
 """
 
 from twext import patches
-patches                         # pacify pyflakes
+patches; del(patches)

Modified: CalendarServer/trunk/twext/patches.py
===================================================================
--- CalendarServer/trunk/twext/patches.py	2010-02-22 03:32:54 UTC (rev 5176)
+++ CalendarServer/trunk/twext/patches.py	2010-02-22 21:04:44 UTC (rev 5177)
@@ -18,6 +18,8 @@
 Patches for behavior in Twisted which calendarserver requires to be different.
 """
 
+__all__ = []
+
 from twisted.mail.imap4 import Command
 
 Command._1_RESPONSES += tuple(['BYE'])

Added: CalendarServer/trunk/twext/python/datetime.py
===================================================================
--- CalendarServer/trunk/twext/python/datetime.py	                        (rev 0)
+++ CalendarServer/trunk/twext/python/datetime.py	2010-02-22 21:04:44 UTC (rev 5177)
@@ -0,0 +1,139 @@
+##
+# Copyright (c) 2006-2010 Apple Inc. All rights reserved.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+##
+
+"""
+Date/time Utilities
+"""
+
+__all__ = [
+    "DateTime",
+    "Date",
+    "TimeDelta",
+    "DateOrDateTime",
+    "TimeRange",
+    "UTC",
+]
+
+from datetime import date as Date, datetime as DateTime, timedelta as TimeDelta
+from vobject.icalendar import dateTimeToString, dateToString, utc as UTC
+
+
+class DateOrDateTime (object):
+    def __init__(self, dateOrDateTime, defaultTZ=None):
+        self._dateOrDateTime = dateOrDateTime
+        if isinstance(dateOrDateTime, DateTime):
+            self._isDateTime = True
+        else:
+            self._isDateTime = False
+        self.defaultTZ = defaultTZ
+
+    def _comparableDateTimes(self, other):
+        dt1, dt2 = self.dateTime, other.dateTime
+
+        def getTZInfo(tz):
+            for defaultTZ in (self.defaultTZ, other.defaultTZ):
+                if defaultTZ is not None:
+                    return defaultTZ
+                return tz
+
+        if dt1.tzinfo is None and dt2.tzinfo is not None:
+            dt1 = dt1.replace(tzinfo=getTZInfo(dt2.tzinfo))
+        elif dt1.tzinfo is not None and dt2.tzinfo is None:
+            dt2 = dt2.replace(tzinfo=getTZInfo(dt1.tzinfo))
+
+        return dt1, dt2
+
+    def __cmp__(self, other):
+        dt1, dt2 = self._comparableDateTimes(other)
+
+        if dt1 == dt2:
+            return 0
+        elif dt1 < dt2:
+            return -1
+        else:
+            return 1
+
+    def __sub__(self, other):
+        dt1, dt2 = self._comparableDateTimes(other)
+        return dt1 - dt2
+
+    @property
+    def date(self):
+        if self._isDateTime:
+            return self._dateOrDateTime.date()
+
+    @property
+    def dateTime(self):
+        if not self._isDateTime:
+            d = self._dateOrDateTime
+            return DateTime(d.year, d.month, d.day, tzinfo=self.tz)
+
+    def iCalendarString(self):
+        if self._isDateTime:
+            return dateTimeToString(self._dateOrDateTime)
+        else:
+            return dateToString(self._dateOrDateTime)
+
+    def asUTC(self):
+        if self._isDateTime:
+            d = self._dateOrDateTime
+            if d.tzinfo is None:
+                return self
+            else:
+                return self.__class__(d.astimezone(UTC))
+        else:
+            return self
+
+
+class TimeRange (object):
+    def __init__(self, start=None, end=None, tz=None):
+        self.start = start
+        self.end = end
+
+    def overlapsWith(self, other, tz=None):
+        if self.start is not None and other.start is not None:
+            if self.end is not None and other.end is not None:
+                return self.start < other.end and self.end > other.start
+            elif self.end is not None:
+                return other.start < self.end
+            elif other.end is not None:
+                self.start >= other.start and self.start < other.end
+            else:
+                return False
+        elif self.start is not None:
+            return self.start < other.end
+        elif other.start is not None:
+            return self.end < other.end and self.end > other.start
+        else:
+            return False
+
+
+def normalizeStartEndDuration(start, end=None, duration=None):
+    """
+    Given a start with a end or dureation (no neither), obtain a
+    normalized tuple of start and end.
+    """
+    assert end is None or duration is None, "Cannot specify both dtend and duration"
+
+    # FIXME: Ask Cyrus: Why UTC?
+    if start is not None:
+        start = start.asUTC()
+    if end is not None:
+        end = end.asUTC()
+    elif duration:
+        end = start + duration
+    
+    return (start, end)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100222/5230f1f3/attachment.html>


More information about the calendarserver-changes mailing list