Commits

Matthew Schinckel committed a17b2a7

Added modulo function to helpers

Comments (0)

Files changed (4)

         the_timedelta = timedelta.TimedeltaField()
 
 There are also some useful methods in helpers.py to work with timedelta
-objects.
+objects. (eg, multiply, divide, modulo and percentages).
+
 
 Todo
 -------------
 
 Changelog
 ----------
+0.4.2: Added helpers.modulo, to allow remainder division of timedlelta objects.
+
 0.4.1: changed get_db_prep_value() code to be in get_prep_value(), since I
     was calling it in get_default(), without a connection value.
 
 
 setup(
     name = "django-timedeltafield",
-    version = "0.4.1",
+    version = "0.4.2",
     description = "TimedeltaField for django models",
     url = "http://hg.schinckel.net/django-timedelta-field/",
     author = "Matthew Schinckel",

timedelta/__init__.py

 from fields import TimedeltaField
 
-from helpers import divide, multiply, parse, nice_repr, percentage
+from helpers import divide, multiply, modulo, parse, nice_repr, percentage

timedelta/helpers.py

         if v is not None )))
 
 
-def divide(obj1, obj2, float=False):
+def divide(obj1, obj2, as_float=False):
     """
     Allows for the division of timedeltas by other timedeltas, or by
     floats/Decimals
     """
     assert isinstance(obj1, datetime.timedelta), "First argument must be a timedelta."
-    #assert isinstance(obj2, (datetime.timedelta, int, float, Decimal)), "Second argument must be a timedelta or number"
+    assert isinstance(obj2, (datetime.timedelta, int, float, Decimal)), "Second argument must be a timedelta or number"
     
     sec1 = obj1.days * 86400 + obj1.seconds
     if isinstance(obj2, datetime.timedelta):
         sec2 = obj2.days * 86400 + obj2.seconds
-        if float:
+        if as_float:
             sec1 *= 1.0
         return sec1 / sec2
     else:
-        if float:
+        if as_float:
             assert None, "float=True is inappropriate when dividing timedelta by a number."
         secs = sec1 / obj2
         if isinstance(secs, Decimal):
             secs = float(secs)
         return datetime.timedelta(seconds=secs)
 
+def modulo(obj1, obj2):
+    """
+    Allows for remainder division of timedelta by timedelta or integer.
+    """
+    assert isinstance(obj1, datetime.timedelta), "First argument must be a timedelta."
+    assert isinstance(obj2, (datetime.timedelta, int)), "Second argument must be a timedelta or int."
+    
+    sec1 = obj1.days * 86400 + obj1.seconds
+    if isinstance(obj2, datetime.timedelta):
+        sec2 = obj2.days * 86400 + obj2.seconds
+        return datetime.timedelta(seconds=sec1 % sec2)
+    else:
+        return datetime.timedelta(seconds=(sec1 % obj2))
+    
 def percentage(obj1, obj2):
     """
     What percentage of obj2 is obj1? We want the answer as a float.
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.