![]() |
pyad time conversion error (plus solution) - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: pyad time conversion error (plus solution) (/thread-14330.html) |
pyad time conversion error (plus solution) - PapaZod - Nov-25-2018 Hi all, While learning how to use the pyad module, I came across and odd situation. I was playing with AD timestamps and one of them returned an actual value of zero. I checked both the high part and the low part and both where zero. When I called pyadutils.convert_time, it bombed with the following error: File "C:\MyTools\temp\testingAD.py", line 17, in <module> sDate = pyadutils.convert_datetime(attr) File "C:\Program Files (x86)\Python37-32\lib\site-packages\pyad\pyadutils.py", line 71, in convert_datetime + int(adsi_time_com_obj.lowpart)) - 116444736000000000),10000000)) OSError: [Errno 22] Invalid argument It turns out the actual error is in datetime.datetime.fromtimestamp. The function cannot take a negative value. Any 64 bit timestamp passed to pyadutils.convert_datetime that results in a negative date value will cause the function to crash. I've modified the convert_datetime function to check for this, and return a date of '1970-01-01 00:00:00' which is the other Microsoft "zero date" value. """ original convert_datetime from pyadutils.py """ def convert_datetime(adsi_time_com_obj): """Converts 64-bit integer COM object representing time into a python datetime object.""" # credit goes to John Nielsen who documented this at # http://docs.activestate.com/activepython/2.6/pywin32/html/com/help/active_directory.html. return datetime.datetime.fromtimestamp(old_div((((int(adsi_time_com_obj.highpart) << 32)\ + int(adsi_time_com_obj.lowpart)) - 116444736000000000),10000000)) """ modified convert_datetime to account for a calculated date value being less than 0 """ def convert_datetime(adsi_time_com_obj): high_part = int(adsi_time_com_obj.highpart) << 32 low_part = int(adsi_time_com_obj.lowpart) date_value = ((high_part + low_part) - 116444736000000000) // 10000000 # # The "fromtimestamp" function in datetime cannot take a # negative value, so if the resulting calculated date value # is negative, explicitly set it to 18000. This will result in # the date 1970-01-01 00:00:00 being returned from this function. # if date_value < 0: date_value = 18000 return datetime.datetime.fromtimestamp(date_value)It would be nice if this could be updated in the pyad module and I hope this helps someone else down the road. --PZ RE: pyad time conversion error (plus solution) - Gribouillis - Nov-25-2018 Pyad's open issues page is probably the best suited place to post your suggestion. RE: pyad time conversion error (plus solution) - PapaZod - Nov-25-2018 (Nov-25-2018, 08:21 AM)Gribouillis Wrote: Pyad's open issues page is probably the best suited place to post your suggestion. Thank you, I have now done that. --PZ |