def ConvertToInterval(timestamp):
  """Converts a datetime timestamp to a time interval for a cron job.

  Args:
    timestamp: a datetime.datetime object

  Returns:
    A value between 0 and 55 corresponding to the interval the timestamp
    falls in. In this calculation, 12am on Monday is interval 0 and each
    interval lasts 3 hours.
  """
  # In datetime, Monday is day 0, sunday is day 6
  # Since 8 intervals in a day, multiply by 8. Round up hours.
  interval = 8*timestamp.weekday() + timestamp.hour/3 + 1

  # If we are exactly on an hour that is a multiple of three
  # we do not wish to round up since floor(x) == ceil(x), contrary
  # to all other cases where ceil(x) == floor(x) + 1
  relative_seconds = sum([3600*(timestamp.hour % 3 == 0),
                          60*timestamp.minute,
                          timestamp.second,
                          timestamp.microsecond/1000.0])
  if relative_seconds < 300:  # under 5 minutes past schedule
    interval -= 1

  return interval % 56