Don’t eat after 8 pm or you’ll gain weight. It’s become the common wisdom as more and more studies link eating the bulk of your calories in the evening with weight gain. But there is more to the story than the hour displayed on the clock. What really matters, new research finds, is the clock inside your body.

Background: Eating most of your daily calories close to bedtime is associated with being overweight. A leading hypothesis: When our bodies are winding down toward the end of the day—and our metabolism slows down with it—we burn fewer of calories we take in compared with what happens when we consume them earlier in the day. As a result, we store more body fat.

The key driver of this metabolism slowdown is the hormone melatonin. It plays a central role in the body’s daily rhythms. Melatonin tends to rise in the evening and stay elevated for about 12 hours. On average, those peak melatonin hours are from 9 pm to 9 am. But different people’s body clocks can be very different—some tend to go to bed earlier and get up earlier, and some tend to go to bed later and get up later. Researchers wondered how these individual body clocks might influence the late-night-eating/body fat effect.

Study: Researchers had 110 young adults record their sleep/wake times daily for a month. Then, for a week, the participants recorded everything they ate or drank (excluding water)—including how much and especially, when they did so. To figure out each person’s body clock, each participant’s saliva melatonin levels were tested throughout one night. Then the researchers analyzed how much each person ate in the four hours before his/her melatonin levels went up—and how that related to body weight and fatness.

Results: The time of day of eating was not, overall, associated with weight. Instead, eating close to each person’s individual melatonin-induced wind-down was significantly associated with weight gain and body fat. And the key fact to remember is that weight gain was most closely associated with consuming more than half of one’s daily calories in the four hours before melatonin rises.

Surprising finding: The number of calories that each person consumed wasn’t associated with body fat.

Bottom line: Eating before you go to bed, when your melatonin levels are high, is strongly linked to higher body fat. But when that time is for you depends on your body clock—not the time displayed on your watch or smartphone.

So go by your time. Melatonin levels tend to rise before you get sleepy and go to bed, so it’s not easy to know exactly when it occurs. But you do know when you tend to go to bed and go to sleep—bedtime. One rule of thumb endorsed by sleep experts is to eat very little within three hours of your bedtime. If your bedtime tends to be midnight, then, any eating after 9 pm should be very light. If you generally hit the sack by 10 pm, you should eat only lightly (if at all) after 7 pm. In other words, eat dinner by no later than 6 or 6:30.