python


Python Time Module


In this tutorial we will learn about how to use time module. We will explore the different functions of the time module.

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-

  1. time.time()
  2. 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.

    Example:
    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.
  3. time.ctime()
  4. 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.

time.struct_time class

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.

Syntax:
time.struct_time(tm_year,tm_mon,tm_mday.............,tm_isdst)

Attributes of struct_time()

  1. tm_year- year
  2. tm_month-month
  3. tm_mday- day of the month
  4. tm_hour-hour
  5. tm_min-minute
  6. tm_sec-second
  7. tm_wday-day of the week, range[0,6], 0 is Monday
  8. tm_yday-day of the year, range[0,366]
  9. tm_isdst-tm_isdst may be set to 1 when daylight savings time is in effect, and 0 when it is not and -1 for unknown.

There are 2 other attributes also- tm_zone- abbbreviation of timezone name and tm_gmtoff- offset east off UTC in seconds.

time.localtime()

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:

    code snippet how localtime works
    

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.

time.gmtime()

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:

code snippet how gmtime works

time.mktime()

The mktime() function takes struct_time as an input and it will return the no. of seconds passed since epoch in local time.

Syntax:
t_local=(tm_year,tm_month,.......,tm_isdst)
local_time=time.mktime(t_local)
Example:
 code snippet of mktime

Note:mktime() is the inverse of localtime().

time.strftime()

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")
  1. %m:month(1,2...,12)
  2. %d:day(1,2...,31)
  3. %Y:Year(0001,....2019,2020,....9999)
  4. %H:Hour(00,01,02...,24)
  5. %M:minute(00,01,02...,59)
  6. %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:

code snippet of strftime

As you can see in the above code I changed day with month and vice-versa and it works very well.

time.asctime()

The asctime() takes struct_time() as an argument and returns a string representing it.

Example:

code snippet of asctime

You can see in the above code that how we can implement time.asctime() function to get the information.