Not understanding difference between system.date.now() and datetime.datetime.now()

I'm trying to modify an existing script that has the following date manipulations:

now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

And I need to add code that will allow me to print out each of the day numbers for the entire month between the 'start' date and the 'end' date, along with doing some database manipulations for each date.

There seems to be plenty of sample code out there that shows how I could start with a date and step through the days like I want to do. But I'm having trouble getting it to work with those first three lines above.

For example the following works fine with 'd' set to datetime.datetime.now() as it is below:

"""Script to add days to a time returned by the system"""

import datetime
from datetime import date, timedelta
from time import gmtime

now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

print now
print start
print end

#get current time
d = datetime.datetime.now()
#d=start

#print date
print(d)

#get the day of month
print(d.strftime("%d"))

modified_date = d + timedelta(days=1)
print(modified_date.strftime("%d"))

But when I comment out the 'd = datetime.datetime.now() and uncomment the d=start so that I can use the date range from the existing script, I get the following error:

Traceback (most recent call last):
  File "<input>", line 23, in <module>
AttributeError: 'java.util.Date' object has no attribute 'strftime'

Where line 23 is the 'print(d.strftime("%d"))' line.

I'm thinking this is a simple conversion issue between the java.util.Date format to datetime, but I'm not seeing how to make that conversion.

Suggestions and an 'education' on the topic would be appreciated.

Many thanks in advance!

My recommendation would be to stick with using system.date.* unless there is some more advanced functionality that you need from java.util.Date or datetime

now = system.date.now()
end = system.date.midnight(system.date.addDays(now, -system.date.getDayOfMonth(now)+1)) # 1st of this month
start = system.date.addMonths(end, -1) # 1 month prior to end

print now
print start
print end

#get current time
d = start

#print date
print(d)

#get the day of month
print(system.date.format(d, "d"))

modified_date = system.date.addDays(d, 1)
print(system.date.format(modified_date, "d"))

# Alternative for getting the day of the month, just note that it returns an int rather than a str
print(system.date.getDayOfMonth(d)) 
1 Like

One is jython/python. The other is java.

Use java objects and classes in Ignition for best results. Yes, converting from python code you found on the internet is harder this way, but jython has a bunch of pitfalls. And it runs slower.

2 Likes

I wasn't understanding that one was Java, and the other was jython, and that I wanted to stick with the Java. That's the nudge I needed.

Thanks to both of you!

I see this mentioned a lot! Is there a cheatsheet/list of jython Vs java equivalents somewhere?

I.e X in jython is better used as Y in java?

1 Like

My hot take is to avoid the Jython standard library completely. I'm pretty sure Phil agrees with me.

If it's not available as a system.* function, there's hopefully a Java dependency available you can pull in. If not, I would literally write a module to bring in a Java lib before I relied on the Jython stdlib for anything non-trivial.

2 Likes

Also agree, unfortunately...

I do. I hardly even use sys and traceback any more, since the error-handling functions I care about are embedded in later.py. (via PythonAsJavaException)