Python Tutorial, Release 3.9.5 Chapter 10. Brief Tour of the Standard Library 82 The datetime module supplies classes for manipulating dates and times in both simple and complex ways. While date and time arithmetic is supported, the focus of the implementation is on efficient member extraction for output formatting and manipulation. The module also supports objects that are timezone aware. >>> import zlib >>> s =b'witch which has which witches wrist watch ' >>> len(s) 41 >>> t =zlib.compress(s) >>> len(t) 37 >>> zlib.decompress(t) b'witch which has which witches wrist watch' >>> zlib.crc32(s) 226805979 Common data archiving and compression formats are directly supported by modules including:$_{zlib}$,$_{gzip}$ ,$_{bz2}$, $_{lzma}$,$_{zipfile}$ and$_{tarfile}$. >>> # dates are easily constructed and formatted >>> from datetime import date >>> now =date.today() >>> now datetime.date(2003, 12, 2) >>> now.strftime("%m- %d -%y. %d %b %Y is a %A on the %d day of %B.") '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.' >>> # dates support calendar arithmetic >>> birthday =date(1964 ,7,31 ) >>> age =now -birthday >>> age.days 14368 >>> from timeit import Timer >>> Timer('t=a; a=b; b=t ','a=1; b=2 ').timeit() 0.57535828626024577 >>> Timer('a,b = b,a ','a=1; b=2 ').timeit() 0.54962537085770791 In contrast to$_{timeit}$’s fine level of granularity, the$_{profile}$and$_{pstats}$modules provide tools for identifying time critical sections in larger blocks of code. Some Python users develop a deep interest in knowing the relative performance of different approaches to the same problem. Python provides a measurement tool that answers those questions immediately. For example, it may be tempting to use the tuple packing and unpacking feature instead of the traditional approach to swapping arguments. The$_{timeit}$ module quickly demonstrates a modest performance advantage: 10.10 Performance Measurement 10.9 Data Compression 10.8 Dates and Times