def getHrsInDayTime(self, start, end): "Split up given time range by PTCS day and night time hours." dur = TimeAgent.dtDiffHrs(start, end) startDate = date(start.year, start.month, start.day) #rise, set = self.sun.getRiseSet(date1) # cast a wide net: compute the rise and set times for any days # that might be covered by the given time range days = (end - start).days + 2 dayTimes = [] for day in range(days): dt = startDate + timedelta(days = day) dayTimes.append(self.sun.getPTCSRiseSet(dt)) # where does our given time range intersect with day time? ints = AnalogSet.intersects([dayTimes, [(start, end)]]) if len(ints) > 0: # some day time day = 0.0 for intersection in ints: td = intersection[1] - intersection[0] day += TimeAgent.timedelta2frachours(td) # the rest must be night time night = abs(dur - day) else: # our range is all night time. day = 0.0 night = dur return (day, night)
def get_sanctioned_users_blackout_times(self, start, end): """ A project is 'blacked out' when all of its sanctioned observers are unavailable or it is using Project Blackouts. User blackouts are much more complicated, thus this method. Returns a list of tuples describing the time ranges where the project is 'blacked out' by user blackouts in UTC. """ if not self.has_sanctioned_observers(): return [] blackouts = [o.user.blackout_set.all() \ for o in self.get_sanctioned_observers()] # Change all to UTC. utcBlackouts = [] for set in blackouts: utc = [] for b in set: utc.extend(b.generateDates(start, end)) utcBlackouts.append(utc) if len(utcBlackouts) == 1: # One observer runs the show. return sorted(utcBlackouts[0]) return AnalogSet.unions(AnalogSet.intersects(utcBlackouts))
def get_users_blackout_times(self, start, end): """ Returns the intersection of all users blackout times. """ sanctioned = self.get_sanctioned_users_blackout_times(start, end) unsanctioned = self.get_unsanctioned_users_blackout_times(start, end) return AnalogSet.intersects([sanctioned, unsanctioned])
def get_unsanctioned_users_blackout_times(self, start, end): universe = (start, end) all_blackout_ranges = [[(start, end)]] # for all un-sanctioned observers one_day = timedelta(days=1) for o in self.investigator_set \ .exclude(user__sanctioned=True) \ .exclude(observer=False): # Get reservation times reservation_sets = \ Reservation.objects.filter(user=o) \ .exclude(start_date__gte=end) \ .exclude(end_date__lte=start) # add a day to end_date (check-out day) because we # assume they are available all that day just like # they are available all day on check-in day onsite_ranges = [(rs.start_date, rs.end_date + one_day) for rs in reservation_sets] # Get black-out ranges blackout_ranges = [] for b in o.user.blackout_set.all(): blackout_ranges.extend(b.generateDates(start, end)) # Get available ranges available_ranges = AnalogSet.diffs(onsite_ranges, blackout_ranges) # Get this observer's blackout times all_blackout_ranges.append( AnalogSet.diffs([universe], available_ranges)) return AnalogSet.intersects(all_blackout_ranges)