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
# Format a timestamp into a readable datetime string
def format_date(timestamp, datetime_format):
    """
    Formats a timestamp into a readable datetime string.

    Args:
        timestamp: Unix timestamp integer
        datetime_format: string specifying the desired date format

    Returns:
      The date correctly formatted as a string.
    """
    timestamp = datetime.fromtimestamp(timestamp)
    return timestamp.strftime(datetime_format)

print(format_date(1514665153, "%d-%m-%Y"))

# Calculate the estimated landing time
def calculate_landing_time(rocket_launch_dt, travel_duration):
    """
    Adds travel time in days to a date in 'YYYY-mm-dd' format and returns the resulting date in 'dd-mm-YYYY' format.

    Args:
        rocket_launch_dt: The date string in 'YYYY-mm-dd' format.
        travel_duration: The number of days to add to the date.

    Returns:
        The resulting date string in 'dd-mm-YYYY' format after adding travel days.
    """
    landing_date = rocket_launch_dt + timedelta(days=travel_duration)
    return landing_date.strftime('%d-%m-%Y')

print(calculate_landing_time(datetime(2023, 2, 15), 20))

# Calculate the days until a package arrives for customers
def days_until_delivery(expected_delivery_dt, current_dt):
    """
    Calculates the days until a package arrives for customers.

    Args:
        expected_delivery_dt: The estimated delivery date as a datetime object for the package.
        current_dt: The current date as a datetime object.

    Returns:
        The number of days before the package arrives to the customer as an integer.
    """
    delivery_days = expected_delivery_dt - current_dt
    return delivery_days.days

print(days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5)))