Python | time.localtime() method

12 Oct, 2021
Time module in Python provides various time-related functions. This module comes under Python’s standard utility modules. time.localtime() method of Time module is used to convert a time expressed in seconds since the epoch to a time.struct_time object in local time. To convert the given time in seconds since the epoch to a time.struct_time object in UTC, time.gmtime() method is used. This method returns a time.struct_time object with a named tuple interface. Following are the values present in time.struct_time object:  
 
Syntax: time.localtime([secs])Parameter: secs (optional): An integer or float value representing time in seconds. Fractions of specified seconds will be ignored. If secs parameter is not provided or None then the current time as returned by time.time() method is used. Return type: This method returns an object of class ‘time.struct_time’.  
Code #1: Use of time.localtime() method  
Python3
# Python program to explain time.localtime() method # importing time moduleimport time # If secs parameter# is not given then# the current time as# returned by time.time() method# is used # Convert the current time in seconds# since the epoch to a# time.struct_time object in Local timeobj = time.localtime() # Print the time.struct.time objectprint(obj) # We can change it to# Day Mon date Hour:Min:Sec year# format using time.asctime() methodt = time.asctime(obj)print(t)
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=10, tm_min=3,
tm_sec=15, tm_wday=3, tm_yday=234, tm_isdst=0)
Thu Aug 22 10:03:15 2019
 
Code #2: Use of time.localtime() method  
Python3
# Python program to explain time.localtime() method # importing time moduleimport time # Time in seconds# since the epochsecs = 950000000 # Convert the given time in seconds# since the epoch to a# time.struct_time object in local time# using time.localtime() methodobj = time.localtime(secs) # Print the time.struct_time objectprint("time.struct_time object for seconds =", secs)print(obj)  # Time in seconds# since the epochsecs = 950000000.81956 # Convert the given time in seconds# since the epoch to a# time.struct_time object in local time# using time.localtime() methodobj = time.localtime(secs) # Print the time.struct_time objectprint("\ntime.struct_time object for seconds =", secs)print(obj)  # Output for secs = 950000000# and secs = 950000000.81956# will be same because# fractions in 950000000.81956# i.e .81956 will be ignored
time.struct_time object for seconds = 950000000
time.struct_time(tm_year=2000, tm_mon=2, tm_mday=8, tm_hour=14, tm_min=23,
tm_sec=20, tm_wday=1, tm_yday=39, tm_isdst=0)

time.struct_time object for seconds = 950000000.81956
time.struct_time(tm_year=2000, tm_mon=2, tm_mday=8, tm_hour=14, tm_min=23,
tm_sec=20, tm_wday=1, tm_yday=39, tm_isdst=0)
 
Reference: https://docs.python.org/3/library/time.html#time.localtime 
sooda367
python-utility
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Different ways to create Pandas Dataframe
Enumerate() in Python
Read a file line by line in Python
How to Install PIP on Windows ?
*args and **kwargs in Python
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Iterate over a list in Python