import pyngres as py
import ctypes

def readable(ingresdate):
    '''return a human-readable representation of an INGRESDATE'''

    # an INGRESDATE requires up to 26 characters to display
    readable_date = ctypes.c_buffer(26)

    # allocate the conversion control block
    cvp = py.IIAPI_CONVERTPARM()
    # describe the source 
    cvp.cv_srcDesc.ds_dataType = py.IIAPI_DTE_TYPE
    cvp.cv_srcDesc.ds_nullable = False 
    cvp.cv_srcDesc.ds_length = 12
    cvp.cv_srcDesc.ds_columnType = py.IIAPI_COL_QPARM
    cvp.cv_srcValue.dv_length = 12
    cvp.cv_srcValue.dv_value = ctypes.addressof(ingresdate)
    # describe the destination
    cvp.cv_dstDesc.ds_dataType = py.IIAPI_CHA_TYPE
    cvp.cv_dstDesc.ds_length = ctypes.sizeof(readable_date)
    cvp.cv_dstDesc.ds_columnType = py.IIAPI_COL_QPARM
    cvp.cv_dstValue.dv_length = ctypes.sizeof(readable_date)
    cvp.cv_dstValue.dv_value = ctypes.addressof(readable_date)

    # convert the INGRESDATE structure to a human-readable byte-array
    py.IIapi_convertData(cvp)
    # decode the byte array into a Python str()
    return readable_date.value.decode()

# the OpenAPI conversion functions won't work unless the OpenAPI is initialized
inp = py.IIAPI_INITPARM()
inp.in_timeout = -1
inp.in_version = py.IIAPI_VERSION
py.IIapi_initialize(inp)
...
# an INGRESDATE structure needs a 12 byte buffer
dob = ctypes.c_buffer(12)
# fetch date-of-birth (dob) from the DBMS
...
# convert the binary INGRESDATE and display it 
readable_dob = readable(dob)
print(readable_dob)
