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

Maybe use ISO8601 as input format? #8

Open jgsogo opened 11 years ago

jgsogo commented 11 years ago

(Thinking about machines and importing data)

There is an standard about how to express durations, ISO8601 [1], it would be interesting to allow this format as input, although maintain as output both, ISO8601 and the more readable one (current).

[1] http://en.wikipedia.org/wiki/ISO_8601#Durations

johnpaulett commented 11 years ago

I was unaware of the ISO 8601 Duration specification. A patch that added support for IS0 8601 and related tests would be very welcomed. Probably involves altering str_to_timedelta.

For backwards compatibility, we would want to still support the existing duration syntax.

jgsogo commented 11 years ago

It seems that isodate [1] or dateutil [2] should do the trick, working with strptime would be so hazardous. I'll try to work on it this week

[1] https://pypi.python.org/pypi/isodate [2] http://labix.org/python-dateutil

codingjoe commented 10 years ago

I looked into it, as I desperately need an implementation of ISO 8601 durations. Sadly you decided to work with pythons datetime.timedelta which is a poor implementation of a duration.

The only way I see to make this happening is, changing your to_python to an object that inherits from timedelta but implements a isoformat() as datetime does it self. This would be downwards compatible, but has one big disadvantage.

timedelta is not a duration. A duration can't be negative by definition, a timedelta can. The cleaner approach would be implementing a real duration and using an UNSIGNED BIGINT your database.

I would be will to implement this if you'd be cool with that little change.

johnpaulett commented 10 years ago

@codingjoe -- I'm trying to understand your use case. Are you looking for:

  1. A means to pass in a IS0 8601 Duration into a DurationField (which the original issue proposes)?
  2. A way to get a timedelta formatted into an ISO 8601 Duration string?
  3. Altering the way this package stores the data in the database?
  4. Something else?

I would completely welcome a patch for the first item. I think the leading "P" of an ISO8601 duration should make detecting it straightforward in str_to_timedelta.

The second item may be currently accomplishable with the isodate library (duration_isoformat can take a timedelta).

I won't argue that perhaps a better name for this library currently is IntervalField (essentially it provides PostgreSQL's INTERVAL across various database backends) or TimedeltaField. However, I believe using form & model validators you could enforce only positive durations using this library currently.

Definitely some opportunity for improvement, but I'm just trying to grasp exactly what you are looking for.

codingjoe commented 10 years ago

@johnpaulett sorry for my confusing post.

Here's what I'm up to: I currently writing a django implementation of mircodata objects as defined on http://schema.org/

That requires a ISO duration. I know that integers are more practical for machine use, but they suck as a service to service interface. I find it a little sad, that durations aren't implemented in python itself.

I didn't know that this library intends to implement a PostgreSQL Interval. PostgreSQL has a confusing naming there as well. In ISO an interval is DateTime+Duration.

I think I'm going to write a patch for 2. if that's cool with you. It looks like 1. is already implemented in str_to_timedelta

johnpaulett commented 10 years ago

To be clear, 1 is not available in str_to_timedelta currently, but I would welcome a patch for it.

Regarding 2, would have to see code a bit more, not 100% sure if this is something this library should provide or if a more widely applicable item for all timedeltas in Python.

codingjoe commented 10 years ago

It's not my number one priority. It's more a long shot, of a library want to develop to maybe use it later commercially. In other words it's gonna take me a while. But I'll keep you posted on developments and i'll supply patches if need be.

jnelson commented 9 years ago

@johnpaulett Do you have a preference between aniso8601 and isodate as supporting lib for a PR on this issue?

johnpaulett commented 9 years ago

@jnelson I would steer away from including a GPL-licensed dependency (aniso8601) for this BSD-licensed library.

jnelson commented 9 years ago

Sorry I didn't catch that. Looks like isodate is also BSD, so that's easy. Thanks!