Fractional Hours from datetime?

W. eWatson wolftracks at invalid.com
Mon Jan 11 11:44:26 EST 2010


Austyn wrote:
> 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... at 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... at 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
> 
There seems to be some controversy about this and other matters of 
datetime. 
<http://blog.twinapex.fi/2008/06/30/relativity-of-time-shortcomings-in-python-datetime-and-workaround/>



More information about the Python-list mailing list