Skip to content

Commit 39a645d

Browse files
authored
Add docs and new future for time_delta.py
1 parent 1d0eece commit 39a645d

File tree

1 file changed

+76
-18
lines changed

1 file changed

+76
-18
lines changed

time_delta.py

Lines changed: 76 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
"""Time Delta Solution"""
2-
1+
"""
2+
Time Delta Calculator
33
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+
"""
47
# -----------------------------------------------------------------------------
58
# You are givent two timestams in the format: Day dd Mon yyyy hh:mm:ss +xxxx
69
# where +xxxx represents the timezone.
@@ -28,30 +31,85 @@
2831
# 88200
2932
# ------------------------------------------------------------------------------
3033

31-
# Imports
34+
3235
import datetime
36+
from typing import List, Tuple
3337

3438

35-
# Complete the time_delta function below.
36-
def time_delta(t1, t2):
39+
def parse_timestamp(timestamp: str) -> datetime.datetime:
3740
"""
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
3948
"""
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)
4352

44-
return t1 - t2
4553

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())
4672

47-
if __name__ == "__main__":
48-
t = int(input())
4973

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}")
52112

53-
t2 = input()
54113

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

Comments
 (0)