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.

# Import packages
from datetime import datetime, timedelta
# Function
def format_date(timestamp: int,datetime_format:str="%Y-%m-%d")-> int:
    """Formats a datetime object as a string
    
    Args:
        timestamp (int): Unix timestamp integer
        datetime_format (str): String representing a desired date format
    
    Returns:
        date_string (str): readable datetime string"""
    
    
    d =  datetime.fromtimestamp(timestamp)
    date_string = d.strftime(datetime_format)
    return date_string
# Function
def calculate_landing_time(rocket_launch_dt:datetime, travel_duration:int)->int:
    """Calculates the estimated landing time
    
    Args:
        rocket_launch_dt (datetime obj): datetime
        travel_duration (int): Expected travel time in days
    
    Returns:
        landing_time (str): estimated landing time on Mars"""
    # Create a timdelta based on the duration of the travel
    td = timedelta(days = travel_duration)
    
    land_datetime = rocket_launch_dt + td
    landing_time = land_datetime.strftime('%d-%m-%Y')
    
    return landing_time  
    
# Function
def days_until_delivery(expected_delivery_dt: datetime, current_dt: datetime) -> int:
    
    """Days until a package arrives for customers

        Args:
            expected_delivery_dt (datetime obj): date of delivery in datetime
            current_dt (datetime obj): current date in datetime

        Returns:
            landing_time (str): estimated landing time on Mars"""
    
    td = expected_delivery_dt - current_dt
    td = td.days
    return td
    
# # c = format_date(1514665153,"%m,%d,%Y-%H:%M:%S")
# # print(c)


# # somedate = datetime(year = 2020, month= 2,day=5)
# # dd = calculate_landing_time(somedate, 10)
# # print(dd,type(dd))

# d = days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5))
# print(d)