0

I want to calculate the time difference between the epoch and a date in the future, mktime gives an error (OverflowError: mktime argument out of range) if the year is 3001 or higher (on certain OS). I would like to be able to enter up to the year 9999.

for the line:

time_sec = time.mktime(event_date.timetuple())

what else can I use to overcome the overflow error, and still be correct to local time?

My code so far is:

import datetime
import time 

yr= 3001
mth = 12
day = 4
start_time = 8

event_date = datetime.datetime(yr, mth, day, int(start_time))
time_sec = time.mktime(event_date.timetuple())
t= time.localtime()
time_frm_epoch = time.mktime(t)
time_remain = time_sec - time_frm_epoch # return false if time_remain < 0

print(time_remain)

Would really appreciate any advice!

filipe
  • 1
  • 1

1 Answers1

0

Here's a hint.

Python | mktime overflow error

I have rewritten your code in this way.

import datetime

yr = 3001
mth = 12
day = 4
start_time = 8

event_date = datetime.datetime(yr, mth, day, int(start_time))
t = datetime.datetime.now()
diff = event_date - t
time_remain = diff.days * 24 * 3600 + diff.seconds

print(time_remain)

However, I think event_date and time_sec in your code are a bit suspicious about the time zone. Please verify my code by paying attention to the time zone.