Python Time



A Tutorial on using the Python Time library.

There are two Python Libraries that deal with time and dates. The first being time and the second datetime. While their uses overlap in some areas, they have fundamental differences in how they work as while as some unique functions. We won’t be covering some of the overlapping material instead focusing on what makes the two libraries different.


time.sleep

Before we go any further, we’ll just demonstrate the use of this function as it’s the most common reason people use this function. (On a basic level anyway)

The time.sleep() function will pause the current thread from executing for a specified number of seconds.

import time

print("Testing")
time.sleep(3)
print("Testing")

If you run this code you’ll find that the second print statement will be displayed exactly 3 seconds after the first.

Warning! The content after this may not be very suitable for beginners. If you’re interested in using dates and time in your software in the more conventional sense, I advise you head over to our Datetime library instead.


Understanding Time

For Unix systems, the start of time is 1st January 00:00:00 1970. This point is referred to as the epoch and serves as the base (or point of reference) for the time function.

Many of the functions in the Python Time library utilize a class called time.struct_time. Below is a break down of an object of this class that is created.

time.struct_time(   tm_year=2020, tm_mon=3, tm_mday=3, 
                    tm_hour=8, tm_min=35, tm_sec= 0, 
                    tm_wday=3, tm_yday=92, tm_isdst=0)
AttributeNameRange
tm_yearYear N/A
tm_monMonth1,2,3……12
tm_mdayDay (w.r.t Month)1,2,3……31
tm_hourHour0,1,2……23
tm_minMinute0,1,2……59
tm_secSecond0,1,2……59
tm_wdayDay (w.r.t Week)0,1,2……6
tm_ydayDay (w.r.t Year)1,2,3……366
tm_isdstDaylight Savings0 or 1 or -1

time.time()

The time() function returns the number of seconds passed since the epoch.

import time

seconds = time.time()
print(seconds)

time.ctime()

The ctime() function takes as input, number of seconds passed. It then returns a string with the local time. In simpler words, it takes number of seconds as input (10000 for instance) and with reference to the epoch calculates the current time.

import time

result1 = time.ctime(100000)
result2 = time.ctime(50000000)
result3 = time.ctime(800000000)

print(result1)
print(result2)
print(result3)
# OUTPUT
Fri Jan  2 08:46:40 1970
Mon Aug  2 21:53:20 1971
Tue May  9 11:13:20 1995

time.localtime()

The localtime() function takes as input, number of seconds passed since epoch and returns a time.struct_time object. You can access individual elements in the object by using the following code.

import time

result = time.localtime(1583214126.0)

print(result)
print("Year: " , result.tm_year)
print("Month: " , result.tm_mon)
print("Day: ",result.tm_mday)
print("Hour: " ,result.tm_hour)
# OUTPUT
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=3,
                 tm_hour=10, tm_min=42, tm_sec=6,
                 tm_wday=1, tm_yday=63, tm_isdst=0)
Year:  2020
Month:  3
Day:  3
Hour:  10

time.mktime()

The mktime() function takes as argument a tuple containing the 9 values found in a time.struct_time object. It then returns the number of seconds that have passed between the epoch and the input time. It may be thought of as the reverse of the time.localtime() function,

import time

tuple = (2020, 3, 3, 10, 42, 6, 3, 103, 0)
time = time.mktime(tuple)
print(time)

time.gmtime()

The gmtime() function takes as input, the number of seconds passed since Epoch, and returns a time.struct_time object.

import time

result = time.gmtime(1048596)
print(result)
# OUTPUT
time.struct_time(tm_year=1970, tm_mon=1, tm_mday=13, 
                 tm_hour=3, tm_min=16, tm_sec=36,
                 tm_wday=1, tm_yday=13, tm_isdst=0)

time.asctime()

The asctime() function a time.struct_time object and returns a string displaying it’s contents in a more readable manner. (You may use a tuple with the 9 required values in place of the time.struct_time object.

import time

newtuple = (2020, 2, 21, 3, 41, 2, 2, 302, 0)
result = time.asctime(newtuple)
print(result)
# Output
Wed Feb 21 03:41:02 2020

What are Day Light Savings?

If you’re familiar with how time zones, seasons and such work, you’ll know that during some parts of the year, the day’s become longer. In order to account for these changes, there is a slight change made to the time zones. Usually an hour’s difference. So if the difference between two time zones were 12 hours, during certain parts of the year, it may become 13.


This marks the end of the Python Time Article. Any suggestions or contributions for CodersLegacy are more than welcome. Any questions can be directed to the comments section below.

Leave a Reply

Your email address will not be published. Required fields are marked *