Featured resource
2025 Tech Upskilling Playbook
Tech Upskilling Playbook

Build future-ready tech teams and hit key business milestones with seven proven plays from industry leaders.

Check it out
  • Lab
    • Libraries: If you want this lab, consider one of these libraries.
    • Cloud
Google Cloud Platform icon
Labs

Working with Time Zones in Python

The due dates stored in the database are naive datetimes. That means they do not have a time zone associated with them. So while we can see a date and time, the datetime object is unaware of *when* that datetime is. What time zone at almost midnight are we talking about? Standard practice is to store all due dates in UTC format, then change them to the relevant time zone as needed. **pytz** is a 3rd-party application that helps with time zones, including daylight savings. From their documentation: *pytz brings the Olson tz database into Python. This library allows accurate and cross platform timezone calculations using Python 2.4 or higher. It also solves the issue of ambiguous times at the end of daylight saving time, which you can read more about in the Python Library Reference (`datetime.tzinfo`).* 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) - [pytz](http://pytz.sourceforge.net/)

Google Cloud Platform icon
Lab platform
Lab Info
Level
Beginner
Last updated
Aug 24, 2025
Duration
45m

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
Table of Contents
  1. Challenge

    Make Due Dates Timezone Aware

    Using the SQLite applications you have already developed you pull all the datetimes and book titles from the db.

    Run python timezones.py. This will result in an AssertionError. There are two sections that must be fixed to pass the assertion.

    In this first section, you must make the due dates aware datetime objects by adding time zone information to the datetime object. The time zone added should the time zone indicated for a book title.

    You create a time zone object with pytz.

    my_timezone = pytz.timezone(<time zone name>)
    

    Then make the due date aware of that time zone. This is done by replacing the None time zone with the real time zone. This does not change the due date, just makes it aware of its time zone:

    aware_due_date = due_date.replace(my_timezone)
    

    We need to install pytz, so run pip3 install pytz. Now make those times aware of their time zone:

    import pytz
    from datetime import datetime
    
    # due date data from db
    title_due_dates = [
        ['Oh Python! My Python!', '2020-11-15 23:59:59'], 
        ['Fun with Django', '2020-06-23 23:59:59'], 
        ['When Bees Attack! The Horror!', '2020-12-10 23:59:59'], 
        ["Martin Buber's Philosophies", '2020-07-12 23:59:59'], 
        ['The Sun Also Orbits', '2020-10-31 23:59:59']
    ]
    
    # dictionary matching the timezone value to book title
    title_time_zones = {
        'Oh Python! My Python!': 'US/Central', 
        'Fun with Django': 'US/Pacific', 
        'When Bees Attack! The Horror!': 'Europe/London', 
        "Martin Buber's Philosophies": 'Australia/Melbourne', 
        'The Sun Also Orbits': 'Europe/Paris'
    }
    
    # when the db due_date string is converted to a datetime object it is naive
    # it does not contain timezone info
    # make the due_date timezone aware with `<due_date>.replace(timezone)`
    # make the timezone the same as indicated in `title_time_zones`
    # this makes the book due just before midnight local time
    aware_title_due_dates = {}
    for book in title_due_dates:
        book_timezone = pytz.timezone(title_time_zones[book[0]])
        naive_date_due = datetime.strptime(book[1], "%Y-%m-%d %H:%M:%S")
        aware_date_due = naive_date_due.replace(tzinfo=book_timezone)
        aware_title_due_dates[book[0]] =  aware_date_due
    
    # remaining code omitted-used in step 2.
    
    
  2. Challenge

    Turn All Due Date to UTC

    We now have a dictionary of aware datetimes by book author timezone. These aware datetimes are set for essentially midnight in the author's timezone.

    When storing due dates, it is a standard to store the datetime in UTC and convert to the user's timezone on the fly. So now we need to change the timezone on the author's due date to UTC and make it a text string for storage in the database.

    # code prior to this omitted
    
    # aware_title_due_dates has the due date in the author's timezone
    # following good db practice we will store the dates as UTC and
    # only convert when necessary to time zone needed
    # update `title_due_dates` with the due_date in UTC time
    for book in title_due_dates:
        utc_due_date = aware_title_due_dates[book[0]].astimezone(pytz.utc)
        utc_due_date_string = utc_due_date.strftime("%Y-%m-%d %H:%M:%S %Z%z")
        book[1] = utc_due_date_string
    
    # we can now use aware_title_due_dates for updating the db
    
    

    Run python timezones.py.

    Congrats! You have shown basic understanding of datetimes and timezones. This skill will be necessary if you work for a compant that has an application used worldwide.

About the author

Pluralsight Skills gives leaders confidence they have the skills needed to execute technology strategy. Technology teams can benchmark expertise across roles, speed up release cycles and build reliable, secure products. By leveraging our expert content, skill assessments and one-of-a-kind analytics, keep up with the pace of change, put the right people on the right projects and boost productivity. It's the most effective path to developing tech skills at scale.

Real skill practice before real-world application

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.

Learn by doing

Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.

Follow your guide

All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.

Turn time into mastery

On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.

Get started with Pluralsight