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 as dt, timedelta, timezone
# Start coding here. Use as many cells as you need.

Define a function called format_date(), which formats a timestamp into a readable datetime string.

  • It must accept two parameters: timestamp - the Unix timestamp integer, and datetime_format - a string specifying the desired date format.
  • The function should return the date correctly formatted as a string.
  • For example, calling format_date(1514665153, "%d-%m-%Y") should output "30-12-2017".
def format_date(timestamp, datetime_format):
    dateime = dt.fromtimestamp(timestamp, tz=timezone.utc)
    
    return dateime.strftime(datetime_format)
format_date(1514665153, "%d-%m-%Y")

Define a function called calculate_landing_time(), which calculates the estimated landing time.

  • It must accept two parameters: rocket_launch_dt - the rocket launch datetime object, and travel_duration - the expected travel time in days as an integer.
  • The function should return the estimated Mars landing time as a datetime string in the format DD-MM-YYYY.
  • For example, calling calculate_landing_time(datetime(2023, 2, 15), 20) should output "07-03-2023".
def calculate_landing_time(rocket_launch_dt, travel_duration):
    rocket_launch_dt = rocket_launch_dt.astimezone(timezone.utc)
    
    landing_time = rocket_launch_dt + timedelta(days = travel_duration)
    
    return landing_time.strftime('%d-%m-%Y')
calculate_landing_time(datetime(2023, 2, 15), 20)

Define a function named days_until_delivery(), which calculates the days until a package arrives for customers.

  • It must accept two parameters: expected_delivery_dt - the estimated delivery date as a datetime object for the package, and current_dt - the current date as a datetime object.
  • The function should calculate the difference in days between the expected delivery datetime and the current datetime, then return the number of days remaining as an integer.
  • For example, calling days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5)) should output 10.
def days_until_delivery(expected_delivery_dt, current_dt):
    expected_delivery_dt = expected_delivery_dt.astimezone(timezone.utc)
    current_dt = current_dt.astimezone(timezone.utc)
    
    delivery_in_days = expected_delivery_dt - current_dt
    
    return delivery_in_days.days
days_until_delivery(datetime(2023, 2, 15), datetime(2023, 2, 5))