Skip to content

You recently quit your job to start a space logistics company that uses rockets to deliver critical cargo to colonies on demand. Since you're still in the startup phase, you're handling everything yourself, including writing the software to manage complex scheduling and timing across different space colonies.

Before developing a full rocket flight planning and logistics system, you want to create core functions using Python's datetime module to handle dates, times, and durations. These basic functions are essential for your rocket delivery service. In this project, you will make simple reusable functions for working with timestamps, calculating rocket landing times based on launch and travel duration, and figuring out days until a delivery deadline to keep those customers updated!

This project is data-less, but you can test your functions by calling them in the workspace and passing them the required variables.

# Re-run this cell
from datetime import datetime, timedelta
# Start coding here. Use as many cells as you need.

def format_date(timestamp: int, datetime_format: str):
    """
    Args:
        timestamp (int): Unix timestamp integer
        datetime_format (str): string of the desired date format
    Returns:
        str: returns the formatted timestamp into a readable datetime string
    """
    # convert the timestamp into a datetime object
    dt = datetime.fromtimestamp(timestamp)
    # format the datetime object into a formatted datetime string
    formatted_date = dt.strftime(datetime_format)
    # return formatted datetime string 
    return formatted_date
def calculate_landing_time(rocket_launch_dt: datetime, travel_duration: int):
    """
    Args:
        rocket_launch_dt (datetime): the rocket launch datetime object
        travel_duration (int): the expected travel time in days as integer
    Returns:
        str: returns the estimated landing time on Mars as a formatted datetime string
    """
    # Calculates the land_date by adding travel_duration to rocket_launch_dt using the timedelta function
    land_date = rocket_launch_dt + timedelta(days = travel_duration)
    # format the land_date datetime object into a formatted datetime string
    formatted_land_date = land_date.strftime("%d-%m-%Y")
    # return the land date in formatted datetime string
    return formatted_land_date
def days_until_delivery(expected_delivery_dt: datetime, current_dt: datetime):
    """
    Args:
        expected_delivery_dt (datetime): estimated delivery date
        current_dt (datetime): current date
    Returns:
        int: difference in days between expected_delivery_dt and current_dt 
    """
    # Calculates for the expected days until delivery by currrent_dt from subtracing expected_delivery_dt 
    dud = expected_delivery_dt - current_dt
    # Get the day component from the days until delivery date
    format_dud = dud.days
    # return the days until delivery 
    return format_dud