## This function is called in the while loop each minute

def get_formatted_time():
	"""
	Returns the current time formatted as:
	mmm-dd-yyyy \n hh:mm am/pm
	Example: Sep-15-2025 \n 10:15 pm
	"""
	t = time.localtime()
	months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
	month_str = months[t.tm_mon - 1]
	day = t.tm_mday
	year = t.tm_year
	hour = t.tm_hour
	minute = t.tm_min
	am_pm = "am" if hour < 12 else "pm"
	hour12 = hour % 12
	if hour12 == 0:
		hour12 = 12
	formatted = f"{hour12}:{minute:02d} {am_pm}"
    
	if minute == 15: # Detects the quarter hour and runs the function 'qhr()' which handles playing Westminster Quarters and then announces it is the quarter hour
		print("Quarter Hour")
		qhr()
	elif minute == 30:
			print("Half Hour") # Same as quarter hour
			hhr()
	elif minute == 45:
			print("Three Quarter Hour") # Same as quarter hour
			tqhr()
	elif minute == 0:
			print("Top of the Hour") # Calls the full hour function to sound the Quarters, ring the bell and announce the time
			fhr()
	else:
		pass
	return formatted

## This function is called when the top of the hour is reached

def fhr():
	print("On the Hour")
	
	# Announce the time with voice
	announce_hour()
	
	# Restart the mixer/synth after voice announcements
	jam.peripherals.audio.play(mixer)
	mixer.voice[0].play(synth)
	mixer.voice[1].play(chorus)
	
	print("Finished announcing hour, now playing tones.")
	for i in wc4:
		time.sleep(0.01)
		n = wcn[i][0]
		t = wcn[i][1]
		note = synthio.Note(synthio.midi_to_hz(n), bend=lfo)
		synth.press(note)
		time.sleep(t)
		synth.release(note)
		if i in (1, 3):
			time.sleep(0.5)

	print("Finished playing tones for hour.")
	
	# Play the gong for the hour
	t = time.localtime()
	hour = t.tm_hour
	gong(hour if hour < 13 else hour - 12)