[CalendarServer-changes] [5200] CalendarServer/trunk/twistedcaldav/dateops.py

source_changes at macosforge.org source_changes at macosforge.org
Wed Feb 24 17:09:57 PST 2010


Revision: 5200
          http://trac.macosforge.org/projects/calendarserver/changeset/5200
Author:   wsanchez at apple.com
Date:     2010-02-24 17:09:56 -0800 (Wed, 24 Feb 2010)
Log Message:
-----------
Start using twext.python.datetime

Modified Paths:
--------------
    CalendarServer/trunk/twistedcaldav/dateops.py

Modified: CalendarServer/trunk/twistedcaldav/dateops.py
===================================================================
--- CalendarServer/trunk/twistedcaldav/dateops.py	2010-02-25 01:04:40 UTC (rev 5199)
+++ CalendarServer/trunk/twistedcaldav/dateops.py	2010-02-25 01:09:56 UTC (rev 5200)
@@ -25,7 +25,6 @@
     "floatoffset",
     "compareDateTime",
     "differenceDateTime",
-    "makeComparableDateTime",
     "timeRangesOverlap",
     "periodEnd",
     "normalizePeriodList",
@@ -33,52 +32,24 @@
 ]
 
 import datetime
-from vobject.icalendar import utc, dateTimeToString, dateToString
+from vobject.icalendar import utc
 
+from twext.python.datetime import dateordatetime, timerange
+
 def toString(dt):
-    """
-    Convert a L{datetime.date} or L{datetime.datetime} object to a string.
-    @param dt: a L{datetime.date} or L{datetime.datetime} object to normalize
-    @return: the converted date or datetime
-    """
-    if not isinstance(dt, datetime.date):
-        raise TypeError("%r is not a datetime.date instance" % (dt,))
-    
-    return dateTimeToString(dt) if isinstance(dt, datetime.datetime) else dateToString(dt)
+    return dateordatetime(dt).iCalendarString()
 
 def normalizeStartEndDuration(dtstart, dtend=None, duration=None):
-    """
-    Given a DTSTART and DTEND or DURATION (or neither), return a normalized tuple of
-    DTSTART and DTEND.
-    """
-    
-    assert dtend is None or duration is None, "Cannot specify both dtend and duration"
-    if dtstart is not None:
-        dtstart = normalizeToUTC(dtstart)
-    if dtend is not None:
-        dtend = normalizeToUTC(dtend)
-    elif duration:
-        dtend = dtstart + duration
-    
-    return (dtstart, dtend)
+    def asUTC(dodt):
+        if dodt is None:
+            return None
+        return dodt.asUTC().dateOrDatetime()
 
+    tr = timerange(dtstart, dtend, duration)
+    return asUTC(tr.start()), asUTC(tr.end())
+
 def normalizeToUTC(dt):
