Commits

Erik Grinaker committed 45d912f

added Date.delta()

Comments (0)

Files changed (3)

+xxxx-xx-xx: 0.3.0
+=================
+
+New features:
+
+* Added Date.delta() and DateTime.delta() for difference calculations
+
 2010-02-06: 0.2.0
 =================
 
         self.month = None
         self.day = None
 
+    def delta(self, date):
+        """
+        Returns the difference between the current object and another date or
+        datetime as seconds.
+
+        Raises :exc:`chrono.error.NoDateTimeError` on missing attributes.
+        """
+
+        self.assert_set()
+        date.assert_set()
+
+        return date.get_unix() - self.get_unix()
+
     def format(self, template):
         """
         Formats the date using *template*, replacing variables as
         self.assertEquals(d.day, None)
 
 
+class Date_deltaTest(unittest.TestCase):
+
+    def test_datetime(self):
+        "Date.delta() handles DateTime objects"
+
+        self.assertEquals(
+            chrono.Date("2010-07-23").delta(
+                chrono.DateTime("2010-09-16 16:27:43")
+            ),
+            4811263
+        )
+
+
+    def test_delta(self):
+        "Date.delta() returns difference"
+
+        self.assertEquals(
+            chrono.Date("2010-07-23").delta(chrono.Date("2010-09-16")),
+            4752000
+        )
+
+    def test_empty(self):
+        "Date.delta() raises NoDateTimeError on missing date"
+
+        self.assertRaises(
+            chrono.NoDateTimeError,
+            chrono.Date().delta, chrono.Date("2010-07-23")
+        )
+
+    def test_empty_other(self):
+        "Date.delta() raises NoDateTimeError on missing date in other object"
+
+        self.assertRaises(
+            chrono.NoDateTimeError,
+            chrono.Date("2010-07-23").delta, chrono.Date()
+        )
+
+    def test_negative(self):
+        "Date.delta() handles negative differences"
+
+        self.assertEquals(
+            chrono.Date("2010-07-23").delta(chrono.Date("2010-05-07")),
+            -6652800
+        )
+
+
 class Date_formatTest(unittest.TestCase):
 
     def test_empty(self):