The ancient Maya people had two somewhat distinct calendar systems.

In somewhat simplified terms, one is a cyclical calendar known as The Calendar Round,
that meshes several sacred and civil cycles; the other is an offset calendar
known as The Long Count, similar in many ways to the Gregorian calendar.

The Calendar Round

The Calendar Round has several intermeshing sacred and civil cycles that uniquely identify a specific date in an approximately 52 year cycle.

The Tzolk’in

The sacred cycle in the Mayan calendar round was called the Tzolk’in. The Tzolk'in has a cycle of 20 day names:

   Imix’
   Ik’
   Ak’bal
   K’an
   Chikchan
   Kimi
   Manik’
   Lamat
   Muluk
   Ok
   Chuwen
   Eb
   Ben
   Hix
   Men
   K’ib’
   Kaban
   Etz’nab’
   Kawak
   Ajaw

Intermeshed with the named days, the Tzolk’in has a cycle of 13 numbered days; 1
through 13. Every day has both a number and a name that repeat in a 260 day cycle.

For example:

   1 Imix’
   2 Ik’
   3 Ak’bal
   ...
   11 Chuwen
   12 Eb
   13 Ben
   1 Hix
   2 Men
   3 K’ib’
   ... and so on.

The Haab’

The Mayan civil calendar is called the Haab’. This calendar has 365 days per
year, and is sometimes called the ‘vague year.’ It is substantially the same as
our year, but does not make leap year adjustments, so over long periods of time,
gets out of synchronization with the seasons. It consists of 18 months with 20 days each,
and the end of the year, a special month of only 5 days, giving a total of 365.
The 5 days of the month of Wayeb’ (the last month), are usually considered to be
a time of bad luck.

Each month in the Haab’ has a name. The Mayan names for the civil months are:

   Pop
   Wo’
   Sip
   Sotz’
   Sek
   Xul
   Yaxk’in
   Mol
   Ch’en
   Yax
   Sak’
   Keh
   Mak
   K’ank’in
   Muwan
   Pax
   K’ayab
   Kumk’u
   Wayeb’ (Short, "unlucky" month)

The months function very much like ours do. That is, for any given month we
count through all the days of that month, and then move on to the next month.

Normally, the day 1 Pop is considered the first day of the civil year, just as 1 January
is the first day of our year. In 2019, 1 Pop falls on April 2nd. But,
because of the leap year in 2020, 1 Pop falls on April 1st in the years 2020-2023.

The only really unusual aspect of the Haab’ calendar is that, although there are
20 (or 5) days in each month, the last day of the month is not called
the 20th (5th). Instead, the last day of the month is referred to as the
‘seating,’ or ‘putting in place,’ of the next month. (Much like how in our
culture, December 24th is Christmas Eve and December 31st is 'New-Years Eve'.) In
the language of the ancient Maya, the word for seating was chum, So you might
have:

   ...
   18 Pop (18th day of the first month)
   19 Pop (19th day of the first month)
   Chum Wo’ (20th day of the first month)
   1 Wo’ (1st day of the second month)
   ... and so on.

Dates for any particular day are a combination of the Tzolk’in sacred date,
and Haab’ civil date. When put together we get the “Calendar Round.”

Calendar Round dates always have two numbers and two names, and they are
always written in the same order:

   (1) the day number in the Tzolk’in
   (2) the day name in the  Tzolk’in
   (3) the day of the month in the Haab’
   (4) the month name in the Haab’

A calendar round is a repeating cycle with a period of just short of 52
Gregorian calendar years. To be precise: it is 52 x 365 days. (No leap days)

Lords of the Night

A third cycle of nine days honored the nine Lords of the Night; nine deities
that were associated with each day in turn. The names of the nine deities are
lost; they are now commonly referred to as G1 through G9. The Lord of the Night
may or may not be included in a Mayan date, if it is, it is typically
just the appropriate G(x) at the end.

The Long Count

Mayans had a separate independent way of measuring time that did not run in
cycles. (At least, not on normal human scales.) Instead, much like our yearly
calendar, each day just gets a little further from the starting point. For the
ancient Maya, the starting point was the ‘creation date’ of the current world.
This date corresponds to our date of August 11, 3114 B.C. Dates are calculated
by measuring how many days have transpired since this starting date; This is
called “The Long Count.” Rather than just an absolute count of days, the long
count is broken up into unit blocks, much like our calendar has months, years,
decades and centuries.

   The basic unit is a k’in - one day.
   A 20 day month is a winal.
   18 winal (360 days) is a tun - sometimes referred to as a short year.
   20 short years (tun) is a k’atun
   20 k’atun is a bak’tun

There are longer units too:

   Piktun == 20 Bak’tun (8,000 short years)
   Kalabtun  == 20 Piktun (160,000 short years)
   Kinchiltun  == 20 Kalabtun (3,200,000 short years)

For the most part, the Maya only used the blocks of time up to the bak’tun. One
bak’tun is around 394 years, much more than a human life span, so that was all
they usually needed to describe dates in this era, or this world. It is worth
noting, the two calendars working together allow much more accurate and reliable
notation for dates than is available in many other calendar systems; mostly due
to the pragmatic choice to make the calendar simply track days, rather than
trying to make it align with seasons and/or try to conflate it with the notion
of time.

Mayan Date correlations

There is some controversy over finding a correlation point between the Gregorian
and Mayan calendars. The Gregorian calendar is full of jumps and skips to keep
the calendar aligned with the seasons so is much more difficult to work with.
The most commonly used correlation
factor is The GMT: 584283. Julian 584283 is a day count corresponding Mon, Aug 11, 3114 BCE
in the Gregorian calendar, and the final day in the last Mayan long count
cycle: 13.0.0.0.0 which is referred to as "the day of creation" in the Mayan
calendar. There is nothing in known Mayan writing or history that suggests that
a long count "cycle" resets every 13 bak’tun. Judging by their other practices,
it would make much more sense for it to reset at 20, if at all.

