Python | time.localtime () method

Python Methods and Functions

time.localtime() Unit Time is used to convert time expressed in seconds since the beginning of an epoch to the time.struct_time object in local time. 
To convert the specified time in seconds from the beginning of the epoch to time.struct_time object to UTC time.gmtime () method time.gmtime() .

This method returns a time.struct_time object with a named tuple interface. Below are the values ​​present in the 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: Using the time.localtime() method

Index Attribute Values ​​
0 tm_year ( for example, 1993)
1 tm_mon range [1, 12]
2 tm_mday range [1, 31]
3 tm_hour range [0, 23]
4 tm_min range [0, 59]
5 tm_sec range [0, 61]
6 tm_wday range [0, 6], Monday is 0
7 tm_yday range [1, 366]
8 tm_isdst 0, 1 or -1
N / A tm_zone abbreviation of timezone name
N / A tm_gmtoff offset east of UTC in seconds

# Python program to explain the time.localtime () method

  
# time module import

import time

 
# If the secs parameter
# not given then
# current time as
# returned by the time.time () method
# used

  
# Convert current time in seconds
# from era to
# time.struct_time object in local time

obj = time.localtime ()

 
# Print the time.struct.time object

print (obj)

 
# We can change this to
# Day Mon date Hour: Min: Sec Year
# format using the time.asctime () method

t = time.asctime (obj)

print (t)

Exit:

 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: Using the time.localtime ()

# Python program to explain the time.localtime () method

 
# import time module

import time

 
# Time in seconds
# since epoch

secs = 950000000

 
# Convert specified time in seconds
# from epoch to
# time.struct_time object in local time
# using the time.localtime () method

obj = time.localtime (secs)

  
# Print the time object. struct_time

print ( "time.struct_time object for seconds = " , secs)

print (obj)

  

  
# Time in seconds
# with e secs

secs = 950000000.81956

 
# Convert specified time in seconds
# from epoch to
# time.struct_time object in local time
# using the time.localtime () method

obj = time.localtime (secs)

  
# Print the object time.struct_time

print ( "time.struct_time object for seconds = " , secs)

print (obj)

 

  
# Exit in sec = 950000000
# and sec = 950000000.81956
# will be the same because
# factions in 950000000.81956
# i.e. .81956 will be ignored

Exit:

 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) 

Link: https:// docs .python.org / 3 / library / time.html # time.localtime





Get Solution for free from DataCamp guru