User schedule shifts across midnight

I set up a schedule in the users schedules, and was able to create some tags such as shift, shift start, shift end, break active. Everything is working except with third shift which runs across midnight. I am checking to see if the current shift is a shift that runs across midnight so I can handle it differently in the script, but i am at a lost on how to handle it. the information that I am having issues with is the shift start time and end time. Below are the time line segments for third shift. The shift starts at 19:30 and ends at 6:00. I could hard code it to use certain segments, but that kind of defeats the purpose, due to possible shift changes in the future. Any help would be greatly appreciated.

0 Sun Feb 07 00:00:00 EST 2021
0 Sun Feb 07 00:10:00 EST 2021
1 Sun Feb 07 00:40:00 EST 2021
1 Sun Feb 07 03:20:00 EST 2021
2 Sun Feb 07 03:30:00 EST 2021
2 Sun Feb 07 06:00:00 EST 2021
3 Sun Feb 07 19:30:00 EST 2021
3 Sun Feb 07 21:50:00 EST 2021
4 Sun Feb 07 22:00:00 EST 2021
4 Mon Feb 08 00:00:00 EST 2021

Can you better explain what the data you presented represents? I’m a bit confused by it.

And also what is the explicit issue you are trying to resolve?

The data is the timeline segments from a schedule set up in the schedule management component, the first number is just representing which segment it is. For shifts that do not span midnight I can just look at the start of the first timeline segment for the shift start time and the end of the last timeline segment for the shift end time. With third shift the start time is in segment 3 and the end time is in segment 2. If I add more breaks those segments change. I did not know if there is a better way to get the shift start and end times. Or if there was a way to get the segments in the order they are in the schedule management component. Below is a screenshot of the schedule setup in the component and a script in the script console I was using to try to find a solution, in the script for midnight I just have it set to segment 2 and 3 right now.

shift = system.user.getSchedule(“Shift 3”)
timeline = shift.getScheduleForDay(cal)
if timeline <> None:
timelineSegments = timeline.getSegments()
style = timeline.getStyle()
x = len(timelineSegments)

midnightShift = False
for n in range(x):
	segStart = (style.toDateForStyle(timelineSegments[n].getStart()))
	segEnd = (style.toDateForStyle(timelineSegments[n].getEnd()))
	print n, segStart
	print n, segEnd
	if segStart == "00:00:00" or segEnd == "00:00:00":
		midnightShift = True

if midnightShift:
	startDate = style.toDateForStyle(timelineSegments[3].getStart())
	endDate = style.toDateForStyle(timelineSegments[2].getEnd())
	startDate = style.toDateForStyle(timelineSegments[0].getStart())
	endDate = style.toDateForStyle(timelineSegments[x - 1].getEnd())

Use a composite schedule called “Third shift” which combines 2 schedules. Third Shift Part A from 6pm - Midnight, Third Shift Part B from Midnight to 6 am. When you call “Third Shift” then you should be able to get your start/end times. Might take some scripting to do it. You can define the composite schedule in the gateway.

I think that the pain you are feeling with finding the end of the shift is because your segments represent work periods and do not represent shifts. To fix this I would start with a statement of a shift like:

  • A shift starts at a defined date/time and is of a set duration.
  • Consecutive shifts can not overlap in time.
  • Within a shift there can be any number of breaks.
  • A break is defined by a time offset from the start of a shift and a duration.
  • Consecutive breaks can not overlap and must have a minimum duration between them.
  • Breaks can not overlap the start and end times of a shift.
  • Different shifts can contain different numbers/durations of breaks

This gives you a well defined set of rules that allows you to easily find the end date/time of any shift. And I’d model it in SQL as a master/detail relationship (this is hinted at by the 1 to many relationship between a shift and its breaks), where the shift is the master and the breaks are the details, where:

Shift Table:

  1. ID Auto increment,
  2. Shift Type (A, B, C etc)
  3. Start datetime,
  4. Duration time

Breaks Table

  1. ID Auto increment
  2. ShiftID (points to ID of shift that this break relates to)
  3. Offset time (Offset from start of shift)
  4. Duration time (length of break)

Thus with some SQL joins (and some scripting) you can find the start and end of any shift and when breaks will occur in a shift. In reverse you can decompose your raw data entry into unambiguous SQL data that matches the Shift and Break tables.

Sorry for adding a month to your project :laughing: