time as float since Jan 1, 0001?

Discussion in 'Python' started by Roy Smith, Feb 23, 2013.

  1. Roy Smith

    Roy Smith Guest

    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?
    Roy Smith, Feb 23, 2013
    #1
    1. Advertising

  2. 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

    --
    Cheers.

    Mark Lawrence
    Mark Lawrence, Feb 23, 2013
    #2
    1. Advertising

  3. On Sun, Feb 24, 2013 at 12:29 AM, Roy Smith <> wrote:
    > 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
    Chris Angelico, Feb 23, 2013
    #3
  4. Roy Smith

    Roy Smith Guest

    In article <>,
    Mark Lawrence <> 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 :)
    Roy Smith, Feb 23, 2013
    #4
  5. On Sat, 23 Feb 2013 08:49:08 -0500, Roy Smith <> declaimed
    the following in gmane.comp.python.general:

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


    But "ordinals" aren't floats... <G>

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, Feb 23, 2013
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vertigo

    days since 1 Jan 1970

    vertigo, Jul 7, 2004, in forum: Perl
    Replies:
    6
    Views:
    7,276
    Joe Smith
    Jul 8, 2004
  2. bd
    Replies:
    0
    Views:
    597
  3. sathyashrayan

    clc selected threads (30-jan-2005 to 31-jan-2005) #1

    sathyashrayan, Feb 6, 2005, in forum: C Programming
    Replies:
    3
    Views:
    352
    Mark McIntyre
    Feb 7, 2005
  4. Summu82
    Replies:
    5
    Views:
    841
    Richard Heathfield
    Jun 7, 2006
  5. Carsten Fuchs
    Replies:
    45
    Views:
    1,486
    James Kanze
    Oct 8, 2009
Loading...

Share This Page