If my schedule is not for users, can I get whether the current time is schedule time

I used schedule manager to create some shifts, and I bound the relationship between production line and scheduling in my own database. At this time, can I get whether the current time is working time according to the shift name bound by a production line?

If you’re using Ignition 7.9.7+, you can create a dummy user, and call the system.user.isUserScheduled function.

If you use an earlier version, or if you want to work with schedules directly, you need to implement some schedule handling yourself.

In the past, I made this function to work with schedules (puzzled it together from some forum posts and some introspection). It’s a bit ugly with all the different date/time datatypes, but it works for me.

import java.util.Date
import java.util.Calendar
import datetime
  
def isScheduleActive(scheduleName):
	now = java.util.Date()
	cal = java.util.Calendar.getInstance();  
	cal.setTime(now); 
	
	schedule = system.user.getSchedule(scheduleName)
	
	if schedule.observeHolidays:
		holidays = system.user.getHolidays()
		for holiday in holidays:
			if system.date.midnight(holiday.getDate()) == system.date.midnight(system.date.now()):
				return False # today is a holiday
	
	timeline = schedule.getScheduleForDay(cal)
	if timeline == None:
		return False # today has no schedule, this means a day off
	
	dtNow = datetime.datetime.now()
	midnight = dtNow.replace(hour=0, minute=0, second=0, microsecond=0)
	msTime = (dtNow - midnight).seconds * 1000
	
	# get the segment based on the milliseconds since midnight, if the segment exists, the schedule is active
	return timeline.getSegment(msTime, False) != None 
2 Likes

Thank you for your support. Does the schedule.getscheduleforday (CAL) function here have a knowledge base? For example, I want to know the function after ‘schedule.’ where can I find it?

When you go to https://docs.inductiveautomation.com/display/DOC79/system.user.getSchedule, you find links to the JAVA API documentation.

http://files.inductiveautomation.com/sdk/javadoc/ignition79/790-beta1/com/inductiveautomation/ignition/common/user/schedule/AbstractScheduleModel.html

There, getScheduleForDay is mentioned, so the main task is getting a JAVA Calendar object for the current day, and then figuring out how to get something out of that timeline it returns.



image
system.user.isUserScheduled(user,date) This function doesn’t seem to distinguish between weekends.

Hmm, that’s strange. Do other times work fine?

I think your code will only work if the timeline variable’s style is Timeline.TimelineStyle.TimeOfDay. I don’t know the exact circumstances over when the other might be used, but there’s also TimelineStyle.Calandar [sic]. The latter might require actual calendar offsets to compare against.

The style object (just getStyle() on timeline) has a toLongForStyle() that might replace your datetime() calls:

        public long toLongForStyle(long dateMillis) {
            if (this == Calandar) {
                return dateMillis;
            } else {
                Calendar c = Calendar.getInstance();
                c.setTimeInMillis(dateMillis);
                return (long) (c.get(Calendar.HOUR_OF_DAY) * 60 + c.get(Calendar.MINUTE)) * 60000 + (
                    c.get(Calendar.SECOND) * 1000) + c.get(Calendar.MILLISECOND);
            }
        }

We have always had this problem in version 7.9.9, including the “isscheduleactive (schedulename)” method you created. Do I have any solutions?