Python Time Module
In this article, we will discuss the time module and various functions provided by this module with the help of good examples.
As the name suggests Python time module allows to work with time in Python. It allows functionality like getting the current time, pausing the Program from executing, etc. So before starting with this module we need to import it.
Importing time module
The time module comes with Python’s standard utility module, so there is no need to install it externally. We can simply import it using the import statement.
What is epoch?
The epoch is the point where the time starts and is platform-dependent. On Windows and most Unix systems, the epoch is January 1, 1970, 00:00:00 (UTC), and leap seconds are not counted towards the time in seconds since the epoch. To check what the epoch is on a given platform we can use time.gmtime(0).
Example: Getting epoch
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)
From the above example, you can see that epoch is 1 January 1970. This means that 2 January 1970 can be expressed as 86400 seconds since epoch as there are 86400 seconds in a day.
Note: The time before the epoch can still be represented in seconds but it will be negative. For example, 31 December 1969 will be represented as -86400 seconds.
Getting current time in seconds since epoch
time.time() methods return the current time in seconds since epoch. It returns a floating-point number.
Example: Current time in seconds since epoch
Current time in seconds since epoch = 1627908387.764925
Getting time string from seconds
time.ctime() function returns a 24 character time string but takes seconds as argument and computes time till mentioned seconds. If no argument is passed, time is calculated till the present.
Example: Getting time string from seconds
Current time: Mon Aug 2 12:45:13 2021
Delaying Execution of programs
Execution can be delayed using time.sleep() method. This method is used to halt the program execution for the time specified in the arguments.
Example: Delaying execution time of programs in Python.
0 1 2 3
Struct_time class helps to access local time i.e. non-epochal timestamps. It returns a named tuple whose value can be accessed by both index and attribute name. Its object contains the following attributes –
|0||tm_year||0000, …, 9999|
|1||tm_mon||1, 2, …, 11, 12|
|2||tm_mday||1, 2, …, 30, 31|
|3||tm_hour||0, 1, …, 22, 23|
|4||tm_min||0, 1, …, 58, 59|
|5||tm_sec||0, 1, …, 60, 61|
|6||tm_wday||0, 1, …, 6; Sunday is 6|
|7||tm_yday||1, 2, …, 365, 366|
|8||tm_isdst||0, 1 or -1|
This class contains various functions. Let’s discuss each function in detail.
localtime() method returns the struct_time object in local time. It takes the number of seconds passed since epoch as an argument. If the seconds parameter is not given then the current time returned by time.time() method is used.
Example: Getting local time from epoch
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=16, tm_min=15, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=0)
time.mktime() is the inverse function of time.localtime() which converts the time expressed in seconds since the epoch to a time.struct_time object in local time.
Example: Converting the struct_time object to seconds since epoch
Local time (in seconds): 1627987508.0
time.gmtime() is used to convert a time expressed in seconds since the epoch to a time.struct_time object in UTC in which tm_isdst attribute is always 0. If the seconds parameter is not given then the current time returned by time.time() method is used.
Example: Use of time.gmtime() method
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=0)
time.strftime() function converts a tuple or struct_time representing a time as returned by gmtime() or localtime() to a string as specified by the format argument. If t is not provided, the current time as returned by localtime() is used. The format must be a string. ValueError is raised if any field in t is outside of the allowed range.
Example: Converting struct_time object to a string using strftime() method
Tue, 03 Aug 2021 10:45:08
time.asctime() method is used to convert a tuple or a time.struct_time object representing a time as returned by time.gmtime() or time.localtime() method to a string of the following form:
Day Mon Date Hour:Min:Sec Year
Example: Converting tuple to time.struct_time object to string
Tue Aug 3 10:45:08 2021 Tue Aug 3 10:45:08 2021
time.strptime() method converts the string representing time to the struct_time object.
Example: Converting string to struct_time object.
time.struct_time(tm_year=2021, tm_mon=8, tm_mday=3, tm_hour=10, tm_min=45, tm_sec=8, tm_wday=1, tm_yday=215, tm_isdst=-1)
Please Login to comment...