Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement time.monotonic() on Mac OS X #58312

Closed
vstinner opened this issue Feb 23, 2012 · 8 comments
Closed

Implement time.monotonic() on Mac OS X #58312

vstinner opened this issue Feb 23, 2012 · 8 comments
Labels
stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 14104
Nosy @ronaldoussoren, @pitrou, @vstinner, @ned-deily
Files
  • monotonic.patch
  • monotonic-2.patch
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2012-03-12.23:27:28.933>
    created_at = <Date 2012-02-23.23:09:14.831>
    labels = ['library']
    title = 'Implement time.monotonic() on Mac OS X'
    updated_at = <Date 2012-03-12.23:29:14.621>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2012-03-12.23:29:14.621>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2012-03-12.23:27:28.933>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2012-02-23.23:09:14.831>
    creator = 'vstinner'
    dependencies = []
    files = ['24798', '24804']
    hgrepos = []
    issue_num = 14104
    keywords = ['patch']
    message_count = 8.0
    messages = ['154095', '155478', '155502', '155507', '155511', '155515', '155520', '155521']
    nosy_count = 6.0
    nosy_names = ['nicholas.riley', 'ronaldoussoren', 'pitrou', 'vstinner', 'ned.deily', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'patch review'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue14104'
    versions = ['Python 3.3']

    @vstinner
    Copy link
    Member Author

    time.monotonic() can use mach_absolute_time() on Mac OS X. mach_timebase_info() may be used to convert the result to a number of seconds. Examples:

    https://github.com/ThomasHabets/monotonic_clock/blob/master/src/monotonic_mach.c
    http://svn.boost.org/svn/boost/trunk/boost/chrono/detail/inlined/mac/chrono.hpp
    (search steady_clock)

    Another way is to use clock_get_time() with host_get_clock_service(SYSTEM_CLOCK). Example:

    https://github.com/gavinbeatty/python-monotonic-time/blob/master/darwin.c

    @vstinner vstinner added the stdlib Python modules in the Lib dir label Feb 23, 2012
    @nicholasriley
    Copy link
    Mannequin

    nicholasriley mannequin commented Mar 12, 2012

    Attached is a patch which implements time.monotonic() using mach_absolute_time() / mach_timebase_info(). This was recommended by Apple in their technical Q&A 1398:

    https://developer.apple.com/library/mac/#qa/qa1398/

    @vstinner
    Copy link
    Member Author

    Attached is a patch which implements time.monotonic() using
    mach_absolute_time() / mach_timebase_info().

    Is it possible that mach_timebase_info() fails? No according to the Technical Q&A QA1398.

    time * timebase.numer / timebase.denom

    "time * timebase.numer" may overflow, you should maybe multiply using the double type to avoid the overflow.

    @nicholasriley
    Copy link
    Mannequin

    nicholasriley mannequin commented Mar 12, 2012

    timebase.numer and timebase.denom are always 1 in practice (see the "note" in the tech Q&A); I included them for completeness, but maybe it's just better to not bother?

    @vstinner
    Copy link
    Member Author

    timebase.numer and timebase.denom are always 1 in practice

    In this case, you will not lose precision and it's safe to cast to double to multiply and divide ;-)

    @nicholasriley
    Copy link
    Mannequin

    nicholasriley mannequin commented Mar 12, 2012

    OK, you have a point :-) New patch attached.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Mar 12, 2012

    New changeset 3c875719e46d by Victor Stinner in branch 'default':
    Issue bpo-14104: Implement time.monotonic() on Mac OS X,
    http://hg.python.org/cpython/rev/3c875719e46d

    @vstinner
    Copy link
    Member Author

    Path commited, thanks for the patch Nicholas!

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant