5

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?

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359
  • Did you figure out what c function in your library could take the timestamp pointer from you? – agf Jul 23 '11 at 16:56
  • no I'm still a bit at a loss on how to continue... – Jonathan Livni Jul 23 '11 at 18:44
  • Once you `import quickfix` you should be able to find the right way to call by looking at `quickfix.__dict__` then the `__dict__` of the class (in case I got the capitalization wrong or something) in my edit. – agf Jul 23 '11 at 19:36

2 Answers2

4

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')
bavaza
  • 10,319
  • 10
  • 64
  • 103
  • This is a very good answer, and is what I do myself. However, you should be aware that `strptime` can fail in certain cases. This is a known bug in python involving multithreading, see http://stackoverflow.com/questions/16309650/python-importerror-for-strptime-in-spyder-for-windows-7 When this happens you can workaround by just calling `qfSendingTime.getString()` and forgetting about `strptime`. Then cast your date string into a different type when you need to use it for something – Wapiti Apr 02 '15 at 01:52
3

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)
agf
  • 171,228
  • 44
  • 289
  • 238