Signup/Sign In
LAST UPDATED: SEPTEMBER 8, 2021

Time Module in Python and some of its useful Functions

Technology #Python#time Module

    In this tutorial, we are going to learn about various different methods which are present in the time module of python. Some of the time module methods are useful for developing applications. We will be exploring all the functions present in the current module. You can use them as you like once you kow what they can do for you.

    To use the time module in python, you must import it in your script so that all its methods are available for using. To import it in your script, use the following statement:

    import time


    Methods Of time Module

    Now, we will discuss different methods of time module one by one.

    1. time() function

    The time() function returns the no. of seconds elapsed since the epoch.

    Here is a simple code example:

    import time
    
    print(time.time())

    Output:

    The output will be a float value, which represents the seconds.


    2. gmtime() function

    The gmtime() function will return a structure of time with nine attributes describing the current year, moth, month_day, hours, minutes, seconds, week_day, year_day, isdst(daylight value).

    If you pass an argument with seconds to this method, it will return the above nine values since the epoch.

    Here is a simple code example:

    import time
    
    print("Calling without any parameter")
    print(time.gmtime())
    print()
    print("Passing seconds")
    print(time.gmtime(time.time()))

    Output:

    Calling without any parameter
    time.struct_time(tm_year=2019, tm_mon=6, tm_mday=26, tm_hour=9, tm_min=50, tm_sec=7, tm_wday=4, tm_yday=165, tm_isdst=0)
    
    Passing seconds
    time.struct_time(tm_year=2019, tm_mon=6, tm_mday=26, tm_hour=9, tm_min=50, tm_sec=7, tm_wday=4, tm_yday=165, tm_isdst=0)


    3. asctime([time]) function

    This function will return the time which humans can understand. We have to pass one argument called time, which is returned by the gmtime() function. If we don't pass any arguments to the method, then it will return the current time.

    Here is a simple code example:

    import time
    
    print(time.asctime(time.gmtime()))

    Output:

    Fri Jun 14 09:53:48 2019


    4. ctime([sec]) function

    The ctime() function will take seconds as argument and returns the time as we saw above. If we don't pass any arguments, then it will calculate the current time.

    Here is a simple code example:

    import time
    
    print(time.ctime(time.time()))

    Output:

    Fri Jun 14 15:31:29 2019


    5. sleep([sec]) function

    The sleep() function is used to halt the execution of the current thread for a specified number of seconds. It is the most used method of the time module.

    Here is a simple code example:

    import time
    
    print("Before execution")
    print(time.ctime(time.time()))
    for i in range(10):
    time.sleep(1)
    
    print()
    
    print("After execution")
    print(time.ctime(time.time()))

    Output:

    Run the above code, you will see ten seconds difference.

    You may also like:

    I am a Computer Science Student. I am interested in Programming. I love to know about new technologies.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS