Python 3 – time localtime() Method
The localtime() method of Python 3 is used to convert a specified time in seconds from the epoch time to a local time. The epoch time refers to the number of seconds since January 1, 1970, 00:00:00 UTC. In this article, we will discuss the syntax of the localtime() method, along with its parameters and return value. We will also explore some examples to understand the usage of the localtime() method.
Syntax of localtime() Method
The syntax of the localtime() method is as follows:
time.localtime([seconds])
Here, the square brackets indicate an optional parameter, which specifies the time in seconds. If the parameter is not specified, localtime() method returns the current time in seconds.
Parameters of localtime() Method
The localtime() method has only one optional parameter:
- seconds: It is an optional parameter, which specifies the time in seconds. If the parameter is not specified, the localtime() method returns the current time in seconds.
Return Value of localtime() Method
The localtime() method returns a tuple, which represents the local time. The tuple has the following elements:
- tm_year: It represents the year in 4 digits format.
- tm_month: It represents the month in the range of 1 to 12.
- tm_day: It represents the day of the month in the range of 1 to 31.
- tm_hour: It represents the hour of the day in the range of 0 to 23.
- tm_min: It represents the minute of the hour in the range of 0 to 59.
- tm_sec: It represents the second of the minute in the range of 0 to 61.
- tm_wday: It represents the day of the week in the range of 0 to 6. (0 is Monday)
- tm_yday: It represents the day of the year in the range of 1 to 366.
- tm_isdst: It represents the DST (Daylight Saving Time) flag. The value can be 0 (no DST) or 1 (DST).
Examples of localtime() Method
Let’s look at some examples to understand the usage of the localtime() method:
Example 1: Get current local time
import time
current_time = time.localtime()
print("Current Time:", current_time)
Output:
Current Time: time.struct_time(tm_year=2022, tm_mon=3, tm_mday=8, tm_hour=3, tm_min=12, tm_sec=39, tm_wday=1, tm_yday=67, tm_isdst=0)
In this example, we imported the time module and called the localtime() method without any parameter. The method returned the current local time in a tuple format.
Example 2: Get local time for a particular moment
import time
seconds = 1000000
local_time = time.localtime(seconds)
print("Local Time for", seconds, "seconds:", local_time)
Output:
Local Time for 1000000 seconds: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=9, tm_min=46, tm_sec=40, tm_wday=0, tm_yday=12, tm_isdst=0)
In this example, we passed the integer value of 1000000 seconds to localtime() method, which returns the local time of that particular moment. Here, the time is January 12, 1970, 09:46:40 AM.
Example 3: Get DST flag
import time
local_time = time.localtime()
print("DST flag:", local_time.tm_isdst)
Output:
DST flag: 0
In this example, we called the localtime() method without any parameter to get the current local time. Then we printed the tm_isdst value of the returned tuple. Here, the DST flag value is 0, which means there is no DST.
Conclusion
In this article, we discussed the localtime() method of Python 3, along with its usage, syntax, parameters, and return value. We also explored some examples to understand the implementation of the localtime() method. The localtime() method is useful for working with date and time in Python 3, and it can be customized as per the needs of the user. We hope this article helped you in understanding the localtime() method of Python 3.