Time Delta Hacker Rank Solution – Queslers

Problem: Time Delta Hacker Rank Solution

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 testcases.
Each testcase contains 2 lines, representing time t1 and time t2.

Constraints :

  • Input contains only valid timestamps
  • year <= 3000

Output Format :

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

Sample Input :

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

Sample Output :

25200
88200

Explanation :

In the first query, when we compare the time in UTC for both the time stamps, we see a difference of 7 hours. which is 7*3600 seconds or 25200 seconds.
Similarly, in the second query, time difference is 5 hours and 30 minutes for time zone adjusting for that we have a difference of 1 day and 30 minutes. Or 24*3600+30*60 => 88200

Time Delta Hacker Rank Solution in python 2

# Enter your code here. Read input from STDIN. Print output to STDOUT
from datetime import datetime, timedelta

def gettime():
    s = raw_input()
    t = datetime.strptime(s[:-6], "%a %d %b %Y %H:%M:%S")
    m = int(s[-2:])  
    h = int(s[-4:-2])
    if s[-5] == '+':
        t -= timedelta(hours=h, minutes=m)
    else:
        t += timedelta(hours=h, minutes=m)
    return t
for _ in xrange(input()):
    t1 = gettime()
    t2 = gettime()
    d = abs(t1 - t2)
    #print d
    print d.days * 86400 + d.seconds


Time Delta Hacker Rank Solution in python 2

Time Delta Hacker Rank Solution in python 3

import datetime

format_string = "%a %d %b %Y %H:%M:%S %z"
T = int(input())

for test in range(T):
    t1 = str(input())
    t2 = str(input())

    parsed_t1 = datetime.datetime.strptime(t1, format_string)
    parsed_t2 = datetime.datetime.strptime(t2, format_string)

    diff = parsed_t2 - parsed_t1

    print (int(abs(diff.total_seconds())))

Time Delta Hacker Rank Solution in pypy

# Enter your code here. Read input from STDIN. Print output to STDOUT
from datetime import datetime,timedelta
def timeformatter(t):
    ret = datetime.strptime(t[:-6],"%a %d %b %Y %H:%M:%S")
    if t[-5] == '-':
        ret +=  timedelta(hours=int(t[-4:-2]),minutes=int(t[-2:]))
    else :
        ret -=  timedelta(hours=int(t[-4:-2]),minutes=int(t[-2:]))
    return ret
T = int(raw_input())

for i in range(T):
    t1,t2= [timeformatter(raw_input()) for _ in range(2)]
    print abs(int((t1-t2).total_seconds()))

Time Delta Hacker Rank Solution in pypy 3

from datetime import datetime
date_strftime =  '%a %d %b %Y %H:%M:%S %z'
a = []
count = input()
for i in range(int(count)):
    start = input()
    end = input()
    a.append(abs(
        datetime.strptime(end, date_strftime) -
        datetime.strptime(start, date_strftime)).total_seconds())
for i in range(int(count)):
    print (int(a[i]))
Time Delta Hacker Rank Solution Review:

In our experience, we suggest you solve this Time Delta Hacker Rank Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

Time Delta problem is available on Hacker Rank for Free, if you are stuck anywhere between a compilation, just visit Queslers to get Time Delta Hacker Rank Solution

Conclusion:

I hope this Time Delta Hacker Rank Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Hacker Rank, Leetcode, Codechef, Codeforce Solution.

This Problem is intended for audiences of all experiences who are interested in learning Programming in a business context; there are no prerequisites.

Keep Learning!

More Hacker Rank Problem & Solutions >>

Staircase Hacker Rank Solution

A Very Big Sum Hacker Rank Solution

Diagonal Difference Hacker Rank Solution

Nested Lists Hacker Rank Solution

Lists Hacker Rank Solution

Leave a Reply

Your email address will not be published.