zakird / pyad

Python Active Directory Tools | *Not actively maintained*
http://zakird.github.io/pyad/
176 stars 72 forks source link

pyad time conversion error #86

Closed tmunro55 closed 5 years ago

tmunro55 commented 5 years ago

Hello,

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 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.

zakird commented 5 years ago

Make sense. If you submit a PR, I'm happy to merge and release.

zakird commented 5 years ago

(Whoops! didn't mean to close)

tmunro55 commented 5 years ago

Sorry, I have no idea what that is or how to do it.

-- Life moves too quickly, don't forget to stop and smell the roses. tmunro55@gmail.com or tim.munro@outlook.com

From: Zakir Durumeric Sent: November 25, 2018 10:34 PM To: zakird/pyad Cc: Tim; Author Subject: Re: [zakird/pyad] pyad time conversion error (#86)

Make sense. If you submit a PR, I'm happy to merge and release. — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

zakird commented 5 years ago

There's some information here: https://help.github.com/articles/about-pull-requests/

tmunro55 commented 5 years ago

Thanks

-- Life moves too quickly, don't forget to stop and smell the roses. tmunro55@gmail.com or tim.munro@outlook.com

From: Zakir Durumeric Sent: November 25, 2018 10:38 PM To: zakird/pyad Cc: Tim; Author Subject: Re: [zakird/pyad] pyad time conversion error (#86)

There's some information here: https://help.github.com/articles/about-pull-requests/ — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

tmunro55 commented 5 years ago

I’m sorry, I’m totally lost here.

-- Life moves too quickly, don't forget to stop and smell the roses. tmunro55@gmail.com or tim.munro@outlook.com

From: Zakir Durumeric Sent: November 25, 2018 10:38 PM To: zakird/pyad Cc: Tim; Author Subject: Re: [zakird/pyad] pyad time conversion error (#86)

There's some information here: https://help.github.com/articles/about-pull-requests/ — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

tmunro55 commented 5 years ago

I've submitted a pull request. Hope I did it correctly. --tim

stevenpitts commented 5 years ago

@zakird it looks like this was pulled, can we close this issue?