Most calendars are intended to follow some celestial cycle, such as the phases of the moon or the seasons. These cycles usually don’t consist of a nice, integral number of days or months, so sometimes complicated rules for varying the length of years and months must be used to keep them in alignment with the target cycle.
The epoch of a calendar is its adopted starting point: the start of year 1 (or day 1) of the calendar. This is not necessarily the same as the day when the calendar was first used. For example, the currently used epoch for the Julian and Common calendars was invented about 500 years after the time indicated by that epoch.
An era is the period of time after an epoch, and so is connected to a particular calendar. To fully specify a particular day, one must specify not just the date corresponding to that day in the chosen calendar, but also which calendar one uses, for readers who might not use the same calendar system. This is done by specifying the era. For example, the period starting with year 1 of the Common calendar is the Common Era. Year 1432 of the Common calendar is referred to as year 1432 of the Common Era, or 1432 C.E.
One needs to be able to refer to years before the epoch of the calendar.
Many calendars were invented in regions and times where the number zero
was not known. In those regions and times, the year directly preceding
year 1 was referred to as "year 1 before the epoch (or era)", and the
preceding year was "year 2 before the epoch". We’ll refer to this
method of counting years as the "historical method". However, it is
mathematically more convenient to keep counting down by ones when one
goes to preceding years, so then the years immediately preceding year 1
are years 0 and -1. This method is preferred by astronomers, and
we’ll refer to it as the "astronomical method". The
function uses the astronomical method.