-
Notifications
You must be signed in to change notification settings - Fork 14
Allow store_data to work with datetime objects #83
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
Conversation
…cts converted to strings to seconds since the epoch, otherwise, things break
Pull Request Test Coverage Report for Build 110
💛 - Coveralls |
pytplot/store_data.py
Outdated
# (i.e., times should be in UTC) - convert times to seconds since epoch | ||
if any(isinstance(t, datetime.datetime) for t in times): | ||
for tt, time in enumerate(times): | ||
times[tt] = (time-datetime.datetime(1970, 1, 1)).total_seconds() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While testing this I came across an error that said you can't subtract a datetime object that has a timezone and a datetime object without a timezone. If you give the line "datetime.datetime(1970, 1, 1)" a timezone, then it works.
That said, it looks like there is already a function that converts python datetime objects into seconds since 1970, the "timestamp()" function:
https://docs.python.org/3/library/datetime.html
Though it says it pretty much does exactly the same thing your code has here, but it includes the timezone
(dt - datetime(1970, 1, 1, tzinfo=timezone.utc)).total_seconds()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Gotta love timezone issues. Fixing this now.
pytplot/store_data.py
Outdated
# If given a list of datetime string, convert times to seconds since epoch | ||
elif any(isinstance(t, str) for t in times): | ||
for tt, time in enumerate(times): | ||
times[tt] = [pytplot.tplot_utilities.str_to_int(time)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And here it looks like you are making a list of single element lists, so times ends up looking like:
[[1], [2], [3], [4]]
instead of
[1, 2, 3, 4]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hah, don't ask me why I put times[tt] = a list of the value... I couldn't tell you why. I'll remove the brackets.
The easiest way to handle this (w/ the least amount of code changes), was to have store_data convert datetime obs/datetime obs that were converted to strings into seconds since the epoch (before doing anything with the variables "times").
Linked to ticket #77