In Python 3.10, the time module provides various functions to work with time information. Here are some examples of how to use time in Python 3.10:
1. Get the Current Time
To get the current time, we use the time() function from the time module. This function returns the number of seconds since the Epoch (January 1, 1970, 00:00:00 UTC).
import time
current_time = time.time()
print(current_time)
Output:
1631084484.117079
2. Convert Timestamp to Human-Readable Format
We can convert a timestamp (number of seconds since the Epoch) to a human-readable format using the strftime() function. This function takes a format string as an argument and returns a string representing the timestamp in that format.
import time
timestamp = 1631084484
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(timestamp))
print(formatted_time)
Output:
2021-09-08 15:28:04
3. Sleep for a Given Amount of Time
We can make our program pause (sleep) for a given amount of time using the sleep() function. This function takes the number of seconds to sleep as an argument.
import time
print("Starting task...")
time.sleep(5)
print("Task completed!")
Output:
Starting task...
Task completed!
These are just a few examples of how to use time in Python 3.10. You can check the Python 3.10 documentation for more information and functions provided by the time module.