Total Minutes from Weekly Schedule

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.

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
>>>
p4_router_Shift1
Friday: 7:00-15:00
Timeline: None

rubber ducked it :stuck_out_tongue:

Somehow I missed isFriday()

1 Like

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

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()
>>>
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

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

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

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.

1 Like

Possibly some inspiration in this old thread:

1 Like

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

# 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
1 Like