In this tutorial we will learn about how to use time module. We will explore the different functions of the time module.
Python has a module named Time which is used to deal with the time-related problems.Although it's a module and as we know we have to import the module to use them.So, here is the way of importing the time module.
import time
Time-related functions
There are so many time-related functions aailable in python.Here are some of them-
The time.time() function returns the number of seconds passed since epoch.
The epoch is the point where the time starts or epoch is defined as the point where the time begins.
For Unix system(Operating system), the time starts from January 01,1970 00:00:00(UTC) is epoch.
import time
t_seconds=time.time()
print("Total seconds since epoch:",t_seconds)
The above code will print out the number of seconds since epoch to the current time.
The time.ctime() function returns the local time when we give it input as number of seconds.The type of local time is string.
' Example:import time
# seconds passed since epoch
t_seconds = 1596273075.2050583
local_time = time.ctime(t_seconds)
print("Local time:", local_time)
print(type(local_time))
Output:
Local time: Sat Aug 1 14:41:15 2020
The above code is used to show how to convert the given no. of seconds into time.
There are several functions in the time module like localtime() takes time.struct_time as an object and return it.
It is an object with a named tuple interface: values can be accessed by index and by attribute name.
time.struct_time(tm_year,tm_mon,tm_mday.............,tm_isdst)
Attributes of struct_time()
There are 2 other attributes also- tm_zone- abbbreviation of timezone name and tm_gmtoff- offset east off UTC in seconds.
The function localtime() takes the number of seconds passed since epoch as an argument and it will return the struct_time in local time.
Syntax:time.localtime(no.of seconds since epoch)
Example:
In the aboe code you can see that we passed epoch in the function time.localtime() which will give the result with the local time.
The function gmtime() takes the number of seconds passed since epoch as an argument and it will return the struct_time in UTC.
Syntax:time.gmtime(no.of seconds since epoch)
Example:
The mktime() function takes struct_time as an input and it will return the no. of seconds passed since epoch in local time.
t_local=(tm_year,tm_month,.......,tm_isdst)
local_time=time.mktime(t_local)
Example:
Note:mktime() is the inverse of localtime().
The strftime() function takes struct_time> as an argument and it will give a string based upon the format given in the code.It will convert a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument.
Syntax:time.strftime("%m/%d/%Y, %H:%M:%S")
- %m:month(1,2...,12)
- %d:day(1,2...,31)
- %Y:Year(0001,....2019,2020,....9999)
- %H:Hour(00,01,02...,24)
- %M:minute(00,01,02...,59)
- %S:Second(00,01,02...,59)
Note:The order of date and time can be changed accordingly, but both cannot be link with each other,Let's take an example:
Example:
As you can see in the above code I changed day with month and vice-versa and it works very well.
The asctime() takes struct_time() as an argument and returns a string representing it.
Example:
You can see in the above code that how we can implement time.asctime() function to get the information.