← Back to team overview

oerppy-hackers team mailing list archive

[Branch ~oerppy-hackers/oerppy/trunk] Rev 26: * Moved start_of_week into util and made it public (also improved the

 

------------------------------------------------------------
revno: 26
committer: duncan@xxxxxxxxxx
branch nick: trunk
timestamp: Wed 2011-06-01 16:52:15 -0600
message:
  * Moved start_of_week into util and made it public (also improved the
    robostness.
  * Added some unit tests for it.
modified:
  oerppy/addons/canonical.py
  oerppy/tests/test_util.py
  oerppy/util.py


--
lp:oerppy
https://code.launchpad.net/~oerppy-hackers/oerppy/trunk

Your team oerppy Hackers is subscribed to branch lp:oerppy.
To unsubscribe from this branch go to https://code.launchpad.net/~oerppy-hackers/oerppy/trunk/+edit-subscription
=== modified file 'oerppy/addons/canonical.py'
--- oerppy/addons/canonical.py	2011-06-01 22:33:25 +0000
+++ oerppy/addons/canonical.py	2011-06-01 22:52:15 +0000
@@ -153,12 +153,6 @@
                 records.append(line)
         return records
 
-    # XXX let's make this public
-    def _start_of_week(self, date):
-        d = datetime.date(int(date[:4]), int(date[5:7]), int(date[8:10]))
-        monday = d + datetime.timedelta(days=-d.weekday())
-        return monday.isoformat()
-
     # XXX fill this in
     def get_month_timesheets(self, month, year):
         pass

=== modified file 'oerppy/tests/test_util.py'
--- oerppy/tests/test_util.py	2011-06-01 07:46:26 +0000
+++ oerppy/tests/test_util.py	2011-06-01 22:52:15 +0000
@@ -1,4 +1,4 @@
-import os, tempfile, unittest
+import datetime, os, tempfile, unittest
 from urlparse import urlparse
 
 from oerppy import const, util
@@ -68,3 +68,26 @@
         curried = util.partial(myfunction, "this", "that")
         result = curried("dunno", "no clue")
         self.assertEqual(result, ('this', 'that', 'dunno', 'no clue'))
+
+
+class DateTestCase(unittest.TestCase):
+
+    def test_start_of_week_with_iso(self):
+       date = "2011-01-01"
+       result = util.start_of_week(date)
+       self.assertEqual(result, datetime.date(2010, 12, 27))
+
+    def test_start_of_week_with_iso_returning_iso(self):
+       date = "2011-01-01"
+       result = util.start_of_week(date, return_iso=True)
+       self.assertEqual(result, "2010-12-27")
+
+    def test_start_of_week_with_datetime(self):
+       date = datetime.date(2011, 1, 1)
+       result = util.start_of_week(date)
+       self.assertEqual(result, datetime.date(2010, 12, 27))
+
+    def test_start_of_week_with_datetime_returning_iso(self):
+       date = datetime.date(2011, 1, 1)
+       result = util.start_of_week(date, return_iso=True)
+       self.assertEqual(result, "2010-12-27")

=== modified file 'oerppy/util.py'
--- oerppy/util.py	2011-06-01 22:33:25 +0000
+++ oerppy/util.py	2011-06-01 22:52:15 +0000
@@ -1,4 +1,5 @@
 import os
+import datetime
 from urlparse import urlparse, urlunparse
 
 from oerppy import const, exceptions
@@ -106,4 +107,15 @@
     return newfunc
 
 
-
+def start_of_week(date, return_iso=False):
+    # check to see if the date is in ISO date format
+    if isinstance(date, basestring):
+        date_object = datetime.date(
+            int(date[:4]), int(date[5:7]), int(date[8:10]))
+    # or if it's a date object already
+    elif isinstance(date, datetime.date):
+        date_object = date
+    monday = date_object + datetime.timedelta(days=-date_object.weekday())
+    if return_iso:
+        monday = monday.isoformat()
+    return monday