Rocket UniData

Dealing with Internal and Display format data in your MV database with Python

  • 1.  Dealing with Internal and Display format data in your MV database with Python

    Posted 07-29-2020 13:42
    Edited by Michael Rajkowski 07-29-2020 13:50

    By storing your data in internal format in your MV database, you can easily convert the data to a desired display format.   One of the most common uses for conversion codes is dealing with dates.  In MV the internal representation of the date is an integer.  

    For example the internal format for "04 Dec 2014" is 17140.  Those familiar with U2 BASIC know they can get the internal format to the display date format by using the OCONV function.

     

    I.e   PRINT OCONV(17140, "D")

     

    The 'D' is the Date conversion code and it is used with both the ICONV function as well as the OCONV function.

     

    With the introduction of the u2py modulo, these functions have been exposed as methods of the u2py.DynArray object.

    python> test = u2py.DynArray("04 Dec 2014")
    python> itest =  test.iconv("D")
    python> itest
    <u2py.DynArray value=b'17140'>

    Since the itest variable now has a date in internal format, you can use a different conversion code to get other display formats. 

    python> itest.oconv("D4/")
    <u2py.DynArray value=b'12/04/2014'>

    For a complete list of Date conversion codes please see BASIC Language documentation for the MV database you are using.  Along with the other date codes there are other built in conversion code in the MV databases. 

    One that tends to go along with date is time.  The internal format of time is the number of seconds since midnight. 


    python> internalTime = u2py.DynArray("16200")
    python> print(internalTime.oconv("MTHS"))
    04:30:00AM

    Up to this point I have been hard coding the internal or external values used to pass to the u2py methods.  The following example shows how you can use the python time module with the u2py module to work with dates.

    Let us first start with displaying the current time/date in python.

    python> import time
    python> now = time.time()
    python> theLocalTime = time.localtime(now)
    python> print( time.asctime(theLocalTime) )
    Fri Dec  5 07:30:07 2014

    While this produces the date and time, it will not convert directly into U2 without modifying the display format to something handled by the U2 iconv method.

    python> u2DisplayTime = u2py.DynArray(time.strftime("%d %b %Y", theLocalTime))
    python> u2DisplayDate.iconv("D")
    <u2py.DynArray value=b'17141'>

     

    Please take some time and work with both the python time modulo as well as the U2 oconv and iconv methods.  In my next post, I will show how to access the python time module from U2 BASIC.


    ------------------------------
    Michael Rajkowski
    Rocket Software
    ------------------------------