Python time localtime() Method



The Python time localtime() method converts the Python time to a local time. Python time is said to be the number of seconds that have been elapsed since the epoch of a system; whereas the local time is the time relative to the geographical location of the system. The local time is also represented by an object.

This method is similar to localtime() method and accepts a floating-point number as its optional argument. If it is not provided or is None, the current time as returned by the time() method is used. However, unlike the localtime() method, the dst flag in the object is set to 1 when DST is applied to the given time.

Syntax

Following is the syntax for the Python time localtime() method −

time.localtime([ sec ])

Parameters

  • sec − (Optional) A floating-point number representing the number of seconds elapsed.

Return Value

This method returns the time elapsed since the epoch in a system as an object.

Example

The following example shows the usage of the Python time localtime() method. We are not passing any values to the optional parameter of this method. Hence, the method takes the return value of the time() method as its argument by default. The method returns the current time as an object.

import time

lt = time.localtime()
print("Current local time:", lt)

When we run above program, it produces following result −

Current local time: time.struct_time(tm_year=2023, tm_mon=1, tm_mday=9, tm_hour=12, tm_min=28, tm_sec=40, tm_wday=0, tm_yday=9, tm_isdst=0)

Example

If the argument passed is an integer representing seconds elapsed from the epoch of the system, the method returns the object representation of the date after the elapsed time.

In the example below, we are trying to find the date after 1000 seconds from the epoch. The epoch of the system this program is executed in, is "Thu Jan 1 05:30:00 1970" in local time. This method returns the time after the elapsed seconds.

import time

# Passing the seconds elapsed as an argument to this method
lt = time.localtime(1000)
print("Python local time:", lt)

Python local time: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=46, tm_sec=40, tm_wday=3, tm_yday=1, tm_isdst=0)

Example

The localtime() method can also be used to get the epoch of a system, in local time.

The method is said to return the local time after the given elapsed seconds, calculated based on the epoch of a system. Thus, if we pass the argument as '0', the method returns the epoch of the system.

import time

# Passing the seconds elapsed as an argument to this method
lt = time.localtime(0)
print("The epoch of the system:", lt)

Let us compile and run the program above, to produce the following result −

The epoch of the system: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Example

This method does not consider the fractional part of the floating-point number arguments that represent elapsed seconds.

Let us pass 2.99 seconds as an argument to the localtime() method. Even if the number is almost equal to 3 seconds, the method is expected to completely ignore the fractional part and consider the argument as only 2 seconds. It is shown in the example below.

import time

# Passing the seconds elapsed as an argument to this method
lt = time.localtime(2.99)
print("Time after elapsed seconds:", lt)

If we compile and run the program above, the output is as follows −

Time after elapsed seconds: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=5, tm_min=30, tm_sec=2, tm_wday=3, tm_yday=1, tm_isdst=0)
python_date_time.htm
Advertisements