Just as a small background before answering your question: as suggested in the previous answers, the menstrual cycle is regulated by complex interactions between the hypothalamic-pituitary-ovarian (HPO) axis and the uterus. The hypothalamus secretes gonadotropin-releasing hormone (GnRH), which stimulates the release of follicle stimulating hormone (FSH) and luteinizing hormone (LH) from the pituitary gland. These so called gonadotropins (LH and FSH) stimulate the ovary to release an oocyte that is capable of fertilisation if it encounters a spermatozoid. In the same time, hormones are secreted by the ovary in response to FSH or LH. These hormones influence the endometrium (= the inner layer of the uterus)
The type of hormones released during the menstrual cycle and their concentration divide a menstrual cycle in three phases (see figure below):

- the follicular or proliferative phase (D4-D14 of the cycle): it is
characterised by the recruitment of a cohort of follicles and the
ultimate selection of a “dominant” preovulatory follicle.
Additionally, this phase shows increasing levels of estradiol which
stimulate the proliferation of endometrium. This proliferation takes
time (approx 10 days) as cells composing the endometrium replicate
intensively (mitosis) in order to enhance the thickness of the
endometrium (remember, the endometrium has to be “prepared” for a
possible implantation of a blastocyts (= first stage of an
embryon)). The figure below shows the change in the structure of the endometrium.
the luteal or secretory phase (D14-D28): the production of
progesterone leads to a differentiation of the endomterium which
reaches its full maturity. Here again, time is needed, to gain the
level of complexity found in the endometrium of the luteal phase: the
glands and the arteries in the endometrium begins to entwine, the
connective tissue show oedematous changes. Everything is prepared to help the blastocyst to develop in the case of an implantation
the menstruation phase (D1-D4): when implantation does not occur, the
amounts of estradiol and progesterone drop significantly, leading to
a constriction of the arteries found in the endometrium. This results
to the expulsion of the so called “functional layer” of the
endometrium.

Your question
How does my body gauge that amount of time? Is there some kind of
external factor (wake/sleep cycles, etc) or is there some kind of
internal timer?
Therefore the duration of the menstrual cycle is defined by the time needed for all these changes to occur: the proliferation of cells of the endometrium, the development of arteries and the differentiation of glands in the endometrium etc… Similar example would be the time needed for wound healing for example (where the proliferative phase is characterised by fibroblast migration, collagen synthesis, angiogenesis, migration of new cells) or lung tissue recovery after a pneumonia.
Why the duration of cycle is different from one woman to another is dependent, as you have already suggested, on internal factors (the concentration of hormones, the presence of a concomitant chronic disease for example) and external factors (stress, drugs,…)
The body gauges the amount of time necessary for the cycle, ie the time necessary for all the endometrial changes to occur (in a woman: approx 28-32 days) through a complex feedback from the hormones produced in the ovary/endometrium which “signal” the hypothalamus and the pituitary on whether a change in the secretion of LH/FSH should occur or not. This hypothalamus-pituitary-ovarian (HPO) axis is the “metronome” for the menstruation cycle (see figure below for an overview of the HPO axis)

Several studies have investigated the effect of menstruation cycle phases on sleep and reported that menstrual cycle alter sleep organisation at different menstrual phases. Also, disruption of circadian rhythms is associated with disturbances in menstrual function. For example, female shiftworkers compared to non-shiftworkers are more likely to report menstrual irregularity and longer menstrual cycles. This is likely due to a change in the circulating hormone, strengthening the role of “metronome” of the HPO in the time regulation of the menstruation cycle.
Sources:
- Longo DL, Fauci AS, Kasper DL, Hauser SL, Jameson J, Loscalzo J. eds. Harrison's Principles of Internal Medicine, 18e. New York, NY: McGraw-Hill; 2012. p 3028-3033
- Shechter A, Boivin DB. Sleep, Hormones, and Circadian Rhythms throughout the Menstrual Cycle in Healthy Women and Women with Premenstrual Dysphoric Disorder. International Journal of Endocrinology. 2010;2010:259345
- Baker FC, Driver HS. Circadian rhythms, sleep, and the menstrual cycle. Sleep Med. 2007 Sep;8(6):613-22. Epub 2007 Mar 26.
- www.meducation.net for Figure 2