Rocket U2 | UniVerse & UniData

Dealing with the Python time module from Rocket MV BASIC

  • 1.  Dealing with the Python time module from Rocket MV BASIC

    ROCKETEER
    Posted 08-05-2020 03:57

     

    I the previous post “Dealing with Internal and Display format data in your MV database with Python” I showed how it is possible to convert the python date to the MV internal format.  We will not show how we can get the python time date representation.  In the time.time() function returns the number of seconds since the Epoch.  The Epoch is a system defined date.

     

    python> time.asctime(time.gmtime(0))
    'Thu Jan  1 00:00:00 1970'

     

    By having the number of seconds from a specified point in time, we are able to generate a display value that has both the time and the date.  This is different then what MV provides, which is q date value and a separate time value.

     

    What we now need to show is how to get the python time values into a MV BASIC program. 

     

    The first thing we need is a module that contains the functions we want to expose to the MV BASIC programmer. 

     

    python_time.py
    # Example module used in article that describes how to get the python
    # time/date from a MV BASIC program.
    import time
     
    def gettime(  ):
         "””gettime( ) - returns the number of seconds from Epoch"””
         now = int(time.time())
         return now
     
    def localtime( now ):
         "””localtime( now ) - takes a interger that represents the seconds
                             since Epoch and converts it into a display format”””
         theLocalTime = time.localtime(now)
         d = ( time.asctime(theLocalTime) )
         return d
     
    def getepoch( outtime ):
        “””getepoch( outtime ) - takes a display formated date/time and returns
                                 the number of seconds since the Epoch”””
        itime = time.strptime( outtime )
        epochtime = time.mktime( itime )
        return epochtime

     

    Note that if we run the functions from python, you see we can get the time is seconds, convert it to a display format, and get the time in seconds from the display format:

     

    python> import python_time
    python> python_time.gettime()
    1418144928
    python> python_time.localtime(1418144928)
    'Tue Dec  9 09:08:48 2014'
    python> python_time.getepoch('Tue Dec  9 09:08:48 2014')
    1418144928.0

     

    To access the python module from MV BASIC we will need to import the module into the current session.

     

    001: ModuleName = "python_time"
    002: *  import the module
    003: pymodule = PyImport(ModuleName)

     

    Note that the U2 python API provides several @ variables to access any python exception information of the last executed python method/function. 

     

    029: CHECK_ERROR:
    030: IF @PYEXCEPTIONTYPE NE '' THEN
    031:   PRINT "EXCEPTION TYPE IS " :@PYEXCEPTIONTYPE
    032:   PRINT "EXCEPTION MESSAGE IS " :@PYEXCEPTIONMSG
    033:   PRINT "EXCEPTIONTRACEBACK IS " :@PYEXCEPTIONTRACEBACK
    034:   STOP "ENDING AT EXECPTION"
    035: END 

    036: RETURN

     

    To call the python function, we need to set a variable with the function name, and pass it into the U2 Basic pyCallFunction:

     

    006: *  call the Python function
    007  FuncName = "gettime"
    008: pyresult = PyCallFunction(ModuleName, FuncName)
    009: GOSUB CHECK_ERROR
    010: PRINT "Python RESULT: "
    011: PRINT "python time= " :pyresult 

     

    In the above example once we verified there were no raised exceptions, we display the results:

     

    Python RESULT:
    python time= 1418146039

     

    The following is the complete listing of the MV BASIC code ( note this example was written in UniData ):

    ModuleName = "python_time"
    *  import the module
    pymodule = PyImport(ModuleName)
    GOSUB CHECK_ERROR
    *
    *  call the Python function
    FuncName = "gettime"
    pyresult = PyCallFunction(ModuleName, FuncName)
    GOSUB CHECK_ERROR
    PRINT "Python RESULT: "
    PRINT "python time= " :pyresult
    *
    A = pyresult
    FuncName = "localtime"
    pyresult = PyCallFunction(ModuleName, FuncName, A )
    GOSUB CHECK_ERROR
    PRINT "Python RESULT: "
    PRINT "python local time= " :pyresult
    *
    B = pyresult
    FuncName = "getepoch"
    pyresult = PyCallFunction(ModuleName, FuncName, B )
    GOSUB CHECK_ERROR
    PRINT "Python RESULT: "
    PRINT "python time= " :pyresult
    STOP
    ************************************************************************
    CHECK_ERROR:
    IF @PYEXCEPTIONTYPE NE '' THEN
      PRINT "EXCEPTION TYPE IS " :@PYEXCEPTIONTYPE
      PRINT "EXCEPTION MESSAGE IS " :@PYEXCEPTIONMSG
      PRINT "EXCEPTIONTRACEBACK IS " :@PYEXCEPTIONTRACEBACK
      STOP "ENDING AT EXECPTION"
    END 
    RETURN

     

    Compile and run the previous program will produce out like the following:

     

    Python RESULT:
    python time= 1418146291
    Python RESULT:
    python local time= Tue Dec  9 09:31:31 2014
    Python RESULT:
    python time= 1418146291

     

    While this example only deals with specific display formats of the python time/date, it is a good starting point for learning how to write and use your own python modules.

    Now that we can get the python time/date from MV BASIC our next step is to make it easier for the MV developer.  In the next post, I will show how to access the python time/date module from a user defined function in U2 BASIC.



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