import calendar


def datetime_to_unix(timestamp, milliseconds=False):
  """Converts a datetime object to a unix timestamp.

  Args:
    timestamp: A datetime object.
    milliseconds: Bool, default False, return result in milliseconds instead.

  Returns:
    An integer of unit timestamp in seconds.
  """
  if milliseconds:
    return int(calendar.timegm(timestamp.timetuple()) * 1000)
  else:
    return int(calendar.timegm(timestamp.timetuple()))