# django-timedelta-field /

Filename Size Date modified Message
timedelta
0 B
15 B
233 B
30 B
1.9 KB
533 B

# django-timedelta-field

PostgreSQL can store data as INTERVAL type, which is close to meaning the same as python's timedelta object (although better in a couple of ways).

I have lots of use for timedelta objects, and having code that basically wrapped integer objects as a number of seconds was common. This module combines the two:

• a timedelta.TimedeltaField() object that transparently converts to and from datetime.timedelta
• storage of the data as an INTERVAL in PostgreSQL, or a string in other databases. (Other databases will be considered if I ever use them, or receive patches).

The coolest part of this package is the way it manipulates strings entered by users, and presents them. Any string of the format:

[X weeks,] [Y days,] [Z hours,] [A minutes,] [B seconds]

will be converted to a timedelta object. Even shortened versions can be used: hrs, hr or h will also suffice. The parsing ignores trailing 's', but is smart about adding them in when presenting the data to the user.

To use, install the package, and use the field:

from django.db import models
import timedelta

class MyModel(models.Model):
the_timedelta = timedelta.TimedeltaField()


There are also some useful methods in helpers.py to work with timedelta objects. (eg, multiply, divide, modulo and percentages).

## Todo

Handle strings with times in other languages. I'm not really sure about how to do this, but it may be useful.

## Changelog

0.4.5: Restore functionality for django <1.2 (thanks Yoav Aner).

0.4.3: 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.
0.4: added the connection and prepared arguments to get_db_prep_value(),
so that django 1.3+ will not complain of DeprecationWarnings.
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.