DEALING WITH THE PYTHON TIME MODULE FROM ROCKET MV BASIC

Part 2 of 3

In 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 now 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 than 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 an 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 an integer 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 formatted 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. It 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

Michael Rajkowski 12 Posts

As a member of the Rocket MultiValue Support organization, Michael has worked with MultiValue for over 25 years in numerous professional roles. He is especially fascinated with the areas of MultiValue that intersect with other technologies. He recently moved to Irvine, where he can not only expand his MultiValue expertise to include the Rocket D3 product family, but also where he, his wife and son can enjoy being closer to Disneyland.

0 Comments

Leave a Comment

Your email address will not be published. Required fields are marked *