Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #39665 > unrolled thread
| Started by | Roy Smith <roy@panix.com> |
|---|---|
| First post | 2013-02-23 08:29 -0500 |
| Last post | 2013-02-24 00:45 +1100 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.python
time as float since Jan 1, 0001? Roy Smith <roy@panix.com> - 2013-02-23 08:29 -0500
Re: time as float since Jan 1, 0001? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-02-23 13:41 +0000
Re: time as float since Jan 1, 0001? Roy Smith <roy@panix.com> - 2013-02-23 08:49 -0500
Re: time as float since Jan 1, 0001? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-02-23 14:08 -0500
Re: time as float since Jan 1, 0001? Chris Angelico <rosuav@gmail.com> - 2013-02-24 00:45 +1100
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-02-23 08:29 -0500 |
| Subject | time as float since Jan 1, 0001? |
| Message-ID | <roy-41E67E.08290123022013@news.panix.com> |
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?
[toc] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2013-02-23 13:41 +0000 |
| Message-ID | <mailman.2342.1361626870.2939.python-list@python.org> |
| In reply to | #39665 |
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
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2013-02-23 08:49 -0500 |
| Message-ID | <roy-338C11.08490823022013@news.panix.com> |
| In reply to | #39667 |
In article <mailman.2342.1361626870.2939.python-list@python.org>, Mark Lawrence <breamoreboy@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 :-)
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2013-02-23 14:08 -0500 |
| Message-ID | <mailman.2370.1361646532.2939.python-list@python.org> |
| In reply to | #39669 |
On Sat, 23 Feb 2013 08:49:08 -0500, Roy Smith <roy@panix.com> 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
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2013-02-24 00:45 +1100 |
| Message-ID | <mailman.2343.1361627108.2939.python-list@python.org> |
| In reply to | #39665 |
On Sun, Feb 24, 2013 at 12:29 AM, Roy Smith <roy@panix.com> 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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web