0

Example code:

from datetime import datetime, date
from dateutil.relativedelta import relativedelta
import time

timestamp = 1620013967000

cunix = int(time.time())
ounix = timestamp / 1000

outc_time = datetime.utcfromtimestamp(ounix)
cutc_time = datetime.utcfromtimestamp(cunix)

#time_diff = cutc_time - outc_time

if condition == met:
    if(outc_time + relativedelta(months=+6) - cutc_time) >= 1: # This doesn't work but I'm trying to give an idea of what I would like to accomplish.
        do_stuff()

An example of the output I'm getting currently with these variables:

143 days, 6:53:04
162 days, 11:42:26
122 days, 19:31:56
131 days, 20:38:03
6 days, 21:03:03
12 days, 3:41:45
159 days, 5:48:15
159 days, 14:37:54
50 days, 0:04:41
153 days, 13:43:03

Basically what I want is an idea on how to make a condition where it'll only proceed if the difference in days is equal to or less than 1. I'm a little lost on how to do it currently. Any help is appreciated.

humid
  • 13
  • 5
  • Instead of `>= 1`, `>= timedelta(days=1)`? Other than that, please think about how to make your code example a [mre]. As it stands, there's unused variables, undefined variables etc. - which makes it kind of hard (for me) to get the point. – FObersteiner May 05 '21 at 09:53
  • also note: [utcfromtimestamp can be missleading](https://blog.ganssle.io/articles/2019/11/utcnow.html) – FObersteiner May 05 '21 at 09:54
  • You say equal to or less than, do you really mean that? Your code is looking for *greater than or equal to 1*, which seems like the reverse of what you say you want. – Blckknght May 05 '21 at 10:13

1 Answers1

0

Try using timedelta:

from datetime import datetime, timedelta
import time

timestamp = 1620013967000

cunix = int(time.time())
ounix = timestamp / 1000

outc_time = datetime.utcfromtimestamp(ounix)
cutc_time = datetime.utcfromtimestamp(cunix)

diff = cutc_time - outc_time

if diff >= timedelta(days=1):
    print("hey")

As @Blckknght pointed out you say less than or equal to 1, but your code uses greater than. Either way, just change the symbol in the if statement above.

Also, is utc necessary for your code? For this example utcfromtimestamp could just be replaced with fromtimestamp.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19