Fatskills
Practice. Master. Repeat.
Study Guide: Python Programming: Mastering Date and Time
Source: https://www.fatskills.com/python/chapter/python-programming-mastering-date-and-time

Python Programming: Mastering Date and Time

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~7 min read

In Python programming language, date and time can be handled in the following ways.

Tick
Tick in Python is the instance if time measured in seconds since January 1, 1970 12:00. Python has time module which has functions to work with time. The function time.time () returns the current system time in ticks since 12:00am, January 1, 1970(epoch) as shown in the below example.

TimeTuple
In Python language, many time functions handle time as a tuple of 9 numbers as indicated in the below table. This tuple is equivalent to struct_time structure shown in the attribute column.

Index

Field

Values

Attributes

0

4-digit year

2008

tm_year

1

Month

1 to 12

tm_mon

2

Day

1 to 31

tm_mday

3

Hour

0 to 23

tm_hour

4

Minute

0 to 59

tm_min

5

Second

0 to 61 (60 or 61 are leap-seconds)

tm_sec

6

Day of Week

0 to 6 (0 is Monday)

tm_wday

7

Day of year

1 to 366 (Julian day)

tm_yday

8

Daylight savings

(-1, 0, 1, -1) means library determines DST

tm_isdst


Python code example for TimeTuple
Getting current local time: To get the current local time in TimeTuple format use the function as time.localtime(time.time()), this function will translate the tick seconds into struct_time structure as a tuple of 9 numbers as discussed above.


Formatting current local time: To get the current local readable format use the function as time.asctime () as shown in the below example.


Print calendar for a month: Python has a calendar module which gives a wide range of methods to work with monthly and yearly calendars. In the below example, we are going to print a calendar for a Feb 2016 month (leap year) using the function calendar.month (year, month).

Summary of functions in Time Module
Below are the functions available in Calendar module.

 

Time Functions

Description

time.altzone                              

This is to be used only when Only use this if daylight is nonzero. It is positive if the offset of the local DST time zone is west of UTC.

This is negative if the local DST time zone is east of UTC (as in Western Europe, including the UK). All in seconds.

time.asctime([tupletime])                 

This function of time module accepts a time-tuple and returns a readable 24-character string such as 'Tue Apr 26 19:09:19 2016'.

time.clock( )                             

This function of time module returns the current CPU time as a floating-point number of seconds.

time.ctime([seconds])                        

This function of time module is just like function asctime (localtime (seconds)) and without arguments is like asctime( ).

time.gmtime([seconds])                       

This function of time module accepts an instant expressed in seconds since the epoch and returns a time-tuple xyz with the UTC time. It is to be noted that zyz.tm_isdst is always 0.

time.localtime([seconds])                    

This function of time module accepts an instant expressed in seconds since the epoch and returns a time-tuple t with the local time (t.tm_isdst is 0 or 1, depending on whether DST applies to instant secs by local rules).

time.mktime(tupletime)                    

This function of time module accepts an instant expressed as a time-tuple in local time. It returns a floating-point value with the instant expressed in seconds since the epoch.

time.sleep(secs)                          

This function of time module suspends the calling thread for secs seconds.

time.strftime(fmt[,tupletime])            

This function of time module accepts an instant expressed as a time-tuple in local time. It returns a string representing the instant as specified by string fmt.

time.strptime(strg,format='%a %b %d %H:%M:%S %Y')

This function of time module parses strg according to format string format. It returns the instant in time-tuple format.

time.time( )                              

As discussed in above examples, this function of time module returns the current time instant, a floating-point number of seconds since the epoch.

time.tzset()                              

This function of time module resets the time conversion rules used by the library routines.

 


Summary of functions in Calendar Module
Below are the functions available in Calendar module.

 

 

Calendar Functions

Description

calendar.calendar(year, width=2,line=1,space=6) 

This function of calendar module returns a multiline string with a calendar for year year formatted into three columns separated by space spaces, width is the width in characters of each date; each line has length 21*width+18+2*c. line is the number of lines for each week.

calendar.firstweekday( )          

This function of calendar module returns the current setting for the weekday that starts each week. Default value is 0, which is Monday.

calendar.isleap(year)             

This function of calendar module returns True if it is a leap year; otherwise, False.

calendar.leapdays(ye1,ye2)          

This function of calendar module returns the total number of leap days in the years within range (ye1, ye2).

calendar.month(year, month, width=2,line=1)

This function of calendar module returns a multiline string with a calendar for month month of year year, one line per week plus two header lines. width is the width in characters of each date; each line has length 7*w+6. line is the number of lines for each week.

calendar.monthcalendar(year, month)

This function of calendar module returns a list of lists of ints. Each such 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.

calendar.monthrange(year, month)   

This function of calendar module 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. Month numbers are 1 (January) to 12 (December). Weekday codes are 0 (Monday) to 6 (Sunday).

calendar.prcal(year,w=2,l=1,c=6)  

This function of calendar module works like print calendar.calendar (year,w,l,c).

calendar.prmonth(year,month,w=2,l=1)

This function of calendar module works like print calendar.month (year,month,w,l).

calendar.setfirstweekday(weekday) 

This function of calendar module sets the first day of each week to weekday code weekday. Month numbers are 1 (January) to 12 (December). Weekday codes are 0 (Monday) to 6 (Sunday).

calendar.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.

calendar.weekday(year, month, day)  

This function of calendar module returns the weekday code for the given date. Month numbers are 1 (January) to 12 (December). Weekday codes are 0 (Monday) to 6 (Sunday).