Keep one's hair on indeed means
To stay calm or, to be patient.
History
As far as I know, it's a colloquial British English idiomatic expression, urging the other party not to lose their cool. However, it seems to have spread across the globe and is widely used across the US, Australia and other English speaking countries.
Acc. to Google Ngrams, the phrase first appears in 1868-69. To keep your shirt on has the same meaning and tone but doesn't seem to appear before 1870-71, according to Google Ngrams and in 1904 according to Etymonline.
NOTE: The expression "keep your hat on" predates them both to the year 1804.
Extended Explanation
(Disclaimer: This is some sheer guesswork, putting two and two together.)
The idiom seems to be constructed from that fact that one might lose their hair due to stress (check this), or even might pull it out in exasperation, anger or frustration.
The Ngram results definitely indicate that keep your hair on is currently more popular than keep your shirt on". And for what its worth, these expressions have nothing to do with to let your hair down.
"I wouldn't have bought any cheese" is expressed as a deduction (as in the discussion you follow up with) "it's not likely I bought cheese", or "I'm not the sort of person to have bought cheese there".
The pragmatic implication in this sense is either "I don't remember for sure whether I ever bought cheese, but I can't believe I did", or possibly "You must see that I'm not the sort of person to buy cheese there".
Best Answer
The above is the phrase in question--whether "when it comes time" is incorrect or unidiomatic.
I agree that "when the time comes" is more familiar. However, "when it comes time" can be short for "when it comes time" to do something--e.g., when it comes time to reconcile accounts, to pay taxes, or whatever.
Without more context it does seem odd, but I don't find it unidiomatic or incorrect.
Here's a quotation (from quote site):
From https://www.brainyquote.com/quotes/quotes/m/michelleob449066.html
Google Ngram
Here are links to specific contemporary examples of the use of "when it comes time" not followed by "to + V," thanks to @Hot Licks in comments.
community.babycenter.com/post/a66644842/…
spwickstrom.com/doubt
goodreads.com/book/show/1873060.The_Go_Giver
laurengreutman.com/gardening-with-raised-beds