Python Forum
pyad time conversion error (plus solution)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyad time conversion error (plus solution)
#1
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
Reply
#2
Pyad's open issues page is probably the best suited place to post your suggestion.
Reply
#3
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Plotting by Time, error mansoorahs 1 736 May-16-2023, 09:46 AM
Last Post: Larz60+
  Need better solution for time-delayed printing Arimathean 6 2,812 Feb-04-2021, 10:42 PM
Last Post: bowlofred
  Time Limit Exceeded error loves 5 3,151 Dec-03-2020, 07:15 AM
Last Post: Sofia_Grace
  Time conversion error tester_V 1 2,029 Oct-28-2020, 10:48 PM
Last Post: tester_V
  PyAD search function adquery SpongeB0B 2 8,844 Sep-21-2020, 04:29 AM
Last Post: SpongeB0B
  Read CSV error: python KeyError: 'Time' charlicruz 1 5,173 Jun-27-2020, 09:56 AM
Last Post: charlicruz
  Data Type conversion error rajeevjagatap 2 4,345 Apr-15-2020, 03:29 PM
Last Post: rajeevjagatap
  Error when running the second time metro17 3 3,757 Aug-30-2019, 12:09 PM
Last Post: ThomasL
  python opencv grayscale conversion error Spandora 1 9,582 May-26-2019, 10:43 AM
Last Post: heiner55
  Error during 2to3 conversion krow4869 3 3,987 Oct-14-2018, 08:19 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020