Date & Time Modules in Python


The time and calendar Modules


These are two important modules provided by Python for referring to the date and time.
Python refers to time in terms of (clock) ticks or seconds since epoch time (0:00, 1 Jan, 1970).
#!/usr/bin/python3
import time;

ticks = time.time()
print ("Number of ticks since 12:00am, January 1, 1970:", ticks)

localtime = time.localtime(ticks)
print ("Local current time :", localtime)

print ("Local current time :", time.asctime( localtime ))
This creates the output
Number of ticks since 12:00am, January 1, 1970: 1531996804.3854601
Local current time : time.struct_time(tm_year=2018, tm_mon=7, tm_mday=19, tm_hour=10, tm_min=40, tm_sec=4, tm_wday=3, tm_yday=200, tm_isdst=0)
Local current time : Thu Jul 19 10:40:04 2018
The time.time() returns the number of ticks. time.localtime(ticks) returns the time tuple and time.asctime() formats this into a readable string.

Time Tuple

The value returned by the time.localtime(ticks) method is called the time tuple. It contains the below data. Note that some fields start from 0 and some from 1.
IndexAttributesValues
0tm_year2016
1tm_mon1 to 12
2tm_mday1 to 31
3tm_hour0 to 23
4tm_min0 to 59
5tm_sec0 to 61 (60 or 61 are leap-seconds)
6tm_wday0 to 6 (0 is Monday)
7tm_yday1 to 366 (Julian day)
8tm_isdst-1, 0, 1, -1 means library determines DST

The time module provides for many other features:
NoFunctionDescription
1time.altzoneThe offset of the local DST timezone, in seconds west of UTC, if one is defined. This is negative if the local DST timezone is east of UTC (as in Western Europe, including the UK). Use this if the daylight is nonzero.
2time.asctime([tupletime])This method accepts a time-tuple and returns a readable 24-character string such as 'Tue Dec 11 18:07:14 2008'.
3time.clock()This is used to get the current CPU time as a floating-point number of seconds. time.clock is used to measure computational costs of code.
4time.ctime([secs])Similar to asctime(localtime(secs))
5time.gmtime([secs])Accepts an instant expressed in seconds since the epoch and returns a time-tuple t as per the UTC time. Note − t.tm_isdst is always 0
6time.localtime([secs])This method accepts an instant in seconds since the epoch and returns a time-tuple t with the local time (t.tm_isdst is set to 0 or 1, depending on whether DST applies to instant secs by local rules).
7time.mktime(tupletime)This converts an instant expressed as a time-tuple in local time into a floating-point value with the instant expressed in seconds since the epoch.
8time.strftime(fmt[,tupletime])Accepts an instant expressed as a time-tuple in local time and returns a string representing the instant as specified by string fmt.
8time.strptime(str,fmt = '%a %b %d %H:%M:%S %Y')Parses str according to format string fmt and returns the instant in time-tuple format.
10time.time()Returns the current time instant, a floating-point number of seconds since the epoch.
11time.tzset()Resets the time conversion rules used by the library routines. The environment variable TZ specifies how this is done.
12time.sleep(secs)This is different from the rest. It suspends the calling thread for secs seconds.

The time module helps us get the current time. The calendar module let's us understand the given time.
NoFunctionDescription
1calendar.calendar(year,w = 2,l = 1,c = 6)It returns a multiline string with a calendar for the year formatted into three columns separated by c spaces. w is the width in characters of each date; each line has length 21*w+18+2*c. l is the number of lines for each week.
2calendar.firstweekday( )This is used to get the current setting for the weekday that starts each week. By default, when calendar is first imported, this is 0, meaning Monday.
3calendar.isleap(year)True or False.
4calendar.leapdays(y1,y2)It returns the total number of leap days in the years within range(y1,y2).
5calendar.month(year,month,w = 2,l = 1)Returns a multiline string with a calendar for month month of year year, one line per week plus two header lines. w is the width in characters of each date; each line has length 7*w+6. l is the number of lines for each week.
6calendar.monthcalendar(year,month)Returns a list of lists of ints. Each sublist denotes a week. Days outside month month of year year are set to 0; days within the month are set to their day-of-month, 1 and up.
7calendar.monthrange(year,month)It returns two integers. The first one is the code of the weekday for the first day of the month month in year year; the second one is the number of days in the month. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 to 12.
8calendar.prcal(year,w = 2,l = 1,c = 6)Similar to print calendar.calendar(year,w,l,c).
9calendar.prmonth(year,month,w = 2,l = 1)Similar to print calendar.month(year,month,w,l).
10calendar.setfirstweekday(weekday)It sets the first day of each week to weekday code weekday. Weekday codes are 0 (Monday) to 6 (Sunday).
11calendar.timegm(tupletime)The inverse of time.gmtime: accepts a time instant in time-tuple form and returns the same instant as a floating-point number of seconds since the epoch.
12calendar.weekday(year,month,day)This returns the weekday code for the given date. Weekday codes are 0 (Monday) to 6 (Sunday); month numbers are 1 (January) to 12 (December).

Python also provides for few other useful modules like datetime and dateutil for handling date and time.