It seems pretty easy to calculate from the get-go that the formula is something like this:
# a halachic hour is one twelfth of the time between sunrise and sunset
halachicHour = (timeOfSunset - timeOfSunrise) / 12
# thus chatzos halayla would be literally
chatzosHalayla = timeOfSunset + (halachicHour * 6)
Above, a halachic hour is determined by the distance from sunrise to sunset divided by twelve.
This makes things interesting, as I presume that often "midnight" doesn't exactly happen in the middle of the night (period between nightfall and sunrise or dawn). How, then, do we calculate Chatzos Halayla?
I was reading a book on Tikkun Chatzos recently which sparked my interest in this. According to the author, Rebbe Nachman of Breslov calculated the middle of the night by adding six civil hours to nightfall, but this doesn't make any sense and I believe that the author is making an assumption about this. It would make sense to say that Rebbe Nachman calculated the middle of the night using six halachic hours, but to use six civil hours doesn't really make any sense to me.