Discussion:
Fractional Hours from datetime?
W. eWatson
2010-01-10 17:28:15 UTC
Permalink
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
Austyn
2010-01-11 04:04:18 UTC
Permalink
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.
Post by W. eWatson
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
Austyn
2010-01-11 04:23:19 UTC
Permalink
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
Post by Austyn
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.
Post by W. eWatson
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
W. eWatson
2010-01-11 16:44:26 UTC
Permalink
Post by Austyn
Here's an improvement in case you want your code to work outside of
from time import time, timezone
h = ((time() - timezone) / 3600) % 24
Post by Austyn
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.
Post by W. eWatson
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/>
M.-A. Lemburg
2010-01-11 12:12:19 UTC
Permalink
Post by W. eWatson
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 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)
Post by W. eWatson
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/
Martin P. Hellwig
2010-01-11 13:18:17 UTC
Permalink
Post by W. eWatson
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
Post by W. eWatson
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.'
Martin P. Hellwig
2010-01-11 13:25:22 UTC
Permalink
Post by Martin P. Hellwig
Post by W. eWatson
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
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = int(now.strftime('%H')) + int(now.strftime('%M'))
/ 60.0
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = now.hour + now.minute / 60.0
--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
W. eWatson
2010-01-12 00:53:21 UTC
Permalink
Post by Martin P. Hellwig
Post by W. eWatson
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
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = int(now.strftime('%H')) +
int(now.strftime('%M')) / 60.0
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = now.hour + now.minute / 60.0
See my post about the datetime controversy about 3-4 posts up from yours.
Ben Finney
2010-01-12 01:27:23 UTC
Permalink
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.

Could you give the Message-ID for that message?
--
\ ?As we enjoy great advantages from the inventions of others, we |
`\ should be glad to serve others by any invention of ours; and |
_o__) this we should do freely and generously.? ?Benjamin Franklin |
Ben Finney
W. eWatson
2010-01-12 02:27:02 UTC
Permalink
Post by Ben Finney
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?

It's easier to place the comment here:

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/>
Ben Finney
2010-01-12 02:32:08 UTC
Permalink
Post by W. eWatson
Post by Ben Finney
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?
It is a field in the header of every message. Show the full header, and
look for the field named ?Message-ID?.

Ideally the value of that field is unique for all messages ever, but
there's no technical enforcement of that so it wouldn't serve to
*guarantee* unique identification. It's good enough to use as an
identifier in referring people to read messages, though.
--
\ ?Oh, I realize it's a penny here and a penny there, but look at |
`\ me: I've worked myself up from nothing to a state of extreme |
_o__) poverty.? ?Groucho Marx |
Ben Finney
Alf P. Steinbach
2010-01-12 02:47:59 UTC
Permalink
Post by W. eWatson
Post by Ben Finney
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?
In Thunderbird (the newsreader that you're using) there's a little '+' to the
left of the message subject line.

That shows the headers.

You can alternatively use [View -> Message Source], or keyboard [Ctrl U].

From that you find that the message id is

hifkh3$bru$1 at news.eternal-september.org

Then, kicking and cajoling your web browser to turn in the direction of Google
Groups' Usenet archive,

groups.google.com

you click the "Advanced search" button, paste the message id, and find that
Google is unable to find your article, he he.

It's common, it's a very very unreliable archive.

However, as with most things, the "Great Wall of Google" prevents you from
reporting this. There's no known way to report any bug to Google. As with
Microsoft in the old days (reportedly Microsoft employees weren't even allowed
to use the words "bug" or "error" with customers, only, at worst, "problems"),
there are Google web forms and whatnot, but they all end up in cul-de-sacs, so
that, together with the total impossibility of reaching any human at Google, one
very very strongly suspects that it's Google *policy* to never admit to bugs, or
waste time on fixing them. And so, I suspect, Google Earth still places Norway
in the middle of Sweden, and I know for a fact that Google Groups still actively
removes the space at the end of a valid signature delimiter, and Google Talk
acts up in various ways, and so on: quite serious bugs, but no way to report
them (thousands upon thousands have tried, at one time a movement was founded
with its own web site, but the "Great Wall of Google" lets no-one through).

And considering this, and the fact that Google's archive is now the main Usenet
archive, message id's are not that useful, really.

So asking for a Usenet article's message id is just showing off -- that one is
not up-to-date on current technology (it gets more unreliable year by year).
Post by W. eWatson
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/>
No, not at all. :-)

Instead, just ignore silly requests for message id's.


Cheers & hth.,

