time as float since Jan 1, 0001?

Roy Smith roy at panix.com
Sat Feb 23 08:49:08 EST 2013


In article <mailman.2342.1361626870.2939.python-list at python.org>,
 Mark Lawrence <breamoreboy at yahoo.co.uk> wrote:

> On 23/02/2013 13:29, Roy Smith wrote:
> > 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

Duh!  I didn't get that far in the docs!  Thanks, that makes life a lot 
easier.

Still, it seems like allowing toordinal() and fromordinal() to handle 
floats would be a useful addition :-)



More information about the Python-list mailing list