The Python time library is quite different from that of other datetime and other libraries as it solely deals with the manipulation of time either in real-life or virtual applications. The time module can be invoked implicitly by simple functions. Let’s take a look into the different classes and methods that the time library offers:

This is how you can import the time library:

import time

Python time.time()

The time.time() function is derived from the time library, it can return an object containing the total seconds passed from 1 January 1970 UTC which is a starting point of time for all programming languages:

import time #This invokes the time library which includes all the important functions and classes
 
timef = time.time() #time.time() returns the seconds passed from the last epoch and saves it in timef object
print(timef)

Python time.sleep()

The python time.sleep() function allows a user to introduce a delay between the calling of the sleep function and the command after it. It is usually used in programming to induce delays.

import time #This invokes the time library which includes all the important functions and classes
 
x = 2 + 5
print("The value of X will be printed after 3 seconds: ") #This will be printed immediately!
time.sleep(3.0) #This function does not need to be saved in an object, it can be directly called.
print("The value of X is:", x) #This statement will be printed after 3.0 seconds.

Python time.ctime()

The python time.ctime() function translates a given amount of seconds into a time-date format, it counts the seconds from 1st January 1970, UTC. If we don’t pass any argument to the function, it will return local time.

import time #This invokes the time library which includes all the important functions and classes\
 
sp0 = 876424444 #A variable containing user specified seconds
sp = time.ctime(sp0) #time.ctime() translates the given seconds into a time-date format and displays it.
print("Current Time:", sp) #prints the time-date format.

Python time.gmtime()

The python time.gmtime() function takes an argument in a similar manner to time.ctime() but it translates the given argument into a structure of day, month, year, hours, minutes and seconds including other details in UTC. Let’s take a look on how it is will be done:

import time #This invokes the time library which includes all the important functions and classes\
 
sp0 = 8764242423 #Seconds passed since 1st January 1970.
sp = time.gmtime(sp0) #time.gmtime taking sp0 variable as an input.
 
print(sp) #This will present the result in the form: time.struct_time(tm_year=1997, tm_mon=10, tm_mday=9, tm_hour=19, tm_min=10, tm_sec=42, tm_wday=3, tm_yday=282, tm_isdst=0)

Python time.mktime()

The Python time.mktime() function is the inverse of time.gmtime() function, the gmtime() takes in raw data of seconds passed since 1st January 1970 and structures it into a list/tuple of 9 elements. Whereas, the mktime() function, takes that tuple and converts it into a seconds format:

import time #This invokes the time library which includes all the important functions and classes
 
sp0 = (1997,10,9,19,10,42,3,282,0) # time.mktime() will take in input in this format: time.struct_time(tm_year=1997, tm_mon=10, tm_mday=9, tm_hour=19, tm_min=10, tm_sec=42, tm_wday=3, tm_yday=282, tm_isdst=0)
sp = time.mktime(sp0) #If we do not assign any input to the mktime() function, it will not work
print(sp)

Python time.localtime()

The Python time.localtime() function works in the same manner to time.gmtime() and time.ctime(), the only difference being that it returns the struct in a general format of time and not in UTC. Let’s see how it works:

import time #This invokes the time library which includes all the important functions and classes
 
sp0 = 8674242424 #The seconds passed since 1st January 1970
sp = time.localtime(sp0) #assigning sp0 variable to the time.localtime() function.
 
print(sp) #This will print the following statement: time.struct_time(tm_year=2244, tm_mon=11, tm_mday=16, tm_hour=12, tm_min=47, tm_sec=4, tm_wday=5, tm_yday=321, tm_isdst=0)

If we do not pass any argument to the time.localtime() function, it will return the current time in a tuple struct like this:

time.struct_time(tm_year=2022, tm_mon=12, tm_mday=14, tm_hour=20, tm_min=35, tm_sec=29, tm_wday=2, tm_yday=348, tm_isdst=0)

Python time.asctime()

The Python time.asctime() works in the same manner as time.gmtime(), the only difference is that time.asctime() takes in a tuple consisting of 9 time elements and structures it into a general time format, DD/MM/YY, HH/MM/SS:

import time #This invokes the time library which includes all the important functions and classes
 
sp0 = (1997,10,9,19,10,42,3,282,0) #This is the tuple we generated earlier on.
sp = time.asctime(sp0) #The asctime() function will convert it into a general time and date format
print(sp) # the time and date format will be printed on the screen, in such a manner: "Thu Oct  9 19:10:42 1997"

Categorized in: