Get POSIX/Unix time in seconds and nanoseconds in Python?

后端 未结 6 1334
春和景丽
春和景丽 2020-12-01 10:33

I\'ve been trying to find a way to get the time since 1970-01-01 00:00:00 UTC in seconds and nanoseconds in python and I cannot find anything that will give me the proper pr

相关标签:
6条回答
  • 2020-12-01 10:44

    Since Python 3.7 it's easy to achieve with time.time_ns()

    Similar to time() but returns time as an integer number of nanoseconds since the epoch.

    All new features that includes nanoseconds in Python 3.7 release: PEP 564: Add new time functions with nanosecond resolution

    0 讨论(0)
  • 2020-12-01 10:46

    The problem is probably related to your OS, not Python. See the documentation of the time module: http://docs.python.org/library/time.html

    time.time()

    Return the time as a floating point number expressed in seconds since the epoch, in UTC. Note that even though the time is always returned as a floating point number, not all systems provide time with a better precision than 1 second. While this function normally returns non-decreasing values, it can return a lower value than a previous call if the system clock has been set back between the two calls.

    In other words: if your OS can't do it, Python can't do it. You can multiply the return value by the appropriate order of magnitude in order to get the nanosecond value, though, imprecise as it may be.

    EDIT: The return is a float variable, so the number of digits after the comma will vary, whether your OS has that level of precision or not. You can format it with "%.nf" where n is the number of digits you want, though, if you want a fixed point string representation.

    0 讨论(0)
  • 2020-12-01 10:48

    It depends on the type of clock and your OS and hardware wether or not you can even get nanosecond precision at all. From the time module documentation:

    The precision of the various real-time functions may be less than suggested by the units in which their value or argument is expressed. E.g. on most Unix systems, the clock “ticks” only 50 or 100 times a second.

    On Python 3, the time module gives you access to 5 different types of clock, each with different properties; some of these may offer you nanosecond precision timing. Use the time.get_clock_info() function to see what features each clock offers and what precision time is reported in.

    On my OS X 10.11 laptop, the features available are:

    >>> for name in ('clock', 'monotonic', 'perf_counter', 'process_time', 'time'):
    ...     print(name, time.get_clock_info(name), sep=': ')
    ...
    clock: namespace(adjustable=False, implementation='clock()', monotonic=True, resolution=1e-06)
    monotonic: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)
    perf_counter: namespace(adjustable=False, implementation='mach_absolute_time()', monotonic=True, resolution=1e-09)
    process_time: namespace(adjustable=False, implementation='getrusage(RUSAGE_SELF)', monotonic=True, resolution=1e-06)
    time: namespace(adjustable=True, implementation='gettimeofday()', monotonic=False, resolution=1e-06)
    

    so using the time.monotonic() or time.perf_counter() functions would theoretically give me nanosecond resolution. Neither clock gives me wall time, only elapsed time; the values are otherwise arbitrary. They are however useful for measuring how long something took.

    0 讨论(0)
  • 2020-12-01 10:49

    I don't think there's a platform-independent way (maybe some third party has coded one, but I can't find it) to get time in nanoseconds; you need to do it in a platform-specific way. For example, this SO question's answer shows how to do it on platform that supply a librt.so system library for "realtime" operations.

    0 讨论(0)
  • 2020-12-01 10:51

    Your precision is just being lost due to string formatting:

    >>> import time
    >>> print "%.20f" % time.time()
    1267919090.35663390159606933594
    
    0 讨论(0)
  • 2020-12-01 10:58

    It is unlikely that you will actually get nanosecond precision from any current machine.

    The machine can't create precision, and displaying significant digits where not appropriate is not The Right Thing To Do.

    0 讨论(0)
提交回复
热议问题