Gateway Event - Time Date, add day for tomorrow

I am trying to add a day when the shifts turns tomorrow. The current system has people put in the start and end time in a string format. I have it working up until I use timedelta(days=1).

HSGLHourEndTime = HSGLHourEndTime + datetime.timedelta(days=1)

If I try to the system.date.addDays, I get a java utility error.

MilitaryStartTime = system.tag.readBlocking('[default]353/Shift Times/Second/10 Hours/Hour 8/Start Time')[0]
MilitaryStartTime = int(MilitaryStartTime.value)
MilitaryStartHour = (MilitaryStartTime/100)
MilitaryStartMinutes = (MilitaryStartTime - (MilitaryStartHour*100))
HSGLHourStartTime = datetime_object.replace(hour = MilitaryStartHour, minute = MilitaryStartMinutes, second = 0)
#HSGLHourStartTime = HSGLHourStartTime.strftime("%Y-%m-%d %H:%M:%S")
#print MilitaryStartTime
#Set our End Times
MilitaryEndTime = system.tag.readBlocking('[default]353/Shift Times/Second/10 Hours/Hour 8/End Time')[0]
MilitaryEndTime = int(MilitaryEndTime.value)
MilitaryEndHour = (MilitaryEndTime/100)
MilitaryEndMinutes = (MilitaryEndTime - (MilitaryEndHour*100))
HSGLHourEndTime = datetime_object.replace(hour = MilitaryEndHour, minute = MilitaryEndMinutes, second = 0)
if HSGLHourEndTime < HSGLHourStartTime:
	HSGLHourEndTime = HSGLHourEndTime + datetime.timedelta(days=1)
HSGLHourStartTime = HSGLHourStartTime.strftime("%Y-%m-%d %H:%M:%S")	
HSGLHourEndTime = HSGLHourEndTime.strftime("%Y-%m-%d %H:%M:%S")
StartTime = str(HSGLHourStartTime)
EndTime = str(HSGLHourEndTime)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)
system.tag.writeBlocking('[default]353/KPI/Second Shift/Hour 8 Tank Counts', data)

I am trying to add a day when the shifts turns tomorrow.

Can you explain this more clearly? How does a shift "turn tomorrow"?

The current system has people put in the start and end time in a string format.

Why is it not entered in a date format? You should do everything date related in datetime data types and format it at the last possible moment prior to display.

Ignition has built-in system.date - Ignition User Manual 8.1 - Ignition Documentation functions which should be able to do everything you want.

If you must use a string input, what is its format? You haven't given an example.

1 Like

And do not use jython's datetime module. At all. Use Ignition's provided system.date.* functions, or native java classes/methods that yield proper java.util.Date objects.

2 Likes

I would say too you don't even have to format in the tag. When you're displaying it via a binding or what have you, if its a component made to work with dates you have options there to modify how it looks, and you always have dateFormat - Ignition User Manual 8.1 - Ignition Documentation as a fallback.

Leave how you present the data on the presentation layer ie the vision window or perspective view.

One other benefit of using the system.date and java date objects is I se you are doing this

StartTime = str(HSGLHourStartTime)
EndTime = str(HSGLHourEndTime)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)

As long as you use system.date.* to make and use your dates, and then in your Named Queries define your parameters as DateTime objects, you do not have to worry about manually handling the stringing of the dates in SQL (thank god!). If you have made StartTime/EndTime as datetime parameters you could instead hypothetically do something like

EndTime = system.date.now()
StartTime = system.date.addHours(EndTime, -8)
parameters = {"StartTime":StartTime, "EndTime":EndTime, "Department":"HS GAS"}
data = system.db.runNamedQuery('Oracle/Hour Count', parameters)

And this would work for you.

1 Like

Shift can be any set of hours the employees work. One Assembly line starts at 4:30pm and goes to 2:30pm. They would like to see hour counts between 4:30 and 5:30...
When you hit 11:30pm and go to 12:30am, we are into the next day within that hour/shift. Customer is always right :slight_smile:

Unfortunately, this is Oracle and it did not like the datetime. I agree with SQL being much easier to utilize the DateTime format.
I could only get the queries to work from strings. I will give this a shot along with system.date, I could always just my shift UDT to give a full date and stop this date manipulation in the script.

I appreciate the help and the suggestions!

This is a problem. You should be passing value parameters.

Show your named query.

1 Like