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

Datetime and timedelta #510

Closed
jetheurer opened this issue May 17, 2017 · 11 comments
Closed

Datetime and timedelta #510

jetheurer opened this issue May 17, 2017 · 11 comments

Comments

@jetheurer
Copy link

Can pint support determining units from a timedelta? It would be useful if it could for dealing with raw data.

@hgrecco
Copy link
Owner

hgrecco commented May 23, 2017

There is not such thing right now but it would be interesting.

@aglassdarkly
Copy link
Contributor

Is this the use case?

ureg = UnitRegistry()
t = ureg.Quantity(datetime.timedelta(days=4))
str(t)
'4.0 day'

@hgrecco
Copy link
Owner

hgrecco commented May 25, 2017

I guess that is more in the direction of interoperating datetime objects with currently supported units such as:

>>> # Currently possible
>>> 4 * ureg.hour
<Quantity(4, 'hour')>
>>> 4 * ureg.hour + 30 * ureg.min
<Quantity(4.5, 'hour')>
>>> # Not possible now
>>> datetime.datetime.now() + 4 * ureg.hour

@jetheurer
Copy link
Author

Yes exactly. I don't think its an easy problem to solve as there are a lot of nuances with dates and times, but as long it is was properly defined/documented it would be interesting. python-dateutil has some interesting solutions.

@hgrecco
Copy link
Owner

hgrecco commented May 25, 2017

I think we can start by providing a conversion utility between quantity and timedelta

@dopplershift
Copy link
Contributor

Not sure if it's a good idea, but you could also put isinstance checks for date time in __add__, __radd__, etc.

@aglassdarkly
Copy link
Contributor

I was exploring a bit and wrote a conversion between timedelta and quantity. I'll try hooking it up in the math ops and see what happens. This is an interesting issue!

@hgrecco
Copy link
Owner

hgrecco commented May 25, 2017

I think that date rules are very similar to our offset units:

datetime - datetime = timedelta
datetime (+  or -) timedelta = datetime
timedelta (+  or -) timedelta = timedelta

and that Pint Quantities are conceptualy timedeltas. What do you think?

@aglassdarkly
Copy link
Contributor

I agree that Pint quantities are conceptually timedeltas. Would it be as simple as adding something like this method to _Quantity and calling it appropriately from __add__, __sub__, etc. when the other side is a datetime?

    def to_timedelta(self):
        return datetime.timedelta(microseconds=self.to('microseconds').magnitude)

I can make a PR with these changes if this seems right.

@hgrecco
Copy link
Owner

hgrecco commented May 25, 2017

Sounds good to me.

bors bot added a commit that referenced this issue May 30, 2017
519: Add support for datetime math r=hgrecco
See #510 

- add _Quantity.to_timedelta()
- modify addition and subtraction with datetimes to convert _Quantity instances to timedeltas before performing the operation
- add tests
bors bot added a commit that referenced this issue Jun 5, 2017
519: Add support for datetime math r=hgrecco
See #510 

- add _Quantity.to_timedelta()
- modify addition and subtraction with datetimes to convert _Quantity instances to timedeltas before performing the operation
- add tests
@hgrecco
Copy link
Owner

hgrecco commented Jun 6, 2017

This is implemented in 0.8.1. Let's see how it plays out.

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

No branches or pull requests

4 participants