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, datetime_format):
    """
    Formats a Unix timestamp into a readable datetime string.

    :param timestamp: int, the Unix timestamp
    :param datetime_format: str, the desired date format
    :return: str, the formatted date
    """
    dt = datetime.fromtimestamp(timestamp)
    return dt.strftime(datetime_format)

# Test the function
print(format_date(1514665153, "%d-%m-%Y"))  # Output: "30-12-2017"
def calculate_landing_time(rocket_launch_dt, travel_duration):
    """
    Calculates the estimated landing time.

    :param rocket_launch_dt: datetime object, the rocket launch datetime
    :param travel_duration: int, the expected travel time in days
    :return: str, the estimated landing time in the format DD-MM-YYYY
    """
    landing_dt = rocket_launch_dt + timedelta(days=travel_duration)
    return landing_dt.strftime("%d-%m-%Y")

# Test the function
print(calculate_landing_time(datetime(2023, 2, 15), 20))  # Output: "07-03-2023"
def days_until_delivery(expected_delivery_dt, current_dt):
    """
    Calculates the days until a package arrives for customers.

    :param expected_delivery_dt: datetime object, the estimated delivery date
    :param current_dt: datetime object, the current date
    :return: int, the number of days until delivery
    """
    delta = expected_delivery_dt - current_dt
    return delta.days

# Test the function
print(days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5)))  # Output: 10