diff --git a/time_delta.py b/time_delta.py index c805c98b770..9b46d34f79f 100644 --- a/time_delta.py +++ b/time_delta.py @@ -1,6 +1,9 @@ -"""Time Delta Solution""" - +""" +Time Delta Calculator +This module provides functionality to calculate the absolute difference +in seconds between two timestamps in the format: Day dd Mon yyyy hh:mm:ss +xxxx +""" # ----------------------------------------------------------------------------- # You are givent two timestams in the format: Day dd Mon yyyy hh:mm:ss +xxxx # where +xxxx represents the timezone. @@ -28,30 +31,85 @@ # 88200 # ------------------------------------------------------------------------------ -# Imports + import datetime +from typing import List, Tuple -# Complete the time_delta function below. -def time_delta(t1, t2): +def parse_timestamp(timestamp: str) -> datetime.datetime: """ - Calculate the time delta between two timestamps in seconds. + Parse a timestamp string into a datetime object. + + Args: + timestamp: String in the format "Day dd Mon yyyy hh:mm:ss +xxxx" + + Returns: + A datetime object with timezone information """ - # Convert the timestamps to datetime objects - t1 = datetime.datetime.strptime(t1, "%a %d %b %Y %H:%M:%S %z") - t2 = datetime.datetime.strptime(t2, "%a %d %b %Y %H:%M:%S %z") + # Define the format string to match the input timestamp format + format_str = "%a %d %b %Y %H:%M:%S %z" + return datetime.datetime.strptime(timestamp, format_str) - return t1 - t2 +def calculate_time_delta(t1: str, t2: str) -> int: + """ + Calculate the absolute time difference between two timestamps in seconds. + + Args: + t1: First timestamp string + t2: Second timestamp string + + Returns: + Absolute time difference in seconds as an integer + """ + # Parse both timestamps + dt1 = parse_timestamp(t1) + dt2 = parse_timestamp(t2) + + # Calculate absolute difference and convert to seconds + time_difference = abs(dt1 - dt2) + return int(time_difference.total_seconds()) -if __name__ == "__main__": - t = int(input()) - for itr_t in range(t): - t1 = input() +def read_test_cases() -> Tuple[int, List[Tuple[str, str]]]: + """ + Read test cases from standard input. + + Returns: + A tuple containing: + - Number of test cases + - List of timestamp pairs for each test case + """ + try: + num_test_cases = int(input().strip()) + test_cases = [] + + for _ in range(num_test_cases): + timestamp1 = input().strip() + timestamp2 = input().strip() + test_cases.append((timestamp1, timestamp2)) + + return num_test_cases, test_cases + except ValueError as e: + raise ValueError("Invalid input format") from e + + +def main() -> None: + """ + Main function to execute the time delta calculation program. + """ + try: + num_test_cases, test_cases = read_test_cases() + + for t1, t2 in test_cases: + result = calculate_time_delta(t1, t2) + print(result) + + except ValueError as e: + print(f"Error: {e}") + except Exception as e: + print(f"Unexpected error: {e}") - t2 = input() - delta = time_delta(t1, t2) - # print Delta with 1 Decimal Place - print(round(delta.total_seconds(), 1)) +if __name__ == "__main__": + main()