from datetime import datetime


def format_date(
	date: datetime, base: datetime = datetime.now(), all_day: bool = False
) -> str:
	"""Convert dates to a specified format
	Arguments:
	<date>: The date to format
	[base]: When the date or time matches the info from base, it will be skipped.
			This helps avoid repeated info when formatting time ranges.
	[all_day]: If set to true, the time of the day will not be included
	"""
	date_format = ""
	# include the date if the date is different from the base
	if date.date() != base.date():
		# %a = Weekday (eg. "Mon"), %d = Day (eg. "01"), %b = Month (eg. "Sep")
		date_format = "%a %d %b"
		# include the year if the date is in a different year
		if date.year != base.year:
			# %Y = Year (eg. "2021")
			date_format += " %Y"
	# include the time if it is not an all day event and not the same as the base
	if not all_day and date != base:
		# %I = Hours (12-hour clock), %M = Minutes, %p = AM or PM
		date_format += " %I:%M %p"
	# format the date and remove leading zeros and trailing spaces
	return date.strftime(date_format).replace(" 0", " ").strip()