Python nanoseconds to time

Python 3: Unix Timestamp

A Unix timestamp is the number of seconds that have passed (elapsed) since the epoch (January 1, 1970 at 00:00:00 UTC). It provides a way to express any date and time as a single number without having to worry about multiple unit components (like hours and minutes) and time zones (since it uses UTC).

Python 3 provides functions for working with timestamps based on the operating system’s definition of «epoch,» which may be different from Unix’s definition. However, Windows and Unix-like systems such as Linux and macOS (10 and above) all use Unix’s definition of epoch (i.e., 1970-01-01 00:00:00 UTC), so Python’s timestamp functions are effectively Unix timestamp functions when they’re called on these operating systems. This article assumes we’re using one of these operating systems.

Current Unix Timestamp

Second Precision

In Python 3, we can get the Unix timestamp for the current time by using the built-in time module:

import time now = int( time.time() ) print( now )

The time.time() function by itself returns the Unix timestamp as a float , with any value to the right of the decimal point representing fractions of a second. However, Python does not make any guarantees about the exact level of precision of this number beyond whole seconds (the exact precision varies depending on the operating system). So in the example above, we convert it into an integer using the built-in int() function, which truncates float s toward zero (i.e., simply removes the fractional value for both positive and negative numbers).

Nanosecond Precision (Python 3.7 and Up)

On Python 3.7 and higher, we can get a guaranteed higher precision timestamp using the time.time_ns() function, which returns an integer representing the number of nanoseconds since the epoch. To get the current nanosecond precision timestamp, we can write:

import time now_ns = time.time_ns() print( now_ns )

Millisecond Precision (Python 3.7 and Up)

If we want millisecond precision, we can simply divide the nanosecond timestamp provided by time.time_ns() by 1,000 and then truncate:

import time now_ms = int( time.time_ns() / 1000 ) print( now_ms )

Convert datetime to Unix Timestamp

To convert a date and time specified in our operating system’s local time zone, we can first create a datetime object representing the date and time and then convert it to a Unix timestamp using the .timestamp() method.

Читайте также:  Int сколько знаков java

For example, to get the Unix timestamp (in UTC) for 2021-03-05 14:30:21 in the system’s local time zone, we can write:

from datetime import datetime dt = datetime( 2021, 3, 5, 14, 30, 21 ) timestamp = int( dt.timestamp() ) print( timestamp )

On Python versions 3.2 and higher, we can use the timezone class to specify any time zone we want. For example, to convert a datetime specified in UTC, we can write:

# Python 3.2 or higher required from datetime import datetime, timezone dt = datetime( 2021, 3, 5, 14, 30, 21, tzinfo=timezone.utc ) timestamp = int( dt.timestamp() ) print( timestamp )

Convert Unix Timestamp to datetime

If we want to find out what date and time a specific Unix timestamp represents, we can convert the timestamp to a datetime object.

For example, to convert a Unix timestamp to a datetime in the system’s local time zone, we can write:

from datetime import datetime timestamp = 1614983421 dt = datetime.fromtimestamp( timestamp ) print( dt )

On Python versions 3.2 and higher, we can use the timezone class to convert Unix timestamps into other time zones. For example, to get the UTC datetime corresponding to the Unix timestamp 1614954621 , we can write:

# Python 3.2 or higher required from datetime import datetime, timezone timestamp = 1614954621 dt = datetime.fromtimestamp( timestamp, tz=timezone.utc ) print( dt )

References

Источник

Python 3.7 nanoseconds

Thanks to my latest change on time.perf_counter(), all Python 3.7 clocks now use nanoseconds as integer internally. It became possible to propose again my old idea of getting time as nanoseconds at Python level and so I wrote a new PEP 564 «Add new time functions with nanosecond resolution». While the PEP was discussed, I also deprecated time.clock() and removed os.stat_float_times().

Читайте также:  Upcoming topics

Old clock

