johnpaulett / django-durationfield

Temporary reusable Django application for adding a DurationField, until Django issue #2443 is resolved.
http://django-durationfield.readthedocs.org
67 stars 24 forks source link

Add Template Tags for time display #12

Closed troygrosfield closed 10 years ago

troygrosfield commented 10 years ago

Create a template tag that format the timedelta to a friendly display. Right now if you call the string representation for the duration field you get:

>>> my_object = MyObject.objects.create(duration='0:03:58.16')
>>> my_object.duration
0:03:58.000016

For template formatting, something like:

{{ my_object.duration|duration }}

That outputs:

0:03:58

or even:

3:58

Something similar to what's going on here:

johnpaulett commented 10 years ago

Patches welcome. However, even if using django-durationfield, since it returns a datetime.timedelta instance, you could use django-timedeltafield's tag, since it just consumes datetime.timedelta

troygrosfield commented 10 years ago

Fair enough. However, I didn't want to add another dependency if possible which is why I'd rather have it in this lib. I'll play around with it a bit.

troygrosfield commented 10 years ago

Also, this might be worth opening another ticket, but in the example above:

>>> my_object = MyObject.objects.create(duration='0:03:58.16')
>>> my_object.duration
0:03:58.000016

I guess I would see that as "3 minutes and 58.16 seconds" which isn't how it gets evaluated. I guess I would expect to see:

>>> my_object = MyObject.objects.create(duration='0:03:58.16')
>>> my_object.duration
0:03:58.16

.16 seconds == 160 milliseconds == 160000 microseconds

Timedelta uses microseconds, not milliseconds which is what I think the issue is? When I go to add a duration to a form field field and add the value:

0:03:58.16

Then, when I go to edit that same value, the default value is:

0:03:58.000016

Which isn't correct. Would you like me to submit the pull request for this?

johnpaulett commented 10 years ago

@troygrosfield Good catch. Looks like the microsecond parsing does int(16) instead of int(160000)

Please submit a pull request.

troygrosfield commented 10 years ago

Looks like it's the regular expression in:

time_format = r"(?:(?P<weeks>\d+)\W*(?:weeks?|w),?)?\W*(?:(?P<days>\d+)\W*(?:days?|d),?)?\W*(?:(?P<hours>\d+):(?P<minutes>\d+)(?::(?P<seconds>\d+)(?:\.(?P<microseconds>\d+))?)?)?"

https://github.com/johnpaulett/django-durationfield/blob/master/durationfield/utils/timestring.py#L32

The "microseconds" should be "milliseconds".

johnpaulett commented 10 years ago

Please add appropriate test cases too. We definitely still want to support microseconds (0:03:58.000016).