From: W. eWatson on
Maybe there's a more elegant way to do this. I want to express the
result of datetime.datetime.now() in fractional hours.

Here's one way.

dt=datetime.datetime.now()
xtup = dt.timetuple()
h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
# now is in fractions of an hour
From: Austyn on
How about:

import time
arizona_utc_offset = -7.00
h = (time.time() / 3600 + arizona_utc_offset) % 24

dt.timetuple()[6] is the day of the week; struct tm_time doesn't
include a sub-second field.

On Jan 10, 10:28 am, "W. eWatson" <wolftra...(a)invalid.com> wrote:
> Maybe there's a more elegant way to do this. I want to express the
> result of datetime.datetime.now() in fractional hours.
>
> Here's one way.
>
> dt=datetime.datetime.now()
> xtup = dt.timetuple()
> h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> #  now is in fractions of an hour

From: Austyn on
Here's an improvement in case you want your code to work outside of
Arizona:

from time import time, timezone
h = ((time() - timezone) / 3600) % 24

On Jan 10, 9:04 pm, Austyn <aus...(a)gmail.com> wrote:
> How about:
>
> import time
> arizona_utc_offset = -7.00
> h = (time.time() / 3600 + arizona_utc_offset) % 24
>
> dt.timetuple()[6] is the day of the week; struct tm_time doesn't
> include a sub-second field.
>
> On Jan 10, 10:28 am, "W. eWatson" <wolftra...(a)invalid.com> wrote:
>
>
>
> > Maybe there's a more elegant way to do this. I want to express the
> > result of datetime.datetime.now() in fractional hours.
>
> > Here's one way.
>
> > dt=datetime.datetime.now()
> > xtup = dt.timetuple()
> > h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> > #  now is in fractions of an hour

From: M.-A. Lemburg on
W. eWatson wrote:
> Maybe there's a more elegant way to do this. I want to express the
> result of datetime.datetime.now() in fractional hours.
>
> Here's one way.
>
> dt=datetime.datetime.now()
> xtup = dt.timetuple()
> h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> # now is in fractions of an hour

Here's how you'd do that with mxDateTime:

>>> from mx.DateTime import now
>>> now().abstime / 3600.0
13.17341068830755

..abstime gives you the time in fractional seconds.

http://www.egenix.com/products/python/mxBase/mxDateTime/

--
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source (#1, Jan 11 2010)
>>> Python/Zope Consulting and Support ... http://www.egenix.com/
>>> mxODBC.Zope.Database.Adapter ... http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ... http://python.egenix.com/
________________________________________________________________________

::: Try our new mxODBC.Connect Python Database Interface for free ! ::::


eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
From: Martin P. Hellwig on
W. eWatson wrote:
> Maybe there's a more elegant way to do this. I want to express the
> result of datetime.datetime.now() in fractional hours.
>
> Here's one way.
>
> dt=datetime.datetime.now()
> xtup = dt.timetuple()
> h = xtup[3]+xtup[4]/60.0+xtup[5]/3600.00+xtup[6]/10**6
> # now is in fractions of an hour

Here is another (though personally I don't find this more elegant than
yours, perhaps a bit more readable):

>>> now = datetime.datetime.now()
>>> fractional_hour = int(now.strftime('%H')) + int(now.strftime('%M'))
/ 60.0

--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'