The Wayback Machine - https://web.archive.org/web/20231106112506/https://www.geeksforgeeks.org/python-time-time_ns-method/
Open In App
Related Articles

Python | time.time_ns() method

Improve Article
Improve
Save Article
Save
Like Article
Like

Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules.

time.time_ns() method of Time module is used to get the time in nanoseconds since the epoch. To get the time in seconds since the epoch, we can use time.time() method.

The epoch is the point where the time starts and is platform dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC) and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).

Note: time.time_ns() method is new in Python version 3.7

Syntax:
time.time_ns()

Parameter:
No parameter is required.

Return type:
This method returns an integer value which
represents the time in nanoseconds since the epoch.

Code: Use of time.time_ns() method




# Python program to explain time.time_ns() method 
    
# importing time module 
import time 
    
# Get the epoch 
obj = time.gmtime(0) 
epoch = time.asctime(obj) 
print("epoch is:", epoch) 
    
# Get the time in seconds 
# since the epoch 
# using time.time() method
time_sec = time.time() 
  
# Get the time in nanoseconds
# since the epoch
# using time.time_ns() method
time_nanosec = time.time_ns()
    
# Print the time 
# in seconds since the epoch 
print("Time in seconds since the epoch:", time_sec) 
  
# Print the time 
# in nanoseconds since the epoch 
print("Time in nanoseconds since the epoch:", time_nanosec) 


Output:

epoch is: Thu Jan  1 00:00:00 1970
Time in seconds since the epoch: 1567451658.4676464
Time in nanoseconds since the epoch: 1567451658467647709

References: https://docs.python.org/3/library/time.html#time.time_ns

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 05 Sep, 2019
Like Article
Save Article
Similar Reads
Related Tutorials