time as float since Jan 1, 0001?

R

Roy Smith

I'm working with matplotlib.plot_date(), which represents time as
"floats starting at January 1st, year 0001". Is there any
straight-forward way to get that out of a datetime?

datetime.toordinal() gives me the number of days since that epoch, but
as an integer. I figured it wouldn't be too hard to just do:

t.toordinal() + t.time().total_seconds()

except it turns out that only timedelta supports total_seconds(); time
doesn't!

I suppose I could do:

t.toordinal() + t.hour / 24.0 \
+ t.minute / 1440.0 \
+ t.second / 86400.0

but that's really ugly. Is there no cleaner way to do this conversion?
 
M

Mark Lawrence

I'm working with matplotlib.plot_date(), which represents time as
"floats starting at January 1st, year 0001". Is there any
straight-forward way to get that out of a datetime?

datetime.toordinal() gives me the number of days since that epoch, but
as an integer. I figured it wouldn't be too hard to just do:

t.toordinal() + t.time().total_seconds()

except it turns out that only timedelta supports total_seconds(); time
doesn't!

I suppose I could do:

t.toordinal() + t.hour / 24.0 \
+ t.minute / 1440.0 \
+ t.second / 86400.0

but that's really ugly. Is there no cleaner way to do this conversion?

IIRC you needn't bother, matplotlib will do all the conversions for you.
In the highly likely case that I'm wrong this should help
http://matplotlib.org/api/dates_api.html#module-matplotlib.dates
 
C

Chris Angelico

datetime.toordinal() gives me the number of days since that epoch, but
as an integer. I figured it wouldn't be too hard to just do:

t.toordinal() + t.time().total_seconds()

What about t.timestamp()? That's since 1970, but you could add (if my
calculations are correct) 62135683200.0 to it.

ChrisA
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top