|
| 1 | +from datetime import timedelta |
| 2 | +from mongoengine.base import BaseField |
| 3 | + |
| 4 | +class TimedeltaField(BaseField): |
| 5 | + """A timedelta field. |
| 6 | +
|
| 7 | + Looks to the outside world like a datatime.timedelta, but stores |
| 8 | + in the database as an integer (or float) number of seconds. |
| 9 | + |
| 10 | + """ |
| 11 | + def validate(self, value): |
| 12 | + if not isinstance(value, (timedelta, int, float)): |
| 13 | + self.error(u'cannot parse timedelta "%r"' % value) |
| 14 | + |
| 15 | + def to_mongo(self, value): |
| 16 | + return self.prepare_query_value(None, value) |
| 17 | + |
| 18 | + def to_python(self, value): |
| 19 | + return timedelta(seconds=value) |
| 20 | + |
| 21 | + def prepare_query_value(self, op, value): |
| 22 | + if value is None: |
| 23 | + return value |
| 24 | + if isinstance(value, timedelta): |
| 25 | + return self.total_seconds(value) |
| 26 | + if isinstance(value, (int, float)): |
| 27 | + return value |
| 28 | + |
| 29 | + @staticmethod |
| 30 | + def total_seconds(value): |
| 31 | + """Implements Python 2.7's datetime.timedelta.total_seconds() |
| 32 | + for backwards compatibility with Python 2.5 and 2.6. |
| 33 | +
|
| 34 | + """ |
| 35 | + try: |
| 36 | + return value.total_seconds() |
| 37 | + except AttributeError: |
| 38 | + return (value.days * 24 * 3600) + \ |
| 39 | + (value.seconds) + \ |
| 40 | + (value.microseconds / 1000000.0) |
0 commit comments