-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPKG-INFO
277 lines (188 loc) · 10.8 KB
/
PKG-INFO
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
Metadata-Version: 1.1
Name: django-timedeltafield
Version: 0.7.10
Summary: TimedeltaField for django models
Home-page: http://bitbucket.org/schinckel/django-timedelta-field/
Author: Matthew Schinckel
Author-email: [email protected]
License: UNKNOWN
Description: django-timedelta-field
==========================
.. image:: https://drone.io/bitbucket.org/schinckel/django-timedelta-field/status.png
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.fields.TimedeltaField()
There are also some useful methods in helpers.py to work with timedelta
objects. (eg, multiply, divide, modulo and percentages).
Additionally, there are two template filters, `timedelta` and `iso8601`, which
will convert a timedelta object into a valid string.
Examples
-------------
Event model::
from django.db import models
import timedelta
class Event(models.Model):
start = models.DateTimeField()
duration = timedelta.fields.TimedeltaField()
@property
def finish(self):
return self.start + self.duration
Storing data within the field::
evt = Event.objects.create(
start=datetime.datetime.now(),
duration=datetime.timedelta(hours=1)
)
print evt.finish
evt.duration = datetime.timedelta(minutes=3)
print evt.finish
# We can use valid strings of the format described above.
Event.objects.update(duration='2 hours, 45 minutes')
evt = Event.objects.get(pk=evt.pk)
print evt.finish
# We can also assign directly to this field a valid string.
evt.duration = '3 days, 2 hours'
evt.save()
# Note: we need to re-fetch to ensure conversion to timedelta
evt = Event.objects.get(pk=evt.pk)
print evt.finish
Using with a form.
~~~~~~~~~~~~~~~~~~~~
You can just use a ModelForm, and it will automatically select the
``TimedeltaFormField``, which will handle the conversion between
formatted strings and timedelta objects.
Or you may use the form field directly::
from django import forms
import timedelta
class EventForm(forms.Form):
start = forms.DateTimeField()
duration = timedelta.forms.TimedeltaFormField()
This form field will display a value in the format::
2 day, 3 hours, 1 minute
And will parse data from a similar format.
Have a look in tests.py for examples of the form field/widget output.
Helpers
-------
``nice_repr(timedelta, display='long', sep=', ')``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns a string of the format:
"2 weeks, 7 hours, 1 day"
``display`` may be one of the strings 'minimal', 'short' or 'long'.
``iso8601_repr(timedelta, format=None)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns a string of the format:
"P2WT7H1D"
As per ISO8601. For timedeltas less than a whole day, the 'alt' format is supported:
"PT01:02:03"
``parse(string)``
~~~~~~~~~~~~~~~~~
Parse a string from the ``nice_repr`` formats.
``divide(timedelta, other)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allow dividing one timedelta by another, or by an integer, float or decimal value.
``modulo(timedelta, other)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allows modulo division of one timedelta by another, or by an integer.
``percentage(timedelta, timedelta)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns what percentage of the first timedelta the second is, as a float.
``decimal_percentag(timedelta, timedelta)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Returns what percentage of the first timedelta the second is, as a decimal.
``multiply(timedelta, other)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Allows for the multiplication of timedeltas by numbers.
``round_to_nearest(obj, timedelta)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Round the first argument (which must be a datetime, time, or timedelta object), to the nearest interval of the second argument.
``decimal_hours(timedelta)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Return a decimal value of the number of hours that this timedelta object refers to.
``total_seconds(timedelta)``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A wrapper for python < 2.7's lack of ``timedelta.total_seconds()``
Todo
-------------
Parse ISO8601 strings. Thanks to Guillame Libersat, we can generate them.
Handle strings with times in other languages. I'm not really sure about how
to do this, but it may be useful.
Changelog
----------
0.7.10: Fix issue for Python3/dumpdata, and None values.
0.7.9: Update PyPI classifiers/supported python versions.
0.7.8: Fix for django migrations.
0.7.7: Minor bugfix for template tag.
0.7.4: Improve template tags so the content they render can be parsed.
0.7.3: Bugfixes/more testing.
Add alternative format for ISO8601 display: PT00:15:00, for instance.
Note that values > timedelta(1) may not be displayed in this manner.
0.7.2: Make nice_repr behave in a more meaningful way with timedelta(0).
Thanks to Andy Evan for the report, and Jake Teton-Landis for the fix.
0.7.1: Allow using arbitrary php-style strings in the timedelta template
tag argument. Like django's date and time filters.
0.7.0: Support for django 1.5
0.6.7: Added LICENSE file.
0.6.6: Add in a couple of new template filters: total_seconds, and total_seconds_sort.
The latter zero-pads the value to 10 places, ideal for lexical sorting.
This correctly sorts timedeltas of up to around 10 years, if you need more
you can pass an argument to the filter.
0.6.5: Empty string values in database now are returned as None for the field value.
Note that you must have field.null=True to store a NULL in the db.
I'm still not 100% happy with this: postgres may choke on empty string values when doing INTERVAL comparisons.
Thanks to Gustavo Dias jaimes and reidpr for the report/fix.
0.6.4: Correctly parse '1w' (previously required 1wk).
Don't parse things like '1 hs', require '1 hrs'.
Test a bit harder.
0.6.3: Correctly parse '1h' as one hour (previously required 1hr).
0.6.2: Remember to include VERSION number.
0.6.0: Added total_seconds helper (for python < 2.7)
0.5.3: Include long_description from this file.
0.5.2: Added ``decimal_percentage``, which gives us a ``decimal.Decimal`` object.
0.5.1: Bugfixes from Mike Fogel.
0.5: Feature from Guillaume Libersat: helper and template for ISO8601 representation.
Bugfix from croepha: allow for non-plural 'days' string.
Bugfix from Guillaume Libersat: don't explode if initial is None
0.4.7: Bugfix from savemu: use unicode() instead of str()
0.4.6: Add in support for PostGIS database.
Make it easier to add in other column types for other databases.
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.
Platform: UNKNOWN
Classifier: Framework :: Django :: 1.4
Classifier: Framework :: Django :: 1.5
Classifier: Framework :: Django :: 1.6
Classifier: Framework :: Django :: 1.7
Classifier: Framework :: Django :: 1.8
Classifier: Framework :: Django
Classifier: License :: OSI Approved :: BSD License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.3
Classifier: Programming Language :: Python :: 3.4
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Programming Language :: Python
Classifier: Topic :: Software Development :: Libraries :: Python Modules