-    """
-    Normalize a L{datetime.date} or L{datetime.datetime} object to UTC.
-    If its a L{datetime.date}, just return it as-is.
-    @param dt: a L{datetime.date} or L{datetime.datetime} object to normalize
-    @return: the normalized date or datetime
-    """
-    if not isinstance(dt, datetime.date):
-        raise TypeError("%r is not a datetime.date instance" % (dt,))
-    
-    if isinstance(dt, datetime.datetime):
-        if dt.tzinfo is not None:
-            return dt.astimezone(utc)
-        else:
-            return dt
-    else:
-        return dt
+    return dateordatetime(dt).asUTC().dateOrDatetime()
 
 def normalizeForIndex(dt):
     """
@@ -113,22 +84,9 @@
         tzinfo = utc
     return dt.astimezone(tzinfo).replace(tzinfo=utc)
 
-def compareDateTime(dt1, dt2, defaulttz = None):
-    """
-    Compare two L{datetime.date} or L{datetime.datetime} objects in
-    a transparent manner that does not depend on the nature of the objects
-    and whether timesones are set.
-    @param dt1: a L{datetime.datetime} or L{datetime.date} specifying a date to test.
-    @param dt2: a L{datetime.datetime} or L{datetime.date} specifying a date to test.
-    @param defaulttz: a L{datetime.tzinfo} for the VTIMEZONE object to use if one of the
-        datetime's is a date or floating.
-    @return:  0 if dt1 == dt2,
-        -1 if dt1 < dt2
-        1 if dt1 > dt2
-    """
-
-    dt1, dt2 = makeComparableDateTime(dt1, dt2, defaulttz)
-
+def compareDateTime(dt1, dt2, defaulttz=None):
+    dt1 = dateordatetime(dt1, defaultTZ=defaulttz)
+    dt2 = dateordatetime(dt2, defaultTZ=defaulttz)
     if dt1 == dt2:
         return 0
     elif dt1 < dt2:
@@ -137,78 +95,21 @@
         return 1
 
 def differenceDateTime(start, end, defaulttz = None):
-    """
-    Determines the difference between start and end datetime's returning the duration.
-    NB This handles the case where start and end are not of the same datetime type.
-    
-    @param start: a L{datetime.datetime} or L{datetime.date} specify the start time.
-    @param end: a L{datetime.datetime} or L{datetime.date} specify the end time.
-    @param defaulttz: a L{datetime.tzinfo} for the VTIMEZONE object to use if one of the
-        datetime's is a date or floating.
-    @return: the L{datetime.timedelta} for the difference between the two.
-    """
+    return dateordatetime(end, defaultTZ=defaulttz) - dateordatetime(start)
 
-    start, end = makeComparableDateTime(start, end, defaulttz)
-    return end - start
+#def timeRangesOverlap(start1, end1, start2, end2, defaulttz = None):
+#    def dodt(d):
+#        if d is None:
+#            return None
+#        else:
+#            return dateordatetime(d, defaulttz)
+#
+#    dodt1 = timerange(dodt(start1), dodt(end1))
+#    dodt2 = timerange(dodt(start2), dodt(end2))
+#
+#    return dodt1.overlapsWith(dodt2)
 
-def makeComparableDateTime(dt1, dt2, defaulttz = None):  
-    """
-    Ensure that the two datetime objects passed in are of a comparable type for arithmetic
-    and comparison operations..
-    
-    @param start: a L{datetime.datetime} or L{datetime.date} specifying one time.
-    @param end: a L{datetime.datetime} or L{datetime.date} specifying another time.
-    @param defaulttz: a L{datetime.tzinfo} for the VTIMEZONE object to use if one of the
-        datetime's is a date or floating.
-    @return: a C{tuple} of two L{datetime.xx}'s for the comparable items.
-    """
-
-    for dt in (dt1, dt2):
-        if not isinstance(dt, datetime.date):
-            raise TypeError("%r is not a datetime.date instance" % (dt,))
-
-    # Pick appropriate tzinfo
-    tzi = [None]
-    def getTzinfo(dtzi):
-        if tzi[0] is None:
-            if defaulttz is not None:
-                tzi[0] = defaulttz
-            else:
-                return dtzi
-        return tzi[0]
-
-    # If any one argument is a datetime.date, convert that into a datetime.datetime
-    # with the time set to midnight and the same timezone as the other argument
-    if isinstance(dt1, datetime.datetime) and not isinstance(dt2, datetime.datetime):
-        dt2 = datetime.datetime(dt2.year, dt2.month, dt2.day, 0, 0, 0, 0, getTzinfo(dt1.tzinfo))
-    elif not isinstance(dt1, datetime.datetime) and isinstance(dt2, datetime.datetime):
-        dt1 = datetime.datetime(dt1.year, dt1.month, dt1.day, 0, 0, 0, 0, getTzinfo(dt2.tzinfo))
-    elif isinstance(dt1, datetime.datetime) and isinstance(dt2, datetime.datetime):
-        # Ensure that they both have or have not a tzinfo
-        if (dt1.tzinfo is not None and dt2.tzinfo is None):
-            dt2 = dt2.replace(tzinfo=getTzinfo(dt1.tzinfo))
-        elif (dt1.tzinfo is None and dt2.tzinfo is not None):
-            dt1 = dt1.replace(tzinfo=getTzinfo(dt2.tzinfo))
-    
-    return (dt1, dt2)
-
 def timeRangesOverlap(start1, end1, start2, end2, defaulttz = None):
-    """
-    Determines whether two time ranges overlap.
-    @param start1: a L{datetime.datetime} or L{datetime.date} specifying the
-        beginning of the first time span.
-    @param end1: a L{datetime.datetime} or L{datetime.date} specifying the
-        end of the first time span.  C{end} may be None, indicating that
-        there is no end date.
-    @param start2: a L{datetime.datetime} or L{datetime.date} specifying the
-        beginning of the second time span.
-    @param end2: a L{datetime.datetime} or L{datetime.date} specifying the
-        end of the second time span.  C{end} may be None, indicating that
-        there is no end date.
-    @param defaulttz: a L{datetime.tzinfo} for the VTIMEZONE object to use if one of the
-        datetime's is a date or floating.
-    @return: True if the two given time spans overlap, False otherwise.
-    """
     # Can't compare datetime.date and datetime.datetime objects, so normalize
     # to date if they are mixed.
     if isinstance(start1, datetime.datetime) and (start2 is not None) and not isinstance(start2, datetime.datetime): start1 = start1.date()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100224/37ab7dd1/attachment-0001.html>


More information about the calendarserver-changes mailing list