# Total Minutes from Weekly Schedule

**URL:** <https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151>\
**Category:** Ignition\
**Tags:** ignition81, scripting\
**Created:** [July 12, 2024, 3:44pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151 "2024-07-12T15:44:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 12, 2024, 3:44pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/1 "2024-07-12T15:44:09Z")

</div>

I'm trying to determine the total number of minutes scheduled from a schedule, but I haven't figured out how to do it.

One problem I've run into, is that I might have hours loaded for Friday, but how do I determine if the day is enabled or not? `schedule.fridayTime` returns the hours string even though the day is not selected, and I don't see any methods to check on whether a day is enabled or not.

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

```python
from java.util import Calendar

now = system.date.now()
midnight = system.date.midnight(now)

cal = Calendar.getInstance()  
cal.setTime(now)

minutes = 0
users = ['p4_router_Shift%s' % x for x in xrange(1,2)]
for user in users:
	print user
	schedule = system.user.getSchedule(user)
	if not schedule:
		continue
	
	print 'Friday: %s' % schedule.fridayTime
	
	timeline = schedule.getScheduleForDay(cal)
	print 'Timeline: %s' % timeline

```

```python
>>>
p4_router_Shift1
Friday: 7:00-15:00
Timeline: None

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 12, 2024, 3:53pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/2 "2024-07-12T15:53:06Z")

</div>

rubber ducked it 😛

Somehow I missed `isFriday()`

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 12, 2024, 5:39pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/3 "2024-07-12T17:39:00Z")

</div>

Run into another point of confusion. How is the timeline 2am to 10 am, 1970?

```python
from java.util import Calendar

now = system.date.now()
yesterday = system.date.addHours(now, -1)
cal = Calendar.getInstance()  
cal.setTime(yesterday)

print 'Calendar Time: %s' % cal.getTime()
print 'Thursday string: %s' % schedule.thursdayTime

timeline = schedule.getScheduleForDay(cal)
print 'Timeline: %s' % timeline
print 'Timeline Style: %s' % timeline.getStyle()

```

```python
>>>
Calendar Time: Thu Jul 11 13:37:18 EDT 2024
Thursday string: 7:00-15:00
Timeline: [Thu Jan 01 02:00:00 EST 1970 -> Thu Jan 01 10:00:00 EST 1970]
Timeline Style: TimeOfDay

```

---

<div class="post-metadata">

**Author:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 12, 2024, 5:52pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/4 "2024-07-12T17:52:38Z")

</div>

Hmm, not sure why, but this gets me to the right answer, but not sure about the weird timeline.

```python
segments = timeline.getSegments()
style = timeline.getStyle()
	
print 'Calendar Time: %s' % cal.getTime()
	
timeline = schedule.getScheduleForDay(cal)
print 'Thursday string: %s' % schedule.thursdayTime
print 'Timeline: %s' % segments
print 'Timeline Style: %s' % style
	
	
# Grab first segment and get start and end time.
starting = segments[0].getStart()
print 'starting: %s' % starting
ending = segments[0].getEnd()
print 'ending: %s' % ending
	
	
start = style.toDateForStyle(starting)
print 'start: %s' % start
end = style.toDateForStyle(ending)
print 'end: %s' % end

```

```python
>>>
Calendar Time: Thu Jul 11 13:51:05 EDT 2024
Thursday string: 7:00-15:00
Timeline: [Thu Jan 01 02:00:00 EST 1970 -> Thu Jan 01 10:00:00 EST 1970]
Timeline Style: TimeOfDay
starting: 25200000
ending: 54000000
start: Fri Jul 12 07:00:00 EDT 2024
end: Fri Jul 12 15:00:00 EDT 2024

```

---

<div class="post-metadata">

**Author:** ![Mark\_Minuth](https://avatars.discourse-cdn.com/v4/letter/m/d78d45/32.png) [@Mark\_Minuth](https://forum.inductiveautomation.com/u/Mark_Minuth)\
**Post date:** [July 12, 2024, 6:02pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/5 "2024-07-12T18:02:32Z")

</div>

My guess is timeline is strictly assuming the times are within a single already stored day, so they're storing the times in milliseconds from the start of the day. (25200000 = 7 hours of milliseconds, 54000000 = 15 hours of milliseconds).  
When trying to display just those stored times as an actual date, it's treating them like an actual stored date, which is just milliseconds after the general computing epoch time (Jan 1st. 1970 UTC), then converting that time to EST for you.

---

<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:** [July 12, 2024, 6:04pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/6 "2024-07-12T18:04:00Z")

</div>

Possibly some inspiration in this old thread:

> [@If my schedule is not for users, can I get whether the current time is schedule time](https://forum.inductiveautomation.com/t/if-my-schedule-is-not-for-users-can-i-get-whether-the-current-time-is-schedule-time/33153):
>
> 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:** ![dkhayes117](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/dkhayes117/32/68344_2.png) [@dkhayes117](https://forum.inductiveautomation.com/u/dkhayes117)\
**Post date:** [July 12, 2024, 6:56pm UTC](https://forum.inductiveautomation.com/t/total-minutes-from-weekly-schedule/93151/7 "2024-07-12T18:56:38Z")

</div>

Here is what I came up with as a starting point that works.

```python
# Create a java.util Calendar instance for the first day of the week (Sunday) 
now = system.date.now()

dayOfWeek = system.date.getDayOfWeek(now) # 1->Sunday, 7->Saturday
offset = 1 - dayOfWeek
sunday = system.date.addDays(now, offset)
	
cal = Calendar.getInstance()  
cal.setTime(sunday)
	
# define total minutes
minutes = 0
	
# Get the Schedule object
schedule = system.user.getSchedule(user)
if not schedule:
	return minutes
	
# Check each day for a schedule
for i in xrange(0,7):
	if i:
		next = system.date.addDays(sunday, i)
		cal.setTime(next)
	
	timeline = schedule.getScheduleForDay(cal)
	if not timeline:
		continue
	
	segments = timeline.getSegments()
	
	# Loop over all segments
	for segment in segments:
		# getDuration() returns millis
		minutes += int(segment.getDuration() / 60000)
		
print 'Shift: %s -> %s minutes' % (user, minutes)
	
return minutes

```
