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

**URL:** <https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [March 17, 2020, 3:10am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153 "2020-03-17T03:10:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent.hu](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@vincent.hu](https://forum.inductiveautomation.com/u/vincent.hu)\
**Post date:** [March 17, 2020, 3:10am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/1 "2020-03-17T03:10:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [March 17, 2020, 8:35am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/2 "2020-03-17T08:35:47Z")

</div>

If you’re using Ignition 7.9.7+, you can create a dummy user, and call the [`system.user.isUserScheduled`](https://docs.inductiveautomation.com/display/DOC79/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.

```auto
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 

```

---

<div class="post-metadata">

**Author:** ![vincent.hu](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@vincent.hu](https://forum.inductiveautomation.com/u/vincent.hu)\
**Post date:** [March 19, 2020, 4:12am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/3 "2020-03-19T04:12:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [March 19, 2020, 9:05am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/4 "2020-03-19T09:05:14Z")

</div>

When you go to [https://docs.inductiveautomation.com/display/DOC79/system.user.getSchedule](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](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.

---

<div class="post-metadata">

**Author:** ![vincent.hu](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@vincent.hu](https://forum.inductiveautomation.com/u/vincent.hu)\
**Post date:** [March 20, 2020, 10:46am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/5 "2020-03-20T10:46:08Z")

</div>

![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/c/c6a79422edf5724c0abe4b2a822118ea958b8f22.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/e/e962ff029d2c5222611cf613e1e22605a3ce2858.png)  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/2/2f5d5eea40fc84ec19ccfd50f71120bfa07a8d9b.png)  
system.user.isUserScheduled(user,date) This function doesn’t seem to distinguish between weekends.

---

<div class="post-metadata">

**Author:** ![Sanderd17](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/sanderd17/32/8735_2.png) [@Sanderd17](https://forum.inductiveautomation.com/u/Sanderd17)\
**Post date:** [March 20, 2020, 10:50am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/6 "2020-03-20T10:50:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [March 20, 2020, 3:11pm UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/7 "2020-03-20T15:11:45Z")

</div>

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:

```java
        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);
            }
        }

```

---

<div class="post-metadata">

**Author:** ![vincent.hu](https://avatars.discourse-cdn.com/v4/letter/v/5e9695/32.png) [@vincent.hu](https://forum.inductiveautomation.com/u/vincent.hu)\
**Post date:** [April 7, 2020, 2:14am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/8 "2020-04-07T02:14:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [December 15, 2023, 7:21pm UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/9 "2023-12-15T19:21:35Z")

</div>

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

```auto
	
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 -

```auto
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.

---

<div class="post-metadata">

**Author:** ![schenk](https://avatars.discourse-cdn.com/v4/letter/s/6bbea6/32.png) [@schenk](https://forum.inductiveautomation.com/u/schenk)\
**Post date:** [December 15, 2023, 9:46pm UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/10 "2023-12-15T21:46:47Z")

</div>

You haven't initialized the date, so it is setting the value of [java.util.Date()](https://docs.oracle.com/javase/8/docs/api/java/util/Date.html) to midnight on Jan 01, 1970 GMT, or 0 ms after the epoch.

Try this instead

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

	...

```

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [December 18, 2023, 1:30pm UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/11 "2023-12-18T13:30:07Z")

</div>

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

```auto
	
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)

```

```auto
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

```

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [December 19, 2023, 4:44am UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/12 "2023-12-19T04:44:26Z")

</div>

> [@kaushik.shah](#):
>
> `import java`

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:

```auto
cal = Calendar.getInstance()

```

---

<div class="post-metadata">

**Author:** ![kaushik.shah](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@kaushik.shah](https://forum.inductiveautomation.com/u/kaushik.shah)\
**Post date:** [December 19, 2023, 9:06pm UTC](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153/13 "2023-12-19T21:06:39Z")

</div>

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

 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/a/9/a90f6561c1079c83fa43bfd08070e66ad169cbf6.png)
