How does 子 come to mean 'midnight'?
It is indeed related to the terrestrial (earthly) branches, and in a 24-hour day,「子時」represents the time of day between 11:00 PM to 1:00 AM.「子正」, as noted in the question, specifically represents midnight (12:00 AM).

Unfortunately, there is no convenient mnemonic or etymology that you can use to make a connection between「子」and midnight, but I doubt that midnight shows up very often anyway as a meaning (unless you're into traditional Korean timekeeping?).