Logo Search packages:      
Sourcecode: libical version File versions

int* icalspanlist_as_freebusy_matrix ( icalspanlist *  sl,
int  delta_t 
)

Return an integer matrix of total events per delta_t timespan.

Return an integer matrix of total events per delta_t timespan.

Parameters:
sl A valid icalspanlist
delta_t The time slice to divide by, in seconds. Default 3600.
Returns:
A pointer to an array of integers containing the number of busy events in each delta_t time period. The final entry contains the value -1.
This calculation is somewhat tricky. This is due to the fact that the time range contains the start time, but does not contain the end time. To perform a proper calculation we subtract one second off the end times to get a true containing time.

Also note that if you supplying a spanlist that does not start or end on a time boundary divisible by delta_t you may get results that are not quite what you expect.

calculate the start and end time as time_t

insure that the time period falls on a time boundary divisable by delta_t

find the duration of this spanlist

malloc our matrix, add one extra slot for a final -1

Definition at line 372 of file icalspanlist.c.

References icaltime_span::end, icaltimezone_get_utc_timezone(), icaltime_span::is_busy, and icaltime_span::start.

                                                                    {
  pvl_elem itr;
  int spanduration_secs;
  int *matrix;
  int matrix_slots;
  time_t sl_start, sl_end;

  icalerror_check_arg_rz( (sl!=0), "spanlist");

  if (!delta_t)
    delta_t = 3600;

  /** calculate the start and end time as time_t **/
  sl_start = icaltime_as_timet_with_zone(sl->start, icaltimezone_get_utc_timezone());
  sl_end   = icaltime_as_timet_with_zone(sl->end, icaltimezone_get_utc_timezone());


  /** insure that the time period falls on a time boundary divisable
      by delta_t */

  sl_start /= delta_t;
  sl_start *= delta_t;

  sl_end /= delta_t;
  sl_end *= delta_t;


  /** find the duration of this spanlist **/
  spanduration_secs = sl_end - sl_start;


  /** malloc our matrix, add one extra slot for a final -1 **/
  matrix_slots = spanduration_secs/delta_t + 1;

  matrix = (int*) malloc(sizeof(int) * matrix_slots);
  if (matrix == NULL) {
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    return NULL;
  }
  memset(matrix, 0, sizeof(int) * matrix_slots);
  matrix[matrix_slots-1] = -1;

  /* loop through each span and mark the slots in the array */

  for( itr = pvl_head(sl->spans);  itr != 0;  itr = pvl_next(itr)) {
    struct icaltime_span *s = (struct icaltime_span*)pvl_data(itr);
    
    if (s->is_busy == 1) {
      int offset_start = s->start/delta_t - sl_start/delta_t;
      int offset_end   = (s->end - 1) /delta_t  - sl_start/delta_t + 1;
      int i;
      
      if (offset_end >= matrix_slots)
      offset_end = matrix_slots - 1;

      i = offset_start;
      for (i=offset_start; i < offset_end; i++) {
      matrix[i]++;
      }
    }
  }
  return matrix;
}


Generated by  Doxygen 1.6.0   Back to index