FAQ
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

Search Discussions

  • Austyn at Jan 11, 2010 at 4:04 am
    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" 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 Dave Mon Jan 11 05:16:03 2010
    From: Dave (Dave)
    Date: Sun, 10 Jan 2010 22:16:03 -0600
    Subject: interactive terminal in Ubuntu Linux : libreadline5-dev works
    only in Python 2.6 not 3.1
    References: <[email protected]>
    <23c7[email protected]>
    <[email protected]>
    <15a78[email protected]>
    Message-ID: <[email protected]>
    On Sat, 9 Jan 2010 16:48:52 -0800 (PST), casevh wrote:
    On Jan 9, 3:10?pm, pdlem... at earthlink.net wrote:
    On Sat, 9 Jan 2010 13:27:07 -0800 (PST), casevh <cas... at gmail.com>
    wrote:
    1) Try the commands again. Make sure all the "./configure" options are
    on one line. Make sure to do "sudo make altinstall". (Don't use "sudo
    make install"; it will give your version of Python the name "python"
    and that can cause confusion on your system.)

    2) Move your applications to another directory.

    3) Try running "python3.1" while you are in that directory.

    If this doesn't work, report back on the error messages you receive.
    Thanks casevh for your time & patience.
    The ./configure . . . was a "continuous" line , although it ran over to
    next line even on 132 char wide terminal because of names of system &
    dir. I was careful with the spaces and hyphens.
    In my reply the "make install" was a typo , I did run make altinstall.

    Moved all my code to pycode dir on my home directory. Removed all
    files from /usr/local/lib/python3.1/dlmodules and removed that dir.

    Twice ran the recompile :
    make distclean
    ./configure --prefix=/usr/local --with-computed-gotos
    --with-wide-unicode
    <^ one space>
    make
    sudo make altinstall
    After each reinstall had same problems : cannot import random or
    any code using it, although random.py is in Lib :
    Traceback
    File "<stdin>", line 1, in <module>
    File "random.py", line 46, in <module>
    import collections as _collections
    File "collections.py", line 9 in <module>
    from _collections import deque, default dict
    ImportError: /usr/local/lib/python3.1/lib-dynload/collections.so:
    undefined symbol: PyUnicodeUCS4_FromString

    After second reinstall today I also found that a module importing
    "time" would not run. Likewise could not import time at >>> .
    Same error, ending : undefined symbol: PyUnicode UCS4_FromString

    And my original problem still there : fouled up keys in interactive
    terminal. Seems minor now ; ) Should I try to remove everything
    and reopen the tarball ?
    Dave WB3DWE, central Texas
  • Austyn at Jan 11, 2010 at 4:23 am
    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 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" 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
  • W. eWatson at Jan 11, 2010 at 4:44 pm

    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 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" 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/>
  • M.-A. Lemburg at Jan 11, 2010 at 12:12 pm

    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/
  • Martin P. Hellwig at Jan 11, 2010 at 1:18 pm

    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.'
  • Martin P. Hellwig at Jan 11, 2010 at 1:25 pm

    Martin P. Hellwig wrote:
    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
    Actually my version is overcomplicated:
    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 at Jan 12, 2010 at 12:53 am

    Martin P. Hellwig wrote:
    Martin P. Hellwig wrote:
    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
    Actually my version is overcomplicated:
    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 at Jan 12, 2010 at 1:27 am

    "W. eWatson" <wolftracks at invalid.com> writes:

    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 at Jan 12, 2010 at 2:27 am

    Ben Finney wrote:
    "W. eWatson" <wolftracks at invalid.com> writes:
    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 at Jan 12, 2010 at 2:32 am

    "W. eWatson" <wolftracks at invalid.com> writes:

    Ben Finney wrote:
    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 at Jan 12, 2010 at 2:47 am

    * W. eWatson:
    Ben Finney wrote:
    "W. eWatson" <wolftracks at invalid.com> writes:
    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).

    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/>
    No, not at all. :-)

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


    Cheers & hth.,

    - Alf
  • Steve Holden at Jan 12, 2010 at 3:03 am

    Alf P. Steinbach wrote:
    * W. eWatson:
    Ben Finney wrote:
    "W. eWatson" <wolftracks at invalid.com> writes:
    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 at Jan 12, 2010 at 3:26 am

    "Alf P. Steinbach" <alfps at start.no> writes:

    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 at Jan 12, 2010 at 3:59 am

    On Mon, Jan 11, 2010 at 10:26 PM, Ben Finney wrote:
    "Alf P. Steinbach" <alfps at start.no> writes:
    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 at Jan 12, 2010 at 7:51 pm

    Ben Finney wrote:
    "Alf P. Steinbach" <alfps at start.no> writes:
    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 at Jan 12, 2010 at 4:23 am

    * Steve Holden:
    Alf P. Steinbach wrote:
    * W. eWatson:
    Ben Finney wrote:
    "W. eWatson" <wolftracks at invalid.com> writes:
    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 at Jan 12, 2010 at 10:24 am

    Alf P. Steinbach wrote:
    * Steve Holden:
    Alf P. Steinbach wrote:
    * W. eWatson:
    Ben Finney wrote:
    "W. eWatson" <wolftracks at invalid.com> writes:
    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!
    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.
    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 at Jan 12, 2010 at 12:53 am

    Martin P. Hellwig wrote:
    Martin P. Hellwig wrote:
    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
    Actually my version is overcomplicated:
    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 at Jan 12, 2010 at 2:52 pm
    W. eWatson wrote:
    <cut>
    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.'

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 10, '10 at 5:28p
activeJan 12, '10 at 7:51p
posts20
users8
websitepython.org

People

Translate

site design / logo © 2023 Grokbase