-1

I have two ints that represents epochtimes:

x1 = 1597611600
x2 = 1597489203

I want to check if there is less then 72 hours between them. What is the best way to do so in Python3? Please notice that both are int.

Cranjis
  • 1,590
  • 8
  • 31
  • 64
  • 1
    What have you tried so far? You could do it by the tools stated in your tags. – adamkwm Jun 27 '21 at 09:13
  • @adamkwm It didn't work because x1, x2 are strings and I couldnt find how to use timedelta – Cranjis Jun 27 '21 at 09:14
  • 2
    Does this answer your question? [python - Difference between two unix timestamps](https://stackoverflow.com/questions/45603232/python-difference-between-two-unix-timestamps) – Jacques Jun 27 '21 at 09:19
  • Do you know what an Epoch time represents, and in which unit? Can you calculate the difference? Can you calculate what 72 hours represent in this unit? Can you do a comparison? What part of this is your actual problem?? – Thierry Lathuille Jun 27 '21 at 09:20

1 Answers1

0

You can convert epoch timestamps to datetime objects, perform subtraction and compare the result to timedelta object. However, you can simply do a comparision in seconds.

from datetime import datetime as dt, timedelta as td
def epoch_diff_within(ts1, ts2, hr):
    d1 = dt.fromtimestamp(x1)
    d2 = dt.fromtimestamp(x2)
    return d1 - d2 < td(hours=hr)

def epoch_diff_within2(ts1, ts2, hr):
    return ts1 - ts2 < hr * 60 * 60

x1 = 1597611600
x2 = 1597489203
print(epoch_diff_within(x1, x2, 72)) # Output: True
print(epoch_diff_within2(x1, x2, 72)) # Output: True
print(epoch_diff_within(x1, x2, 24)) # Output: False
print(epoch_diff_within2(x1, x2, 24)) # Output: False
adamkwm
  • 1,155
  • 2
  • 6
  • 18