Introductory Programming in Python: Lesson 22
Handling Dates and Times

[Prev: Operating System Functionality] [Course Outline] [Next: Writing Meaningful Error Messages]

The Unix Epoch

Dates and times have been traditionally tricky for computers to handle, because of the complexities of the Gregorian calendar, let alone taking into account the existence of non-western cultural calendars. The solution was to find something standard, common to every calendar, and easy to manipulate. The second was chosen as the standard unit of time in programming, and in the UNIX world a fixed standard time was named, the UNIX epoch, being 00:00am 1st January, 1970. (The year UNIX was first released). Other operating systems use a different epoch. The reason for this, is that practically every application of arithmetic to times or dates involves breaking down the date or time in question to a value in seconds, and then converting it back. Both conversion to and from seconds, and finally into a nice human readable representation are common practice in programming, tedious, and thus already solved by someone else in the form of the time module.

Using the time Module

Whilst there are many functions available in the time modules, three are of primary importance, ignoring the complexities of locales and timezones. Obviously the first thing we need to do is to import the module...

Python 2.4.3 (#1, Oct	2 2006, 21:50:13) 
[GCC 3.4.6 (Gentoo 3.4.6-r1, ssp-3.4.5-1.0, pie-8.7.9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>>

Exercises

[Prev: Operating System Functionality] [Course Outline] [Next: Writing Meaningful Error Messages]