Time Delta is a medium difficulty problem that involves datetime manipulation and we have to return the absolute difference between in seconds. We will learn how to solve this problem in Python through a step-by-step tutorial in Python3.
Problem Statement and Explanation
Given two timestamps in the format Day dd Mon yyyy hh:mm:ss +xxxx
where +xxxx
represents the time zone. We have to print the absolute difference (in seconds) between them.
Suppose the first timestamp is Sun 10 May 2015 13:54:36 -0700
and the second timestamp is Sun 10 May 2015 13:54:36 -0000
.
- t1 = Sun 10 May 2015 13:54:36 -0700
- t2 = Sun 10 May 2015 13:54:36 -0000
The absolute difference between them is 25200 seconds because the time zone offsets are -0700
and -0000
and then 7 hours is 25200 seconds. 7 * 3600 = 25200 seconds.
- In the given solution, we will use the
datetime
module to convert the given string into a datetime object. - Then we will find the absolute difference between them and return the difference in seconds.
Time Delta Solution in Python
Time Complexity of the Solution
The time complexity of the above solution is O(1)
because we are using the datetime
module to convert the given string into a datetime object and then we are finding the absolute difference between them and returning the difference in seconds.
Problem statement is taken from Hackerrank, and the solutions are implemented by CodePerfectPlus team
Top comments (0)