-
Notifications
You must be signed in to change notification settings - Fork 479
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
Add ability to convert datetime to Unix timestamp #23
Comments
+1 |
One problem here is that the semantics might be slightly unclear when you pass a naive datetime. I suppose we can just interpret naive times as local time. |
Very much this. Especially in light of your next sentence, since my default assumption would be to go the other way and subtract naive |
@jbrockmendel Yes, that would have been my preferred solution, but this would effectively be a backport of >>> from datetime import datetime
>>> from dateutil import tz
>>> dt = datetime(2018, 1, 3, 12, 20, tzinfo=tz.tzlocal())
>>> (dt.astimezone(tz.tzutc()) - datetime(1970, 1, 1, tzinfo=tz.tzutc())).total_seconds()
1515000000.0
>>> datetime.fromtimestamp(1515000000.0)
datetime.datetime(2018, 1, 3, 12, 20)
>>> datetime.utcfromtimestamp(1515000000.0)
datetime.datetime(2018, 1, 3, 17, 20) I think I'm going to put this decision off for a release after 2.7.0 because I'm not quite sure what we should do about "compatibility" methods like this. It might be that we add a new |
By the way, if we do decide to do a separate version of this (rather than a backport of I would probably just vendor Still, in this case, I think it's probably best to do compat-only. |
I have a utility function to map a datetime to a Unix timestamp but this use case is common enough to have it in a library (preferably
dateutil
orpytz
as these are very common Python datetime dependencies).Searching online I can see that this comes up often enough that multiple approaches were coined. The first doesn't require additional dependencies, the second approach does depend on
pytz
:Is this something that would be useful in this library? The key thing is that neither of the above approaches expresses the idea in an "obvious" enough way to duplicate it across your codebase and a utility function would be desirable anyhow.
The text was updated successfully, but these errors were encountered: