IsLeapYear🔗

Type: Transpilable verification function
Check that the year of an xs:date is a leap year.

The year is expected to be given as text, i.e. , as digits with an optional minus prefix, as the year can be arbitrarily large.

We consider the years B.C. to be one-off. See the note at https://www.w3.org/TR/xmlschema-2/#dateTime: "'-0001' is the lexical representation of the year 1 Before Common Era (1 BCE, sometimes written "1 BC")." Hence, the year -1 in XML is the year 0 in astronomical years.

year🔗
Text of the year to be checked
Return🔗
True if the year is a leap year

Code

remainder = int(year.lstrip("-")[-4:]) % 400

if year[:1] == "-":
    # NOTE (mristin):
    # We shift the years B.C. by one. We add 399 instead of subtracting 1 so
    # that the remainder is never negative, as the modulo of negative numbers
    # differs between the programming languages.
    remainder = (remainder + 399) % 400

# See: https://en.wikipedia.org/wiki/Leap_year#Algorithm
return remainder % 4 == 0 and (remainder % 100 != 0 or remainder == 0)