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.

1 - Create a function that formats Unix timestamps into readable dates

# Define the Function
def format_date(timestamp, datetime_format):
    
    # Convert and Format Timestamp to string Datetime
    dt = datetime.fromtimestamp(timestamp)
    str_dt = dt.strftime(datetime_format)
    
    return str_dt
# Testing format_date function
format_date(1514665153, '%d-%m-%Y')

2 - Create a function that calculates the rockets landing date

# Define the function
def calculate_landing_time(rocket_launch_dt, travel_duration):
    
    # Calculate the travel duration
    landing_date = rocket_launch_dt + timedelta(days=travel_duration)
    
    # Format the date string
    landing_date_string = landing_date.strftime('%d-%m-%Y')
    
    return landing_date_string
# Testing calculate_landing_time function
calculate_landing_time(datetime(2023, 2, 15), 20)

3 - Create a function that calculates the number of days left until the package delivery

# Define the function
def days_until_delivery(expected_delivery_dt, current_dt):
    
    # Calculate the time delta
    time_del = expected_delivery_dt - current_dt
    
    # Calculate the number of days left until the delivery date
    days_until = time_del.days
    
    return days_until
# Testing days_until_delivery function
days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5))