AttributeError: type object 'datetime.time' has no attribute 'mktime'

tamus picture tamus · Nov 5, 2016 · Viewed 7.8k times · Source

I'm getting this attribute error either because I'm importing the modules or else referencing them incorrectly.

from datetime import date, timedelta, datetime, time, tzinfo

with

def utc2local (utc):
    epoch = time.mktime(utc.timetuple())
    offset = datetime.fromtimestamp (epoch) - datetime.utcfromtimestamp (epoch)
    return utc + offset

called by

(utc2local(rise), utc2local(set))

which gives me - AttributeError: type object 'datetime.time' has no attribute 'mktime'

I have changed my imports to

import time
from datetime import date, timedelta, datetime, tzinfo

but I get - TypeError: 'module' object is not callable

How should I be calling the modules or what should I be using to reference them, thanks

Answer

Dwi Sulfahnur picture Dwi Sulfahnur · Nov 5, 2016

you don't need to import datetime Just do it:

import time
time.mktime(your_time)

or

from time import mktime
mktime(t)

source: https://www.tutorialspoint.com/python/time_mktime.htm