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