I guess this is a python vs SWIG question more than anything else...
I'm using a C++ package with SWIG Python bindings. One of the objects I receive is a UTC time stamp from which I'm trying to extract the time stamp.
The object has the following characteristics:
>>> print type(obj)
<type 'SwigPyObject'>
>>> print dir(obj)
['__class__', '__cmp__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__hex__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__ne__', '__new__', '__oct__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'acquire', 'append', 'disown', 'next', 'own']
>>> print obj
<Swig Object of type 'UtcTimeStamp *' at 0x0379F320>
How do I extract the data out of it?
UPDATE:
I've found the UTCTimeStamp class which is derived from a DateTime struct - it is part of the open source QuickFix package.
However I still don't know how to access the data. DateTime has simple getter functions such as getYear() - however, how do I access them?
The SWIG %module directive specifies the name of the Python module. If you specify `%module example', then everything is wrapped into a Python 'example' module. Underneath the covers, this module consists of a Python source file example.py and a low-level extension module _example.so.
Getting the UTC timestampUse the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.
You can use the datetime module to convert a datetime to a UTC timestamp in Python. If you already have the datetime object in UTC, you can the timestamp() to get a UTC timestamp. This function returns the time since epoch for that datetime object.
The Simplified Wrapper and Interface Generator (SWIG) is an open-source software tool used to connect computer programs or libraries written in C or C++ with scripting languages such as Lua, Perl, PHP, Python, R, Ruby, Tcl, and other languages like C#, Java, JavaScript, Go, D, OCaml, Octave, Scilab and Scheme.
Instead of using qfTimeField.getValue()
on the time field, use qfTimeField.getString()
, and then just strptime()
the resulting string. For example:
qfSendingTime = fix.SendingTime()
message.getHeader().getField(qfSendingTime)
my_datetime = datetime.strptime(qfSendingTime.getString(), '%Y%m%d-%H:%M:%S.%f')
Did you try obj.getYear()
? It appears from the documentation that UTCTimeStamp
derives from DateTime
, so you should be able to access methods of the parent class.
If that doesn't work, what kind of object do you get if you do newobj = obj.next()
? Did you try print obj.__doc__?
Edit: from http://www.swig.org/Doc1.3/Python.html#Python_nn27a:
This pointer value can be freely passed around to different C functions that expect to receive an object of type ... The only thing you can't do is dereference the pointer from Python.
So you need to pass it to a wrapper for a C++ function that can take a DateTime
. I don't know specifically what that is, as I don't know what you're wrapping.
In this case, that is http://www.quickfixengine.org/quickfix/doc/html/struct_f_i_x_1_1_utc_time_stamp_convertor.html. I believe, although I can't test this, that you call it with:
import quickfix
converter = quickfix.UtcTimeStampConverter()
string = converter.convert(obj)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With