time.clock()

Since I wrote the PEP 418 «Add monotonic time, performance counter, and process time functions» in 2012, I dislike time.clock(). This clock is not portable: on Windows it mesures wall-clock, whereas it measures CPU time on Unix. Extract of time.clock() documentation:

Deprecated since version 3.3: The behaviour of this function depends on the platform: use perf_counter() or process_time() instead, depending on your requirements, to have a well defined behaviour.

My PEP 418 deprecated time.clock() in the documentation. In bpo-31803, I modified time.clock() and time.get_clock_info('clock') to also emit a DeprecationWarning warning. I replaced time.clock() with time.perf_counter() in tests and demos. I also removed hasattr(time, 'monotonic') in test_time since time.monotonic() is always available since Python 3.5.

os.stat_float_times()

The os.stat_float_times() function was introduced in Python 2.3 to get file modification times with sub-second resolution (commit f607bdaa), the default was still to get time as seconds (integer). The function was introduced to get a smooth transition to time as floating point number, to keep the backward compatibility with Python 2.2.

os.stat() was modified to return time as float by default in Python 2.5 (commit fe33d0ba). Python 2.5 was released 11 years ago, I consider that people had enough time to migrate their code to float time 🙂 I modified os.stat_float_times() in Python 3.1 to emit a DeprecationWarning warning (commit 034d0aa2 of bpo-14711).

Finally, I removed os.stat_float_times() in Python 3.7: bpo-31827.

Serhiy Storchaka proposed to also remove last three items from os.stat_result. For example, stat_result[stat.ST_MTIME] could be replaced with stat_result.st_time. But I tried to remove these items and it broke the logging module, so I decided to leave it unchanged.

Читайте также:  Java class file editor

PEP 564: time.time_ns()

Six years ago (2012), I wrote the PEP 410 «Use decimal.Decimal type for timestamps» which proposes a large and complex change in all Python functions returning time to support nanosecond resolution using the decimal.Decimal type. The PEP was rejected for different reasons.

Since all clock now use nanoseconds internally in Python 3.7, I proposed a new PEP 564 «Add new time functions with nanosecond resolution». Abstract:

Add six new «nanosecond» variants of existing functions to the time module: clock_gettime_ns(), clock_settime_ns(), monotonic_ns(), perf_counter_ns(), process_time_ns() and time_ns(). While similar to the existing functions without the _ns suffix, they provide nanosecond resolution: they return a number of nanoseconds as a Python int.

The time.time_ns() resolution is 3 times better than the time.time() resolution on Linux and Windows.

People were now convinced by the need for nanosecond resolution, so I added an «Issues caused by precision loss» section with 2 examples:

  • Example 1: measure time delta in long-running process
  • Example 2: compare times with different resolution

As for my previous PEP 410, many people proposed many alternatives recorded in the PEP: sub-nanosecond resolution, modifying time.time() result type, different types, different API, a new module, etc.

Hopefully for me, Guido van Rossum quickly approved my PEP for Python 3.7!

Implementaton of the PEP 564

I implemented my PEP 564 in bpo-31784 with the commit c29b585f. I added 6 new time functions:

  • time.clock_gettime_ns()
  • time.clock_settime_ns()
  • time.monotonic_ns()
  • time.perf_counter_ns()
  • time.process_time_ns()
  • time.time_ns()
$ python3.7 Python 3.7.0b2+ (heads/3.7:31e2b76f7b, Mar 6 2018, 15:31:29) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)] on linux >>> import time >>> time.time() 1520354387.7663522 >>> time.time_ns() 1520354388319257562

I also added tests on os.times() in test_os, previously the function wasn’t tested at all!

Conclusion

I added 6 new functions to get time with a nanosecond resolution like time.time_ns() with my approved PEP 564. I also modified time.clock() to emit a DeprecationWarning and I removed the legacy os.stat_float_times() function.

Источник

Оцените статью