|
1 |
| -"""Time Delta Solution""" |
2 |
| - |
| 1 | +""" |
| 2 | +Time Delta Calculator |
3 | 3 |
|
| 4 | +This module provides functionality to calculate the absolute difference |
| 5 | +in seconds between two timestamps in the format: Day dd Mon yyyy hh:mm:ss +xxxx |
| 6 | +""" |
4 | 7 | # -----------------------------------------------------------------------------
|
5 | 8 | # You are givent two timestams in the format: Day dd Mon yyyy hh:mm:ss +xxxx
|
6 | 9 | # where +xxxx represents the timezone.
|
|
28 | 31 | # 88200
|
29 | 32 | # ------------------------------------------------------------------------------
|
30 | 33 |
|
31 |
| -# Imports |
| 34 | + |
32 | 35 | import datetime
|
| 36 | +from typing import List, Tuple |
33 | 37 |
|
34 | 38 |
|
35 |
| -# Complete the time_delta function below. |
36 |
| -def time_delta(t1, t2): |
| 39 | +def parse_timestamp(timestamp: str) -> datetime.datetime: |
37 | 40 | """
|
38 |
| - Calculate the time delta between two timestamps in seconds. |
| 41 | + Parse a timestamp string into a datetime object. |
| 42 | + |
| 43 | + Args: |
| 44 | + timestamp: String in the format "Day dd Mon yyyy hh:mm:ss +xxxx" |
| 45 | + |
| 46 | + Returns: |
| 47 | + A datetime object with timezone information |
39 | 48 | """
|
40 |
| - # Convert the timestamps to datetime objects |
41 |
| - t1 = datetime.datetime.strptime(t1, "%a %d %b %Y %H:%M:%S %z") |
42 |
| - t2 = datetime.datetime.strptime(t2, "%a %d %b %Y %H:%M:%S %z") |
| 49 | + # Define the format string to match the input timestamp format |
| 50 | + format_str = "%a %d %b %Y %H:%M:%S %z" |
| 51 | + return datetime.datetime.strptime(timestamp, format_str) |
43 | 52 |
|
44 |
| - return t1 - t2 |
45 | 53 |
|
| 54 | +def calculate_time_delta(t1: str, t2: str) -> int: |
| 55 | + """ |
| 56 | + Calculate the absolute time difference between two timestamps in seconds. |
| 57 | + |
| 58 | + Args: |
| 59 | + t1: First timestamp string |
| 60 | + t2: Second timestamp string |
| 61 | + |
| 62 | + Returns: |
| 63 | + Absolute time difference in seconds as an integer |
| 64 | + """ |
| 65 | + # Parse both timestamps |
| 66 | + dt1 = parse_timestamp(t1) |
| 67 | + dt2 = parse_timestamp(t2) |
| 68 | + |
| 69 | + # Calculate absolute difference and convert to seconds |
| 70 | + time_difference = abs(dt1 - dt2) |
| 71 | + return int(time_difference.total_seconds()) |
46 | 72 |
|
47 |
| -if __name__ == "__main__": |
48 |
| - t = int(input()) |
49 | 73 |
|
50 |
| - for itr_t in range(t): |
51 |
| - t1 = input() |
| 74 | +def read_test_cases() -> Tuple[int, List[Tuple[str, str]]]: |
| 75 | + """ |
| 76 | + Read test cases from standard input. |
| 77 | + |
| 78 | + Returns: |
| 79 | + A tuple containing: |
| 80 | + - Number of test cases |
| 81 | + - List of timestamp pairs for each test case |
| 82 | + """ |
| 83 | + try: |
| 84 | + num_test_cases = int(input().strip()) |
| 85 | + test_cases = [] |
| 86 | + |
| 87 | + for _ in range(num_test_cases): |
| 88 | + timestamp1 = input().strip() |
| 89 | + timestamp2 = input().strip() |
| 90 | + test_cases.append((timestamp1, timestamp2)) |
| 91 | + |
| 92 | + return num_test_cases, test_cases |
| 93 | + except ValueError as e: |
| 94 | + raise ValueError("Invalid input format") from e |
| 95 | + |
| 96 | + |
| 97 | +def main() -> None: |
| 98 | + """ |
| 99 | + Main function to execute the time delta calculation program. |
| 100 | + """ |
| 101 | + try: |
| 102 | + num_test_cases, test_cases = read_test_cases() |
| 103 | + |
| 104 | + for t1, t2 in test_cases: |
| 105 | + result = calculate_time_delta(t1, t2) |
| 106 | + print(result) |
| 107 | + |
| 108 | + except ValueError as e: |
| 109 | + print(f"Error: {e}") |
| 110 | + except Exception as e: |
| 111 | + print(f"Unexpected error: {e}") |
52 | 112 |
|
53 |
| - t2 = input() |
54 | 113 |
|
55 |
| - delta = time_delta(t1, t2) |
56 |
| - # print Delta with 1 Decimal Place |
57 |
| - print(round(delta.total_seconds(), 1)) |
| 114 | +if __name__ == "__main__": |
| 115 | + main() |
0 commit comments