- Alf
Steve Holden
2010-01-12 03:03:11 UTC
Permalink
Post by Alf P. Steinbach
Post by W. eWatson
Post by Ben Finney
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?
In Thunderbird (the newsreader that you're using) there's a little '+'
to the left of the message subject line.
That shows the headers.
It shows a very limited subset of the headers ...

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
Ben Finney
2010-01-12 03:26:53 UTC
Permalink
Post by Alf P. Steinbach
And considering this, and the fact that Google's archive is now the
main Usenet archive, message id's are not that useful, really.
You've demonstrated only that Google is an unreliable Usenet archive.

One doesn't even need to use Usenet, in this case, since
comp.lang.python is a forum distributed both as a Usenet forum and a
mailing-list forum.
--
\ ?Two possibilities exist: Either we are alone in the Universe |
`\ or we are not. Both are equally terrifying.? ?Arthur C. Clarke, |
_o__) 1999 |
Ben Finney
David Robinow
2010-01-12 03:59:11 UTC
Permalink
Post by Ben Finney
Post by Alf P. Steinbach
And considering this, and the fact that Google's archive is now the
main Usenet archive, message id's are not that useful, really.
You've demonstrated only that Google is an unreliable Usenet archive.
One doesn't even need to use Usenet, in this case, since
comp.lang.python is a forum distributed both as a Usenet forum and a
mailing-list forum.
--
?\ ? ? ? ?Two possibilities exist: Either we are alone in the Universe |
?`\ ? or we are not. Both are equally terrifying.? ?Arthur C. Clarke, |
_o__) ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 1999 |
Ben Finney
--
http://mail.python.org/mailman/listinfo/python-list
The archive at http://mail.python.org/pipermail/python-list/ doesn't
show the message id either.
However, the monthly gzip'ed text file does have them.

FWIW, for you google-haters, I read python-list using gmail and I
don't see any message id's.
Great spam detection though.
W. eWatson
2010-01-12 19:51:11 UTC
Permalink
Post by Ben Finney
Post by Alf P. Steinbach
And considering this, and the fact that Google's archive is now the
main Usenet archive, message id's are not that useful, really.
You've demonstrated only that Google is an unreliable Usenet archive.
One doesn't even need to use Usenet, in this case, since
comp.lang.python is a forum distributed both as a Usenet forum and a
mailing-list forum.
Good Clarke quote. (Not present here.)

Alf P. Steinbach
2010-01-12 04:23:16 UTC
Permalink
Post by Steve Holden
Post by Alf P. Steinbach
Post by W. eWatson
Post by Ben Finney
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?
In Thunderbird (the newsreader that you're using) there's a little '+'
to the left of the message subject line.
That shows the headers.
It shows a very limited subset of the headers ...
Really? My Thunderbird shows all headers. Perhaps you need to configure something.

Or do as I wrote next and you snipped, use [View -> Message Source].


Cheers & hth. (even if rather off-topic by now, not even direct response!),

- Alf
Steve Holden
2010-01-12 10:24:30 UTC
Permalink
Post by Alf P. Steinbach
Post by Steve Holden
Post by Alf P. Steinbach
Post by W. eWatson
Post by Ben Finney
Post by W. eWatson
See my post about the datetime controversy about 3-4 posts up from yours.
This forum is distributed, and there's no ?up? or ?3-4 messages? that is
common for all readers.
Could you give the Message-ID for that message?
Sort of like outer space I guess. No real direction. How would I find
the message ID?
In Thunderbird (the newsreader that you're using) there's a little '+'
to the left of the message subject line.
That shows the headers.
It shows a very limited subset of the headers ...
Really? My Thunderbird shows all headers. Perhaps you need to configure something.
I don't need to configure anything, thank you very much, it's already
configured perfectly nicely as it is, thank you, toView | Headers | Normal.

I agree I can switch to View | Headers | All, but for the typical modern
newsgroup post this reveals a bug in Thunderbird, because the headers
view isn't scrollable: not only can you not see all the headers, but
none of the message is visible either!
Post by Alf P. Steinbach
Or do as I wrote next and you snipped, use [View -> Message Source].
Yup, when I need to see that crap (which is almost never) I just hit
Ctrl/U and look at the headers in the message source.
Post by Alf P. Steinbach
Cheers & hth. (even if rather off-topic by now, not even direct response!),
I've sucked a few eggs in my time. Thanks.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
PyCon is coming! Atlanta, Feb 2010 http://us.pycon.org/
Holden Web LLC http://www.holdenweb.com/
UPCOMING EVENTS: http://holdenweb.eventbrite.com/
W. eWatson
2010-01-12 00:53:41 UTC
Permalink
Post by Martin P. Hellwig
Post by W. eWatson
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
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = int(now.strftime('%H')) +
int(now.strftime('%M')) / 60.0
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = now.hour + now.minute / 60.0
See my post about the datetime controversy about 3-4 posts up from yours.
Martin P. Hellwig
2010-01-12 14:52:40 UTC
Permalink
W. eWatson wrote:
<cut>
Post by W. eWatson
now = datetime.datetime.now()
fractional_hour = now.hour + now.minute / 60.0
See my post about the datetime controversy about 3-4 posts up from yours.
If timezones might be a problem area, than it might be worth while to
see it in the context of the actual application. For local, one user
only, use, the problem will be practically non-existent. Multiple users
across multiple machines will make it more difficult since you need a
verified source for each users timezone. But then again what about
travellers, wrongly set-up machines (right time wrong zone, wrong time
right zone and wrong zone with wrong time?) or people who just prefer to
do have their time set to UTC regardless of their location and season
(when I travelled alot, I just set my wristwatch, phone and laptop to UTC).
--
MPH
http://blog.dcuktec.com
'If consumed, best digested with added seasoning to own preference.'
Loading...