2

I have a Job variable last_updated of type DateTime. I'm trying to use it from a Python Script component, I thought it will show up as datetime.datetime object in python but it isn't :

print(latest_updated) # Sat Jan 01 00:00:00 UTC 2000
print(latest_updated.getClass()) # <type 'com.matillion.bi.emerald.server.scripting.MatillionDate'>

From what I see it seems to be a python bridge to some a Java's object of type MatillionDate.

How can I convert this to a regular datetime.datetime object?

RubenLaguna
  • 21,435
  • 13
  • 113
  • 151

1 Answers1

1

It depends on what python interpreter you use:

Jython:

If you use Jython, then the Job variable of type DateTime are, as mentioned, instances of MatillionDate and not datetime. You can convert it back to datetime.datetime, by converting it to a Java's Instant, getting the ISO 8601 representation and parsing that:

from dateutil.parser import parse
from dateutil.tz import tzutc

as_iso_string = str(latest_updated.toInstant()) # 2000-01-01T00:00:00Z
asdt = parse(as_iso_string) # datetime.datetime(2000, 1, 1, 0, 0, tzinfo=tzlocal())) tzinfo should be tzutc() not tzlocal()
asdt = parse(as_iso_string).replace(tzinfo=None) # naive datetime
asdt = parse(as_iso_string).replace(tzinfo=tzutc()) # datetime.datetime(2000, 1, 1, 0, 0, tzinfo=tzutc())

Make sure that you get the timezone right, if your Job variable was meant to be in UTC you will need to do .replace(tzinfo=tzutc()) as for some reason dateutil.parser.parse() in Jython is not parsing the Z as timezone UTC (it does in regular python 2.7.12)

Python 2 / Python 3:

The variable would have the right type already datetime.datetime. No timezone

print(repr(latest_updated)) # datetime.datetime(2000, 1, 1, 0, 0)
RubenLaguna
  • 21,435
  • 13
  • 113
  • 151