String (in ISO8601 Zulu time) to DateTimeType (local time zone)

When I did String -> Epoch -> String -> DateTimeType, it works as expected.

def test(event):
    date = "2019-12-19T00:00:00.000Z" 

    direct = DateTimeType(date)"direct: " + direct.toString()) 

    epoch = DateTime(date).millis
    fromepoch = DateTimeType(DateTime(epoch).toString())"indirect: " + fromepoch.toString()) 

The result:
direct: 2019-12-19T00:00:00.000+0000
indirect: 2019-12-19T10:00:00.000+1000

Why is it different? I would expect that the direct one would give me the local timezone i.e. the same as indirect.

To rephrase the question, how do I turn the string into a DateTimeType which will print out (toString) in my local timezone, regardless of the input string’s timezone?

Take a look at what you are passing to the constructor. This may help…

from org.joda.time import DateTime

from core.log import logging, LOG_PREFIX

LOG = logging.getLogger("{}.TEST".format(LOG_PREFIX))

test_date = "2019-12-19T00:00:00.000Z" 

LOG.warn("test_date: {}".format(test_date))
LOG.warn("DateTimeType(test_date): {}, getOffset(): {}".format(DateTimeType(test_date), DateTimeType(test_date).zonedDateTime.getOffset()))
LOG.warn("DateTime(test_date): {}".format(DateTime(test_date)))
LOG.warn("DateTimeType(DateTime(test_date).toString()): {}, getOffset(): {}".format(DateTimeType(DateTime(test_date).toString()), DateTimeType(DateTime(test_date).toString()).zonedDateTime.getOffset()))

Which logs…

2019-12-22 15:40:55.689 [WARN ] [jsr223.jython.TEST] - test_date: 2019-12-19T00:00:00.000Z
2019-12-22 15:40:55.690 [WARN ] [jsr223.jython.TEST] - DateTimeType(test_date): 2019-12-19T00:00:00.000+0000, getOffset(): Z
2019-12-22 15:40:55.690 [WARN ] [jsr223.jython.TEST] - 
2019-12-22 15:40:55.690 [WARN ] [jsr223.jython.TEST] - DateTime(test_date): 2019-12-18T19:00:00.000-05:00
2019-12-22 15:40:55.691 [WARN ] [jsr223.jython.TEST] - DateTimeType(DateTime(test_date).toString()): 2019-12-18T19:00:00.000-0500, getOffset(): -05:00

You are being provided exactly what you asked for. If you need to convert a DateTimeType to another DateTimeType in another timezone, you can do something like…

from java.time import ZoneId

from core.log import logging, LOG_PREFIX
LOG = logging.getLogger("{}.TEST".format(LOG_PREFIX))

test_date = "2019-12-19T00:00:00.000Z" 

direct = DateTimeType(test_date)

direct_local_zoneid_of = DateTimeType(DateTimeType(test_date).zonedDateTime.withZoneSameInstant(ZoneId.of("America/New_York")))
LOG.warn("direct: {}, direct_local_zoneid_of: {}".format(direct, direct_local_zoneid_of))

direct_local_system_default = DateTimeType(DateTimeType(test_date).zonedDateTime.withZoneSameInstant(ZoneId.systemDefault()))
LOG.warn("direct: {}, direct_local_system_default: {}".format(direct, direct_local_system_default))

We need more documentation for using Java classes in DateTime conversions!

1 Like