How to list & parse 'atq' jobs using Python

#!/usr/bin/env python3
from datetime import datetime
from invoke import run
 
def get_atq_jobs():
    """
    Retrieves the list of scheduled jobs from the at queue.

    This function runs the `atq` command to get the list of scheduled jobs,
    parses the output, and returns a list of datetime objects representing
    the scheduled times of the jobs.

    Returns:
        list: A list of datetime objects representing the scheduled times of the jobs.
    """
    result = run("atq", hide=True)
    jobs = []
    for line in result.stdout.splitlines():
        parts = line.split()
        job_id = parts[0]
        job_datetime_str = " ".join(parts[1:6])
        job_datetime = datetime.strptime(job_datetime_str, "%a %b %d %H:%M:%S %Y")
        jobs.append(job_datetime)
    return jobs

Example

atq output:

36      Tue Nov 26 12:45:01 2024 a user
35      Tue Nov 26 12:41:05 2024 a user

returns

[datetime.datetime(2024, 11, 26, 12, 45, 1),
 datetime.datetime(2024, 11, 26, 12, 41, 5)]