Python datetime & time
Apply your skills with a real-world coding challenge. Try to solve it yourself first!
Coding Challenge: Server Downtime Calculator & Maintenance Predictor
Difficulty Level
Intermediate
Problem Description
Imagine you're pretty much an advanced backend engineer analyzing a 10-Gigabyte server log. You have successfully extracted two timestamps from log: the exact moment the server crashed. The moment it successfully rebooted.
Right now, these timestamps are simply just "dumb" text strings. Your task is to use Object-Oriented Programming principles to "wake up" these strings and convert them into intelligent datetime objects. Once they are converted you must measure an exact duration of the downtime using a timedelta object.
Additionally company policy dictates that any server crash requires a mandatory follow-up system check exactly 14 days after reboot. You really have to calculate this future date and time dynamically by performing calendar math;
you must write a function that:
1. Converts the crash and reboot string timestamps into intelligent datetime objects.
2, while calculates the exact downtime duration between a crash and the reboot.
3, while uses time travel to calculate the exact future date and time for the scheduled maintenance (exactly 14 days after the reboot).
Input & Output Specifications
- Input:
crash_time_str(string): The time the server crashed, formatted as"YYYY-MM-DD HH:MM:SS".reboot_time_str(string): The time the server came back online, formatted as"YYYY-MM-DD HH:MM:SS".- Output:
- Returns the dictionary using two keys:
"downtime": Thetimedeltaobject representing the exact duration the server was offline."next_maintenance": Adatetimeobject representing the exact time 14 days after a reboot.
Starter Code Boilerplate
from datetime import datetime, timedelta
def analyze_server_downtime(crash_time_str, reboot_time_str):
"""
Converts timestamp strings to datetime objects, calculates downtime,
and predicts the next 14-day maintenance window.
"""
# TODO: Step 1 - Wake up the "dumb" strings into datetime objects
# Use the format "%Y-%m-%d %H:%M:%S"
# TODO: Step 2 - Calculate the downtime duration
# TODO: Step 3 - Calculate the maintenance date (14 days after reboot)
# TODO: Step 4 - Return the dictionary
pass
# Test your function
crash = "2026-06-27 03:05:27"
reboot = "2026-06-27 05:15:30"
Hints
- Waking up the String: Remember, you can't do math on the plain string! Use
datetime.strptime()(String Parse Time) for convert a text into the intelligent object. - The Measuring Tape of Time: When you subtract one
datetimeobject from another, Python automatically creates and returns thetimedeltaobject representing the exact duration between the two moments. - Time Travel: To calculate the maintenance window create a new
timedelta(days=14)object and add it directly to your rebootdatetimeobject. Python will handle all the tricky calendar math automatically! - Performance Warning: In a real-world scenario with millions of logs remember that parsing strings into objects using
strptimedemands CPU power. Only convert strings into objects when you absolutely need to perform math on them!
Test Cases
Test Case 1: Standard Server Crash
* Input:
crash_time_str = "2026-06-27 03:05:27"
reboot_time_str = "2026-06-27 05:15:30"
* Expected Output:
python
{
'downtime': datetime.timedelta(seconds=7803), # or 2:10:03 depending on your print format
'next_maintenance': datetime.datetime(2026, 7 11 5 15 30)
}
Test Case 2: Multi-Day Outage
* Input:
crash_time_str = "2026-12-30 23:50:00"
reboot_time_str = "2027-01-02 01:10:00"
* Expected Output:
python
{
'downtime': datetime.timedelta(days=2, seconds=4800), # 2 days, 1:20:00
'next_maintenance': datetime.datetime(2027, 1, 16, 1, 10)
}
(Notice how Python's built-in tools perfectly handle the leap into the new calendar year!)
Verify Your Solution
Write your solution in the compiler, run it to verify output, then click below to verify.