Dev ❤ Ops

Time Module in Python

The time module in Python provides functions for working with time, including retrieving the current time and converting between different representations of time.

Here are some examples of using the time module:

import time

# Get the current time in seconds since the epoch (the epoch is a predefined point in time, usually the beginning of the year 1970)
current_time_in_seconds = time.time()

# Convert the current time to a struct_time object, which is a convenient way to represent the time in various formats
current_time = time.gmtime(current_time_in_seconds)

# Extract individual fields from the struct_time object
year = current_time.tm_year
month = current_time.tm_mon
day = current_time.tm_mday
hour = current_time.tm_hour
minute = current_time.tm_min
second = current_time.tm_sec

# Format the current time as a string
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S %Z %A", current_time)

# Parse a time string into a struct_time object
parsed_time = time.strptime("2022-12-26 13:37:00", "%Y-%m-%d %H:%M:%S")

print(current_time_in_seconds)
print(current_time)
print(year)
print(month)
print(day)
print(hour)
print(minute)
print(second)
print(formatted_time)
print(parsed_time)

Output:

1672086137.5064
time.struct_time(tm_year=2022, tm_mon=12, tm_mday=26, tm_hour=20, tm_min=22, tm_sec=17, tm_wday=0, tm_yday=360, tm_isdst=0)
2022
12
26
20
22
17
2022-12-26 20:22:17 UTC Monday
time.struct_time(tm_year=2022, tm_mon=12, tm_mday=26, tm_hour=13, tm_min=37, tm_sec=0, tm_wday=0, tm_yday=360, tm_isdst=-1)

Here are some additional examples of using the time module in Python:

import time

# Get the current time in milliseconds
current_time_in_milliseconds = int(time.time() * 1000)

# Convert a struct_time object to a timestamp (the number of seconds since the epoch)
timestamp = time.mktime(time.gmtime())

# Measure the time it takes to run a block of code
start_time = time.perf_counter()
# run some code here
elapsed_time = time.perf_counter() - start_time

# Measure the time it takes to run a block of code, including time spent in subfunctions
start_time = time.process_time()
# run some code here
elapsed_time = time.process_time() - start_time

# Convert a timestamp to a struct_time object
timestamp = 1609459200
time_struct = time.gmtime(timestamp)

# Format a struct_time object as a string
formatted_time = time.strftime("%A, %B %d, %Y %I:%M %p %Z", time_struct)
print(formatted_time)  # Output: "Sunday, September 13, 2020 12:00 AM UTC"

# Parse a string into a struct_time object
time_string = "Sunday, September 13, 2020 12:00 AM UTC"
parsed_time = time.strptime(time_string, "%A, %B %d, %Y %I:%M %p %Z")

Note that the time module only provides access to the system clock, so it is not suitable for measuring short intervals or for use in multi-threaded programs. For these purposes, you may want to use the perf_counter() or process_time() functions from the time module, or the timeit module.

For more information please visit python.org

This article is created based on experience but If you discover any corrections or enhancements, please write a comment in the comment section or email us at contribute@devopsforu.com. You can also reach out to us from Contact-Us Page.

Follow us on LinkedIn for updates!

Leave a comment

Your email address will not be published. Required fields are marked *

One thought on “Time Module in Python”