# Time Delta - HackerRank Python Solution

## Time Delta - HackerRank Python Solution

Hello Friends, How are you? Today I am going to solve the HackerRank Python Time Delta Problem with a very easy explanation. In this article, you will get more than one approach to solving this problem. So let's start-

### HackerRank Python Time Delta Solution - Problem Statement

When users post an update on social media, such as a URL, image, status update etc., other users in their network are able to view this new post on their news feed. Users can also see exactly when the post was published, i.e, how many hours, minutes or seconds ago.

Since sometimes posts are published and viewed in different time zones, this can be confusing. You are given two timestamps of one such post that a user can see on his newsfeed in the following format:

Day dd Mon yyyy hh:mm:ss +xxxx

Here +xxxx represents the time zone. Your task is to print the absolute difference (in seconds) between them.

Input Format

The first line contains T, the number of test cases.
Each test case contains 2 lines, representing time t1 and time t2.

Constraints

• The input contains only valid timestamps
• Year <= 3000

Output Format

Print the absolute difference (t1-t2) in seconds.

Sample Input 0

2 Sun 10 May 2015 13:54:36 -0700 Sun 10 May 2015 13:54:36 -0000 Sat 02 May 2015 19:54:36 +0530 Fri 01 May 2015 13:54:36 -0000 {codeBox}

Sample Output 0

25200 88200 {codeBox}

Explanation

In the first query, when we compare the time in UTC for both the timestamps, we see a difference of 7 hours. which is 7 x 3600 seconds or 25200 seconds.

Similarly, in the second query, the time difference is 5 hours and 30 minutes for the time zone adjusting for that we have a difference of 1 day and 30 minutes. Or  24 x 3600 + 30 x 60 = 88200

### Python Time Delta - Hacker Rank Solution

Approach I: Time Delta HackerRank Python Solution

``````import datetime
cas = int(input())
for t in range(cas):
timestamp1 = input().strip()
timestamp2 = input().strip()
time_format = "%a %d %b %Y %H:%M:%S %z"
time_second1 = datetime.datetime.strptime(timestamp1,time_format)
time_second2 = datetime.datetime.strptime(timestamp2,time_format)
print(int(abs((time_second1-time_second2).total_seconds())))``````

Approach II: HackerRank Time Delta Python Solution

``````import datetime

return datetime.datetime.strptime(input(), '%a %d %b %Y %H:%M:%S %z')

def main():
T = int(input())
for _ in range(T):

print(int(abs(t1 - t2).total_seconds()))

if __name__ == '__main__':
main()``````

Approach III: HackerRank Python Time Delta Solution

``````import sys
from datetime import datetime, timedelta

def time_delta(t1, t2):
t1_tokens, t2_tokens = (t1.split(), t2.split())
datetime1, datetime2 = (datetime.strptime(' '.join(t1_tokens[1:-1]), '%d %b %Y %H:%M:%S'),
datetime.strptime(' '.join(t2_tokens[1:-1]), '%d %b %Y %H:%M:%S'))
t1_tmz_token = t1_tokens[-1:]
t1_tmz = (t1_tmz_token[:1], t1_tmz_token[1:3].lstrip('0'), t1_tmz_token[3:].lstrip('0'))
# print(datetime1, t1_tmz)

t2_tmz_token = t2_tokens[-1:]
t2_tmz = (t2_tmz_token[:1], t2_tmz_token[1:3].lstrip('0'), t2_tmz_token[3:].lstrip('0'))
# print(datetime2, t2_tmz)

datetime1 = apply_timezone(datetime1, t1_tmz)
datetime2 = apply_timezone(datetime2, t2_tmz)
# print(datetime1, datetime2)

return int(abs(((datetime1 - datetime2).total_seconds())))

def apply_timezone(dt, tmz):
if tmz:
hours_arg = -int(tmz + tmz)
# print("hours arg:", hours_arg)
dt = dt + timedelta(hours=hours_arg)

if tmz:
minutes_arg = -int(tmz + tmz)
# print("minutes arg:", minutes_arg)
dt = dt + timedelta(minutes=minutes_arg)

return dt

if __name__ == "__main__":
t = int(input().strip())
for a0 in range(t):
t1 = input().strip()
t2 = input().strip()
delta = time_delta(t1, t2)
print(delta)``````

Disclaimer: The above Problem ( Python Time Delta ) is generated by Hackerrank but the Solution is Provided by MyEduWaves. This tutorial is only for Educational and Learning purposes. Authority if any of the queries regarding this post or website fill the contact form.

I hope you have understood the solution to this HackerRank Problem. All these three solutions will pass all the test cases. Now visit Python Time Delta Hackerrank Problem and try to solve it again.

All the Best!