Dates and times with Python

This entry is part 14 of 14 in the series Python Tutorial

Sometimes we have to work with dates and times. I use dates less than times because with times you can measure code execution time which is sometimes essential (for my website scraping book’s performance measurement is the core part I guess).

So let’s jump right ahead into the date-time modules and see what we can achieve with the usage of that.

Ticks

Time units in computer science are mostly starting from the so called epoch: 00:00, 01. January 1970.

The popular time module of Python counts the time since this epoch in ticks.

The result of the example will differently vary on your computer because time ticks away…

The good thing with the ticks is that you can subtract them. With this you can measure code execution.

The result above is in seconds what is nice. So it is a good option to use time.time() for runtime measurement.

Time tuples

Python’s dates are represented with time tuples: 9 numbers in a tuple. The problem with time-tuples is that you have to enter all the values to have a valid tuple. This can lead to errors.

Formatting time

To format the time you have to use some built-in methods. I will use time.asctime() which requires a time-tuple to provide. If no tuple is provided then the actual system time is used.

The problem with the second example above is that the 9th July 2016 will be a Saturday not a Monday. However we have had to provide the “Day of Week” field where I out of laziness added 0 for Monday. I think you have arrived to the same conclusion: there should be another way around…

Datetime

The datetime module comes handy in the previous case because it can handle dates without requiring all the tuple parameters asctime required. The built-in function isoformat returns a string representing the ISO format of the date.

Seems a bit awkward with the ‘T’ denoting the delimiter between date and time at first but this is just a default convenience method. If you use the str function you get the ‘T’ removed so it is more nicely in your displaying. If you use print then str is called implicitly.

Now this is good but can we separate date and time? Of course, it is Python, you can do everything with this language (naturally some problems require more coding effort).

Advanced datetime Formatting

We have learned about string formatting and I gave you some methods how to format strings. We have seen in this section already that datetime object format nicely when printing them. So in this case we can combine our knowledge and have formatted dates within formatted strings.

Because datetime objects are objects and they are some special kind which often requires formatting, they have the built-in function strftime which requires a format string as parameter and returns the datetime object according to this format. Naturally this format string can contain some more text to include around the datetime object.

As you can see, strftime uses some placeholders you already know from string formatting, however there are quite some formatters to list them here. If you want to see them all, take a look at the docs.

If you try to use the % string formatting along with datetime objects you will soon see that it does not work out. That’s because for the % formatting you need more parameters and the format-string placeholders are overlapping so you would encounter an error.

You can use the format function on strings with a datetime as parameter, however in this case you will get the representation of the str function — which is not very appropriate in most of the cases.

That’s why I suggest you to use strftime to get a formatted string with a datetime object. If you have multiple datetime objects to represent in a single string use string concatenation.

Calendars

The calendar module supplies functions related to calendars. For example you can ask if a year is a leap-year or just print the whole calendar for a month or even a year. So I would say the calendar module is loaded with utility functions.

Let’s go straight into the examples and have a calendar printed to our console.

As you can see the calendar module takes Monday as the first day of the week, however you can change it easily. To any day of the week you want to.

As you can see (I hope the formatting of the code block stays the same as I see it now) the weekdays start from 0 (Monday) to 6 (Sunday).

And when you change the first week you change it for the whole module. This means from now on (until the interpreter is re-started) the first day of week will be Thursday. Even a re-import of the calendar module does not help to resolve this problem. So if you change the first day of week for some purpose do not forget to change it back or you can end up with some surprises.

Printing calendars made easy

Well, you can print a calendar on-the-fly too without requiring intermediate objects. The calendar module has the built-in functions prmonth and prcal which prints the month or the calendar directly to the console. The arguments are the same than for month and calendar functions.

Series Navigation<< Object-Oriented Programming (OOP)

Leave A Comment

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