Utilizing Dates and Times in Python

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

Python is able to work with time quite efficiently, as long as the coder is aware of a few functions.

In the `datetime.datetime` package:
– `strptime` is used to parse a text string representing a datetime object into a datetime object.
– `strftime` is used to turn a datetime object into a string representing a datetime object.
– `timedelta` can be used to add to or delete a certain amount of time from a datetime object.

You will need basic Python programming and datetime skills for this lab:
– [Certified Associate in Python Programming Certification](https://linuxacademy.com/cp/modules/view/id/470)
– [Python’s datetime](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-format-codes)

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Convert All Due Date Strings to datetime Objects

Using the SQLite applications you have already developed, you pull all the datetimes and book titles from the database. SQLite has no datetime date type, and so we are storing them as strings. These strings need to be turned into actual datetime objects.

Update the code so that the datetime string is converted to a datetime object, and add the last minute of the day to it:

from datetime import datetime, timedelta

# due date data from db
title_due_dates = [
    ["Oh Python! My Python!", "2020-11-15"],
    ["Fun with Django", "2020-06-23"],
    ["When Bees Attack! The Horror!", "2020-12-10"],
    ["Martin Buber's Philosophies", "2020-07-12"],
    ["The Sun Also Orbits", "2020-10-31"]

# replace the string date with a date object for each title
# add the last minute of day to each due date
# add a timedelta representing the last minute of the day
for book in title_due_dates:
    due_date = datetime.strptime(book[1], "%Y-%m-%d")
    due_date = due_date + timedelta(hours=23, minutes=59)
    book[1] = due_date

Congratulations! You have shown that you can convert strings to datetime objects.

Turn All Resulting datetime Objects Into a String

Before we can use our SQLite applications to upload the new due dates, we need to convert them to string representation.

Run python add_time.py. This will result in an AssertionError.

Now update add_time.py so that the datetime object is converted back to a string:

# previous code omitted

# replace the datetime object with a string representation
# remember SQLite stores dates as strings
for book in title_due_dates:
    book[1] = book[1].strftime("%Y-%m-%d %H:%M:%S")

Run python add_time.py.

Congratulations! You have shown that you can convert datetime objects to strings.

Additional Resources

Atlantic Publishing is negotiating new due dates with its authors. The original due date was set as just a date. At the advice of the legal team, the due date needs to be set with a definite time. Further author percentages are affected by the actual date submitted against the due date.

You have been asked to write an application that will take the strings representing the due date from the database, and add the time component of midnight to them.

Logging In

There are a couple of ways to get in and work with the code. One is to use the credentials provided in the hands-on lab page, log in with SSH, and use a text editor in the terminal.

The other is using VS Code in a web browser. If you'd like to go this route, then you will need to navigate to the public IP address of the workstation server (provided in the hands-on lab page) on port 8080 (example: http://PUBLIC_IP:8080). Your password will be the same password that you'd use to connect over SSH.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?