Time module in Python
In this tutorial, you are going to learn about the inbuilt functions of the time module in Python. This module provides various functions to manipulate the time values.
Different functions of time() module in Python
- time()
Returns the current time in seconds since the epoch.
The epoch is system defined; on Unix, which is January 1st, 1970.import time as t # import the module print(" The time in seconds since epoch:", t.time())
Output:-
The time in seconds since epoch: 1562595208.919087
- time_ns ()
Returns the current time in nanoseconds since the epoch.import time as t # use of time_ns() print(" The time in nanoseconds since epoch:", t.time_ns())
Output:-
The time in nanoseconds since epoch: 1562595455438187200
- localtime ([seconds])
It converts the seconds since epoch to the time_tuple expressing the local time. When the seconds are not passed as the arguments, then it converts the current time and returns the tuple.import time as t print(" The time in seconds since epoch:", t.time()) # taking t.time() as argument print(t.localtime(t.time())) # without argument print(t.localtime())
Output:-
The time in seconds since epoch: 1562596465.8275158 time.struct_time(tm_year=2019, tm_mon=7, tm_mday=8, tm_hour=20, tm_min=4, tm_sec=25, tm_wday=0, tm_yday=189, tm_isdst=0) time.struct_time(tm_year=2019, tm_mon=7, tm_mday=8, tm_hour=20, tm_min=4, tm_sec=25, tm_wday=0, tm_yday=189, tm_isdst=0)
- asctime([time_tuple])
It converts a tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998’. When the tuple is not passed as the argument, then it returns the current date and time.import time as t print("The current date and time is:",t.asctime()) print("Using the local time as argument:",t.asctime(t.localtime()))
Output:-
The current date and time is: Mon Jul 8 20:41:57 2019 Using the local time as argument: Mon Jul 8 20:41:57 2019
- sleep(second)
It delays the execution for the given number of seconds.import time as t print("Sleep for 3 second:",t.sleep(3))
Output:-
Sleep for 3 second: None
Note: The output will show after the 3 seconds as the normal execution is delayed for 3 seconds.
- strftime([time_tuple])
It converts the tuple into a string according to a format specification.%Y represent year with century as a decimal number.
%m represent month as a decimal number [01-12].
%d represent day of the month as a decimal number [01-31].
%H represents the hour (24-hour clock) as a decimal number [00-23].
%M represent minute as a decimal number [00-59].
%S represent second as a decimal number [00,61].
%Z represent time zone.
%a represent locale’s abbreviated weekday name.
%A represent locale’s full weekday name.
%b represents locale’s abbreviated month name.
%B represent locale’s full month name.
%c represents the locale’s appropriate date and time representation.
%I represents the hour (12-hour clock) as a decimal number [01,12].
%p represents the locale’s equivalent of either AM or PM.import time as t rec=t.localtime(t.time()) print(t.strftime("%Y/%m/%d",rec)) print(t.strftime("%Y-%m-%d",rec)) print(t.strftime("%H:%M:%S %Z",rec)) print(t.strftime("%A %B",rec)) print(t.strftime("%a %b",rec)) print(t.strftime("%c",rec)) print(t.strftime("%I %p",rec))
Output:-
2019/07/08 2019-07-08 21:19:39 India Standard Time Monday July Mon Jul Mon Jul 8 21:19:39 2019 09 PM
Go and check other tutorials on python:
How to create your own package in Python
Python program to create a simple chat box.
Leave a Reply