Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timedeltas Quantities not acting as expected #690

Closed
dalymople opened this issue Sep 5, 2018 · 2 comments
Closed

Timedeltas Quantities not acting as expected #690

dalymople opened this issue Sep 5, 2018 · 2 comments

Comments

@dalymople
Copy link

I have been playing around with this package (which is fantastic by the way!) and I have stumbled across some issues with the support for datetime.timedetas. Some of the issues appear to be due to the underlying implementation of the datetime.timedelta objects rather than Pint, but the way they are handled does seem a bit strange.

Expanding on the example in issue #608

>>> ureg = UnitRegistry()
>>> a = datetime.timedelta(seconds=4)
>>> t = ureg.Quantity(a, units='seconds')
>>> t
<Quantity(0:00:04, 'second')>

Looks good, lets try some unit conversions.

>>> t.to('minutes')
<Quantity(0:00:00.066667, 'minute')>
>>> t.to('hours')
<Quantity(0:00:00.001111, 'hour')>
>>> t.to('days')
<Quantity(0:00:00.000046, 'day')>

All good, try some more.

>>> t.to('milliseconds')
<Quantity(1:06:40, 'millisecond')>
>>> t.to('microseconds')
<Quantity(46 days, 7:06:40, 'microsecond')>

That looks strange, but seems to be simply due to how it is displayed.

The bigger issue to do with functionality is the following;

>>> d = ureg.Quantity(10, 'meters')
>>> t = ureg.Quantity(datetime.timedelta(seconds=4), units='seconds')
>>> d/t
TypeError: unsupported operand type(s) for /: 'int' and 'datetime.timedelta'

That is not completely unexpected, the exception is raised by the underlying datetime.timedelta implementation rather than Pint. However it doesn't feel right... the only way around this seems to be not to use a datetime.timedelta like so;

>>> t = ureg.Quantity(a.total_seconds(), 'seconds')
>>> d/t
<Quantity(2.5, 'meter / second')>

Would a pull request addressing these issues be accepted? I have never submitted a PR before but I would be happy to give it a go if there is support for these changes?

@jondoesntgit
Copy link
Contributor

What API would you suggest? Show some sample output perhaps?

@hgrecco
Copy link
Owner

hgrecco commented Dec 4, 2019

Closing. Feel free to reopen if an API is suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants