Click to share! ⬇️

The Python time module provides functions for working with time and dates. It includes functions for getting the current time, converting between different time representations, and performing time-related operations such as delay and time zone conversions. Here are some of the key functions provided by the time module:

time.time()

time.time() returns the current time in seconds since the Unix epoch (the point in time at which the Unix operating system was started on January 1, 1970) as a floating-point number.

The time.time() function returns the current time in seconds since the Unix epoch (the point in time at which the Unix operating system was started on January 1, 1970) as a floating-point number. Here is an example of how to use the time.time() function in Python:

import time

# Get the current time in seconds since the Unix epoch
current_time = time.time()

# Print the current time
print(current_time)

This code will print the current time as a floating-point number. On most systems, the time will be represented as a decimal number with a large integer part (representing the number of seconds since the Unix epoch) and a decimal part representing the fraction of a second.

Here is an example of the output you might see from this code:

1607643724.163963

This number represents the time at which the time.time() function was called, in seconds since the Unix epoch.

If you want to display the current time in a more human-readable format, you can use the time.strftime() function to convert the time to a string representation according to a specified format string. For example:

import time

# Get the current time in seconds since the Unix epoch
current_time = time.time()

# Convert the time to a string in the format "YYYY-MM-DD HH:MM:SS"
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)

# Print the formatted time
print(formatted_time)

This code will convert the current time to a string in the format “YYYY-MM-DD HH:MM:SS” and print the resulting string. Here is an example of the output you might see from this code:

2022-12-06 22:12:04

This represents the current date and time in the specified format.

time.sleep()

time.sleep(seconds) causes the program to pause for the specified number of seconds. This can be used to delay the execution of code or to control the rate at which a loop is executed.

The time.sleep() function causes the program to pause for the specified number of seconds. This can be useful for delaying the execution of code, or for controlling the rate at which a loop is executed. Here is an example of how to use the time.sleep() function in Python:

import time

# Print "Hello, world!" every second for 5 seconds
for i in range(5):
    print("Hello, world!")
    time.sleep(1)

This code will print the message “Hello, world!” five times, with a one-second delay between each message. This means that the entire loop will take five seconds to complete.

Here is the output you might see from this code:

Hello, world!
Hello, world!
Hello, world!
Hello, world!
Hello, world!

As you can see, the time.sleep() function causes the program to pause for the specified number of seconds before continuing with the next iteration of the loop. This can be useful for controlling the rate at which a loop is executed, or for introducing delays between different actions in your program.

time.strftime()

time.strftime(format, time) converts a time expressed in seconds since the Unix epoch to a string representation according to the specified format string.

The time.strftime() function converts a time expressed in seconds since the Unix epoch to a string representation according to a specified format string. Here is an example of how to use the time.strftime() function in Python:

import time

# Get the current time in seconds since the Unix epoch
current_time = time.time()

# Convert the time to a string in the format "YYYY-MM-DD HH:MM:SS"
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)

# Print the formatted time
print(formatted_time)

This code will convert the current time to a string in the format “YYYY-MM-DD HH:MM:SS” and print the resulting string. Here is an example of the output you might see from this code:

2022-12-06 22:12:04

This represents the current date and time in the specified format.

The format string used in the time.strftime() function specifies the desired format for the resulting string. It is made up of format specifiers that represent different parts of the date and time, such as the year, month, day, hour, minute, and second.

For example, the format string "%Y-%m-%d %H:%M:%S" used in the code above contains the following format specifiers:

  • %Y represents the year with four digits (e.g. 2022)
  • %m represents the month with two digits (e.g. 12)
  • %d represents the day of the month with two digits (e.g. 06)
  • %H represents the hour with two digits in 24-hour format (e.g. 22)
  • %M represents the minute with two digits (e.g. 12)
  • %S represents the second with two digits (e.g. 04)

These format specifiers are combined to produce a string in the desired format.

time.strptime()

time.strptime(string, format) parses a string representing a time according to the specified format string and returns the time in seconds since the Unix epoch.

The time.strptime() function parses a string representing a time according to a specified format string and returns the time in seconds since the Unix epoch. This is the opposite of the time.strftime() function, which converts a time in seconds since the Unix epoch to a string representation according to a format string.

Here is an example of how to use the time.strptime() function in Python:

import time

# Parse a string representing a time in the format "YYYY-MM-DD HH:MM:SS"
time_string = "2022-12-06 22:12:04"
time_format = "%Y-%m-%d %H:%M:%S"
parsed_time = time.strptime(time_string, time_format)

# Print the parsed time
print(parsed_time)

This code will parse the given time string according to the specified format string, and print the resulting time. The output you might see from this code would look something like this:

time.struct_time(tm_year=2022, tm_mon=12, tm_mday=6, tm_hour=22, tm_min=12, tm_sec=4, tm_wday=1, tm_yday=341, tm_isdst=-1)

This is a time.struct_time object containing the parsed time. This object has attributes for each part of the date and time, such as tm_year for the year, tm_mon for the month, and tm_sec for the second. You can access these attributes to get the individual parts of the parsed time.

Here is an example of how to access the individual parts of the parsed time:

import time

# Parse a string representing a time in the format "YYYY-MM-DD HH:MM:SS"
time_string = "2022-12-06 22:12:04"
time_format = "%Y-%m-%d %H:%M:%S"
parsed_time = time.strptime(time_string, time_format)

# Print the year, month, and day from the parsed time
print(parsed_time.tm_year)
print(parsed_time.tm_mon)
print(parsed_time.tm_mday)

This code will parse the given time string and print the year, month, and day from the parsed time. The output you might see from this code would look something like this:

2022
12
6

To use the time module in your code, you must first import it:

import time

Then you can use any of the functions from the time module by calling them with the time prefix, like this:

current_time = time.time()
time.sleep(1)
formatted_time = time.strftime("%Y-%m-%d %H:%M:%S", current_time)

For more information and a complete list of the functions provided by the time module, please see the Python Time Documentation.

Click to share! ⬇️