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?

I am trying to use getScheduleForDay function but seems like i am missing something.
with this code:

	
def printScheduleInfo(aSchedule):
    import java
    now = java.util.Date()
    cal = java.util.Calendar.getInstance()
    cal.setTime(now)
	
    if aSchedule.getType() == "basic schedule":
        print "Basic schedule type: ",aSchedule.getName(), aSchedule.getDescription(), aSchedule.isAllDays(), aSchedule.getWeekDayTime(), aSchedule.getScheduleForDay(cal)
    	timeline = schedule.getScheduleForDay(cal)
#    	print aSchedule.getWeekDayTime()
    	print  type(aSchedule.getWeekDayTime())
    	print 'Timeline: %s' % timeline
    
    elif aSchedule.getType() == "composite schedule":
        compositePieces = aSchedule.getModels()
        print "Composite schedule type:",aSchedule.getName(), aSchedule.getDescription(), " which is made up of..."
        for piece in compositePieces:
            printScheduleInfo(piece)
    else:
        print "Other schedule type: ", aSchedule.getName(), aSchedule.getDescription(), aSchedule.getType(), aSchedule.isObserveHolidays()
 
schedules = system.user.getSchedules()
for schedule in schedules:
    printScheduleInfo(schedule)
    

Its printing this -

Basic schedule type:  Afternoons None False 2:00 PM- 10:30 PM [Thu Jan 01 09:00:00 EST 1970 -> Thu Jan 01 17:30:00 EST 1970]
<type 'unicode'>
Timeline: [Thu Jan 01 09:00:00 EST 1970 -> Thu Jan 01 17:30:00 EST 1970]
Basic schedule type:  Always Built-in schedule that is always available: 24x7x365 True 0:00-24:00 [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 19:00:00 EST 1970]
<type 'unicode'>
Timeline: [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 19:00:00 EST 1970]
Basic schedule type:  Mornings None False 06:00 AM - 2:30 PM [Thu Jan 01 01:00:00 EST 1970 -> Thu Jan 01 09:30:00 EST 1970]
<type 'unicode'>
Timeline: [Thu Jan 01 01:00:00 EST 1970 -> Thu Jan 01 09:30:00 EST 1970]
Basic schedule type:  Nights None False 0:00-24:00 [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 01:30:00 EST 1970, Thu Jan 01 17:00:00 EST 1970 -> Thu Jan 01 19:00:00 EST 1970]
<type 'unicode'>
Timeline: [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 01:30:00 EST 1970, Thu Jan 01 17:00:00 EST 1970 -> Thu Jan 01 19:00:00 EST 1970]
Basic schedule type:  Weekend On Call None False 0:00-24:00 None
<type 'unicode'>
Timeline: None

Not sure why the dates are all over the place.

You haven't initialized the date, so it is setting the value of java.util.Date() to midnight on Jan 01, 1970 GMT, or 0 ms after the epoch.

Try this instead

def printScheduleInfo(aSchedule):
	from java.util import Calendar
	now = system.date.now() # returns a java.util.Date object
	cal = Calendar.getInstance()
	cal.setTime(now)

	...
1 Like

I changed to java date instance time but still shows same range:

	
def printScheduleInfo(aSchedule):
    import java
    now = system.date.now()
    cal = java.util.Calendar.getInstance()
    cal.setTime(now)
    print cal.getTime()
	
    if aSchedule.getType() == "basic schedule":
#        print "Basic schedule type: ",aSchedule.getName(), aSchedule.getDescription(), aSchedule.isAllDays(), aSchedule.getWeekDayTime(), aSchedule.getScheduleForDay(cal)
    	timeline = schedule.getScheduleForDay(cal)
#    	print aSchedule.getWeekDayTime()
    	print 'Timeline: %s' % timeline
    
    elif aSchedule.getType() == "composite schedule":
        compositePieces = aSchedule.getModels()
        print "Composite schedule type:",aSchedule.getName(), aSchedule.getDescription(), " which is made up of..."
        for piece in compositePieces:
            printScheduleInfo(piece)
    else:
        print "Other schedule type: ", aSchedule.getName(), aSchedule.getDescription(), aSchedule.getType(), aSchedule.isObserveHolidays()
 
schedules = system.user.getSchedules()
for schedule in schedules:
    printScheduleInfo(schedule)
Mon Dec 18 08:27:40 EST 2023
Timeline: [Thu Jan 01 09:00:00 EST 1970 -> Thu Jan 01 17:30:00 EST 1970]
Mon Dec 18 08:27:40 EST 2023
Timeline: [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 19:00:00 EST 1970]
Mon Dec 18 08:27:40 EST 2023
Timeline: [Thu Jan 01 01:00:00 EST 1970 -> Thu Jan 01 09:30:00 EST 1970]
Mon Dec 18 08:27:40 EST 2023
Timeline: [Wed Dec 31 19:00:00 EST 1969 -> Thu Jan 01 01:30:00 EST 1970, Thu Jan 01 17:00:00 EST 1970 -> Thu Jan 01 19:00:00 EST 1970]
Mon Dec 18 08:27:40 EST 2023
Timeline: None

I'm not where I can experiment with this, but I feel compelled to point out that your import should probably be more specific. I don't normally see a full Java import. I normally see it written from java.util import Calendar. Then, you can create your call variable like this:

cal = Calendar.getInstance()

Not sure how that would have made a difference but gave it a shot, still the same thing