The reason there was much interest at all, outside historical scholars, in
the Mayan calendar is that the long count recently (relatively speaking) rolled over to 13.0.0.0.0 (the same as the historic day of creation Long Count date) on Fri,
Dec 21, 2012 (using the most common GMT correlation factor), prompting conspiracy
theorists to predict a cataclysmic "end-of-the-world" scenario.

Excerpts taken from, and recommended reading:

From the website of the Foundation for the Advancement of Meso-America Studies, Inc. 
Pitts, Mark. The complete Writing in Maya Glyphs Book 2 – Maya Numbers and the Maya Calendar. 2009. Accessed 2019-01-19. 
http://www.famsi.org/research/pitts/MayaGlyphsBook2.pdf
wikipedia: Maya calendar
wikipedia: Mesoamerican Long Count calendar

The Task:

Write a reusable routine that takes a Gregorian date and returns the equivalent date in Mayan in the Calendar Round and the Long Count. At a minimum, use the GMT correlation. If desired, support other correlations.

Using the GMT correlation, the following Gregorian and Mayan dates are equivalent:

  Dec 21, 2012 (Gregorian)
  4 Ajaw 3 K’ank’in G9 (Calendar round)
  13.0.0.0.0 (Long count)

Support looking up dates for at least 50 years before and after the Mayan Long Count 13 bak’tun rollover: Dec 21, 2012. (Will require taking into account Gregorian leap days.)

Show the output here, on this page, for at least the following dates:

(Note that these are in ISO-8601 format: YYYY-MM-DD. There is no requirement to use ISO-8601 format in your program, but if you differ, make a note of the expected format.)

   2004-06-19
   2012-12-18
   2012-12-21
   2019-01-19
   2019-03-27
   2020-02-29
   2020-03-01

import math, strformat, times
 
const
 
  Tzolk´in = ["Imix´", "Ik´", "Ak´bal", "K´an", "Chikchan", "Kimi", "Manik´", "Lamat", "Muluk", "Ok",
              "Chuwen", "Eb", "Ben", "Hix", "Men", "K´ib´", "Kaban", "Etz´nab´", "Kawak", "Ajaw"]
 
  Haab´ = ["Pop", "Wo´", "Sip", "Sotz´", "Sek", "Xul", "Yaxk´in", "Mol", "Ch´en", "Yax",
           "Sak´", "Keh", "Mak", "K´ank´in", "Muwan", "Pax", "K´ayab", "Kumk´u", "Wayeb´"]
 
let
  creationTzolk´in = initDateTime(21, mDec, 2012, 0, 0, 0, utc())
  zeroHaab´ = initDateTime(2, mApr, 2019, 0, 0, 0, utc())
 
func daysPerMayanMonth(month: string): int =
  if month == "Wayeb´": 5 else: 20
 
proc tzolkin(gregorian: DateTime): string =
  let deltaDays = (gregorian - creationTzolk´in).inDays
  let rem = floorMod(deltaDays, 13)
  result = $(if rem <= 9: rem + 4 else: rem - 9) & ' ' & Tzolk´in[floorMod(deltaDays - 1, 20)]
 
proc haab(gregorian: DateTime): string =
  let rem = floorMod((gregorian - zeroHaab´).inDays, 365)
  let month = Haab´[(rem + 1) div 20]
  let dayOfMonth = rem mod 20 + 1
  result = if dayOfMonth < daysPerMayanMonth(month): &"{dayOfMonth} {month}" else: &"Chum {month}"
 
proc toLongDate(gregorian: DateTime): string =
  var delta = (gregorian - creationTzolk´in).inDays + 13 * 360 * 400
  var baktun = delta div (360 * 400)
  delta = delta mod (400 * 360)
  let katun = delta div (20 * 360)
  delta = delta mod (20 * 360)
  let tun = delta div 360
  delta = delta mod 360
  let winal = delta div 20
  let kin = delta mod 20
  result = &"{baktun:2} {katun:2} {tun:2} {winal:2} {kin:2}"
 
proc nightLord(gregorian: DateTime): string =
  var rem = (gregorian - creationTzolk´in).inDays mod 9
  if rem <= 0: rem += 9
  result = 'G' & $rem
 
 
when isMainModule:
 
  let testDates = [initDateTime(21, mNov, 1963, 0, 0, 0, utc()),
                   initDateTime(19, mJun, 2004, 0, 0, 0, utc()),
                   initDateTime(18, mDec, 2012, 0, 0, 0, utc()),
                   initDateTime(21, mDec, 2012, 0, 0, 0, utc()),
                   initDateTime(19, mJan, 2019, 0, 0, 0, utc()),
                   initDateTime(27, mMar, 2019, 0, 0, 0, utc()),
                   initDateTime(29, mFeb, 2020, 0, 0, 0, utc()),
                   initDateTime(01, mMar, 2020, 0, 0, 0, utc()),
                   initDateTime(16, mMay, 2071, 0, 0, 0, utc()),
                   initDateTime(02, mfeb, 2020, 0, 0, 0, utc())]
 
echo "Gregorian      Long Count       Tzolk´in  Haab´       Nightlord"
echo "———————————————————————————————————————————————————————————————"
for date in testDates:
  let dateStr = date.format("YYYY-MM-dd")
  echo &"{dateStr:14} {date.toLongDate:16} {tzolkin(date):9} {haab(date):14} {nightLord(date)}"