Mayan calendar: Difference between revisions

m (J: slightly more concise)
 
(11 intermediate revisions by 6 users not shown)
Line 212:
2020-02-29
2020-03-01
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">
V _DAYS_IN_MONTH = [-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
 
V _DAYS_BEFORE_MONTH = [-1]
V dbm = 0
L(dim) _DAYS_IN_MONTH[1..]
_DAYS_BEFORE_MONTH.append(dbm)
dbm += dim
 
F _days_before_year(year)
‘year -> number of days before January 1st of year.’
V y = year - 1
R y * 365 + y I/ 4 - y I/ 100 + y I/ 400
 
F _is_leap(year)
‘year -> 1 if leap year, else 0.’
R year % 4 == 0 & (year % 100 != 0 | year % 400 == 0)
 
F _days_before_month(year, month)
‘year, month -> number of days in year preceding first day of month.’
R :_DAYS_BEFORE_MONTH[month] + (month > 2 & _is_leap(year))
 
F _ymd2ord(year, month, day)
‘year, month, day -> ordinal, considering 01-Jan-0001 as day 1.’
R (_days_before_year(year) + _days_before_month(year, month) + day)
 
F g2m(date, gtm_correlation = 1B)
V correlation = I gtm_correlation {584283} E 584285
 
V long_count_days = [144000, 7200, 360, 20, 1]
 
V tzolkin_months = ["Imix'", "Ik'", "Ak'bal", "K'an", ‘Chikchan’, ‘Kimi’, "Manik'", ‘Lamat’, ‘Muluk’, ‘Ok’, ‘Chuwen’,
‘Eb’, ‘Ben’, ‘Hix’, ‘Men’, "K'ib'", ‘Kaban’, "Etz'nab'", ‘Kawak’, ‘Ajaw’]
 
V haad_months = [‘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'"]
 
V (year, month, day) = date.split(‘-’).map(Int)
V gregorian_days = _ymd2ord(year, month, day)
V julian_days = gregorian_days + 1721425
 
[Int] long_date
V remainder = julian_days - correlation
 
L(days) long_count_days
(V result, remainder) = divmod(remainder, days)
long_date.append(Int(result))
 
V long_date_str = (long_date.map(d -> ‘#02’.format(d))).join(‘.’)
 
V tzolkin_month = (julian_days + 16) % 20
V tzolkin_day = ((julian_days + 5) % 13) + 1
 
V haab_month = Int(((julian_days + 65) % 365) / 20)
V haab_day = ((julian_days + 65) % 365) % 20
V haab_day_str = I haab_day {String(haab_day)} E ‘Chum’
 
V lord_number = (julian_days - correlation) % 9
lord_number = I lord_number {lord_number} E 9
 
V round_date = tzolkin_day‘ ’tzolkin_months[tzolkin_month]‘ ’haab_day_str‘ ’haad_months[haab_month]‘ G’lord_number
 
R (long_date_str, round_date)
 
V dates = [‘2004-06-19’, ‘2012-12-18’, ‘2012-12-21’, ‘2019-01-19’, ‘2019-03-27’, ‘2020-02-29’, ‘2020-03-01’]
L(date) dates
V (long, round_date) = g2m(date)
print(date‘ ’long‘ ’round_date)
</syntaxhighlight>
 
{{out}}
<pre>
2004-06-19 12.19.11.06.13 4 Ben 16 Sotz' G7
2012-12-18 12.19.19.17.17 1 Kaban Chum K'ank'in G6
2012-12-21 13.00.00.00.00 4 Ajaw 3 K'ank'in G9
2019-01-19 13.00.06.03.00 1 Ajaw 13 Muwan G6
2019-03-27 13.00.06.06.07 3 Manik' Chum Wayeb' G1
2020-02-29 13.00.07.05.06 4 Kimi 14 K'ayab G7
2020-03-01 13.00.07.05.07 5 Manik' 15 K'ayab G8
</pre>
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <chrono>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
 
const std::vector<std::string> Tzolkin = { "Imix'", "Ik'", "Ak'bal", "K'an", "Chikchan", "Kimi", "Manik'",
"Lamat", "Muluk", "Ok", "Chuwen", "Eb", "Ben", "Hix", "Men", "K'ib'", "Kaban", "Etz'nab'", "Kawak", "Ajaw" };
 
const std::vector<std::string> 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'" };
 
 
int32_t positive_modulus(const int32_t& base, const int32_t& modulus) {
const int32_t result = base % modulus;
return ( result < 0 ) ? result + modulus : result;
}
 
std::chrono::sys_days create_date(const std::string& iso_date) {
const int year = std::stoi(iso_date.substr(0, 4));
const unsigned int month = std::stoi(iso_date.substr(5, 7));
const unsigned int day = std::stoi(iso_date.substr(8, 10));
 
std::chrono::year_month_day date{std::chrono::year{year}, std::chrono::month{month}, std::chrono::day{day}};
return std::chrono::sys_days(date);
}
 
const std::chrono::sys_days CREATION_TZOLKIN = create_date("2012-12-21");
const std::chrono::sys_days ZERO_HAAB = create_date("2019-04-02");
 
int32_t days_per_mayan_month(const std::string& month) {
return ( month == "Wayeb'" ) ? 5 : 20;
}
 
std::string tzolkin(const std::chrono::sys_days& gregorian) {
const int32_t days_between = ( gregorian - CREATION_TZOLKIN ).count();
int32_t remainder = positive_modulus(days_between, 13);
remainder += ( remainder <= 9 ) ? 4 : -9;
return std::to_string(remainder) + " " + Tzolkin[positive_modulus(days_between - 1, 20)];
}
 
std::string haab(const std::chrono::sys_days& gregorian) {
const int32_t days_between = ( gregorian - ZERO_HAAB ).count();
int32_t remainder = positive_modulus(days_between, 365);
const std::string month = Haab[positive_modulus(remainder + 1, 20)];
const int32_t day_of_month = remainder % 20 + 1;
return ( day_of_month < days_per_mayan_month(month) ) ?
std::to_string(day_of_month) + " " + month : "Chum " + month;
}
 
std::string long_count(const std::chrono::sys_days& gregorian) {
int32_t days_between = ( gregorian - CREATION_TZOLKIN ).count() + 13 * 360 * 400;
const int32_t baktun = positive_modulus(days_between, 360 * 400);
days_between = days_between % ( 360 * 400 );
const int32_t katun = days_between / ( 20 * 360 );
days_between = days_between % ( 20 * 360 );
const int32_t tun = days_between / 360;
days_between = days_between % 360;
const int32_t winal = days_between / 20;
const int32_t kin = days_between % 20;
 
std::string result = "";
for ( int32_t number : { baktun, katun, tun, winal, kin } ) {
std::string value = std::to_string(number) + ".";
result += ( number <= 9 ) ? "0" + value : value;
}
return result.substr(0, result.length() - 1);
}
 
std::string lords_of_the_night(const std::chrono::sys_days& gregorian) {
const int32_t days_between = ( gregorian - CREATION_TZOLKIN ).count();
const int32_t remainder = days_between % 9;
return "G" + std::to_string( ( remainder <= 0 ) ? remainder + 9 : remainder );
}
 
int main() {
const std::vector<std::string> iso_dates = { "2004-06-19", "2012-12-18", "2012-12-21", "2019-01-19",
"2019-03-27", "2020-02-29", "2020-03-01", "2071-05-16", "2020-02-02" };
 
std::cout << "Gregorian Long Count Tzolk'in Haab' Lords of the Night" << std::endl;
std::cout << "------------------------------------------------------------------------------" << std::endl;
for ( const std::string& iso_date : iso_dates ) {
const std::chrono::sys_days date = create_date(iso_date);
 
std::cout << std::left << std::setw(15) << iso_date << std::setw(19) << long_count(date)
<< std::setw(12) << tzolkin(date) << std::setw(18) << haab(date)
<< lords_of_the_night(date) << std::endl;
}
}
</syntaxhighlight>
{{ out }}
<pre>
Gregorian Long Count Tzolk'in Haab' Lords of the Night
------------------------------------------------------------------------------
2004-06-19 12.19.11.06.13 4 Ben 16 Sotz' G7
2012-12-18 12.19.19.17.17 1 Kaban Chum K'ank'in G6
2012-12-21 13.00.00.00.00 4 Ajaw 3 K'ank'in G9
2019-01-19 13.00.06.03.00 1 Ajaw 13 Muwan G6
2019-03-27 13.00.06.06.07 3 Manik' Chum Wayeb' G1
2020-02-29 13.00.07.05.06 4 Kimi 14 K'ayab G7
2020-03-01 13.00.07.05.07 5 Manik' 15 K'ayab G8
2071-05-16 13.02.19.04.10 1 Ok 18 Sip G9
2020-02-02 13.00.07.03.19 3 Kawak 7 Pax G7
</pre>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
PROGRAM MAYA_DRIVER
IMPLICIT NONE
Line 375 ⟶ 567:
END SUBROUTINE maya_time
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 390 ⟶ 582:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 491 ⟶ 683:
fmt.Printf("%s %2d %-8s %4s %-9s %-14s %s\n", dt, n, s, d, m, lc, l)
}
}</langsyntaxhighlight>
 
{{out}}
Line 511 ⟶ 703:
 
Implementation:
<langsyntaxhighlight Jlang="j">require 'types/datetime'
 
NB. 1794214= (0 20 18 20 20#.13 0 0 0 0)-todayno 2012 12 21
Line 535 ⟶ 727:
round=: [:;:inv tsolkin;haab;'G',&":1+9|]
 
gmt=: >@(fmtDate;round;longcount)@todayno</langsyntaxhighlight>
 
J's <tt>todayno</tt> counts days from January 1, 1800. And, each of these date formatting routines expects a number generated by todayno, with hard coded offsets, if necessary, to make this work. (The lord of night number uses an offset of zero, in this context.)
 
Also, in haab, our 'chum' days are zeros from modulo arithmetic (and, thus, belong to the previous month).
 
Task examples:
<langsyntaxhighlight Jlang="j"> gmt 2004 6 19
June 19, 2004
4 Ben 16 Sotz’ G7
Line 571 ⟶ 767:
March 1, 2019
3 Imix’ 14 K’ayab G2
13.0.5.13.1</langsyntaxhighlight>
 
=={{header|Java}}==
<syntaxhighlight lang="java">
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.List;
 
public final class MayanCalendar {
 
public static void main(String[] aArgs) {
List<LocalDate> testDates = List.of( LocalDate.parse("2004-06-19"),
LocalDate.parse("2012-12-18"),
LocalDate.parse("2012-12-21"),
LocalDate.parse("2019-01-19"),
LocalDate.parse("2019-03-27"),
LocalDate.parse("2020-02-29"),
LocalDate.parse("2020-03-01"),
LocalDate.parse("2071-05-16"),
LocalDate.parse("2020-02-02") );
System.out.println("Gregorian Long Count Tzolk'in Haab' Lords of the Night");
System.out.println("------------------------------------------------------------------------------");
for ( LocalDate date : testDates ) {
System.out.println(String.format("%-15s%-19s%-12s%-18s%s",
date.toString(), longCount(date), tzolkin(date), haab(date), lordsOfTheNight(date)));
}
}
private static String lordsOfTheNight(LocalDate aGregorian) {
long daysBetween = ChronoUnit.DAYS.between(CREATION_TZOLKIN, aGregorian);
long remainder = Math.floorMod(daysBetween, 9);
return "G" + ( ( remainder <= 0 ) ? remainder + 9 : remainder );
}
private static String longCount(LocalDate aGregorian) {
long daysBetween = ChronoUnit.DAYS.between(CREATION_TZOLKIN, aGregorian) + 13 * 360 * 400;
long baktun = Math.floorDiv(daysBetween, 360 * 400);
daysBetween = Math.floorMod(daysBetween, 360 * 400);
long katun = Math.floorDiv(daysBetween, 20 * 360);
daysBetween = Math.floorMod(daysBetween, 20 * 360);
long tun = Math.floorDiv(daysBetween, 360);
daysBetween = Math.floorMod(daysBetween, 360);
long winal = Math.floorDiv(daysBetween, 20);
long kin = Math.floorMod(daysBetween, 20);
StringBuilder result = new StringBuilder();
for ( long number : List.of( baktun, katun, tun, winal, kin ) ) {
String value = String.valueOf(number) + ".";
result.append( number <= 9 ? "0" + value : value );
}
return result.toString().substring(0, result.length() - 1);
}
private static String haab(LocalDate aGregorian) {
long daysBetween = ChronoUnit.DAYS.between(ZERO_HAAB, aGregorian);
int remainder = Math.floorMod(daysBetween, 365);
String month = Haab.get(Math.floorDiv(remainder + 1, 20));
int dayOfMonth = Math.floorMod(remainder, 20) + 1;
return ( dayOfMonth < daysPerMayanMonth(month) ) ? dayOfMonth + " " + month : "Chum " + month;
}
 
private static String tzolkin(LocalDate aGregorian) {
long daysBetween = ChronoUnit.DAYS.between(CREATION_TZOLKIN, aGregorian);
int remainder = Math.floorMod(daysBetween, 13);
remainder += ( remainder <= 9 ) ? 4 : -9;
return remainder + " " + Tzolkin.get(Math.floorMod(daysBetween - 1, 20));
}
private static int daysPerMayanMonth(String aMonth) {
return ( aMonth == "Wayeb'" ) ? 5 : 20;
}
private static List<String> Tzolkin = List.of( "Imix'", "Ik'", "Ak'bal", "K'an", "Chikchan", "Kimi", "Manik'",
"Lamat", "Muluk", "Ok", "Chuwen", "Eb", "Ben", "Hix", "Men", "K'ib'", "Kaban", "Etz'nab'", "Kawak", "Ajaw" );
 
private static List<String> Haab = List.of( "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'" );
private static final LocalDate CREATION_TZOLKIN = LocalDate.parse("2012-12-21");
private static final LocalDate ZERO_HAAB = LocalDate.parse("2019-04-02");
 
}
</syntaxhighlight>
{{ out }}
<pre>
Gregorian Long Count Tzolk'in Haab' Lords of the Night
------------------------------------------------------------------------------
2004-06-19 12.19.11.06.13 4 Ben 16 Sotz' G7
2012-12-18 12.19.19.17.17 1 Kaban Chum K'ank'in G6
2012-12-21 13.00.00.00.00 4 Ajaw 3 K'ank'in G9
2019-01-19 13.00.06.03.00 1 Ajaw 13 Muwan G6
2019-03-27 13.00.06.06.07 3 Manik' Chum Wayeb' G1
2020-02-29 13.00.07.05.06 4 Kimi 14 K'ayab G7
2020-03-01 13.00.07.05.07 5 Manik' 15 K'ayab G8
2071-05-16 13.02.19.04.10 1 Ok 18 Sip G9
2020-02-02 13.00.07.03.19 3 Kawak 7 Pax G7
</pre>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
 
In this entry, the functions for converting Gregorian dates to dates
in the Mayan calendar take the form of zero-arity filters that expect,
as input, strings of the form "yyyy-mm-dd" or JSON Date objects of the
form {"year", "month", "day"}.
 
<syntaxhighlight lang="jq">
### General utilities
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l) + .;
def rpad($len): tostring | ($len - length) as $l | . + (" " * $l);
 
# days_between("2020-01-01"; "2020-01-02") #=> 1
# days_between("2020-01-02"; "2020-01-01") #=> -1
def days_between($yyyymmddBefore; $yyyymmddAfter):
(yyyymmddBefore | strptime("%Y-%m-%d") | mktime) as $before
| (yyyymmddAfter | strptime("%Y-%m-%d") | mktime) as $after
# leap seconds are always inserted
| (($after - $before) / (24*60*60) | floor) ;
 
### `Date` objects
 
def Date($year; $month; $day): {$year, $month, $day};
 
# Convert a Date object to a string yyyy-mm-dd
# Other inputs are (currently) unscathed.
def yyyymmdd:
if type == "object" then "\(.year)-\(.month)-\(.day)"
else .
end;
 
### Mayan Calendar
 
def sacred:
"Imix’ Ik’ Ak’bal K’an Chikchan Kimi Manik’ Lamat Muluk Ok Chuwen Eb Ben Hix Men K’ib’ Kaban Etz’nab’ Kawak Ajaw"
| split(" ");
 
def civil:
"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’"
| split(" ");
 
def CREATION_TZOLKIN: "2012-12-21";
 
# Input: Date or yyyy-mm-dd
def tzolkin:
(days_between(CREATION_TZOLKIN; yyyymmdd)) as $diff
| {rem: ($diff % 13)}
| if .rem < 0 then .rem += 13 end
| .num = (if .rem <= 9 then .rem + 4 else .rem - 9 end)
| .rem = $diff % 20
| if .rem <= 0 then .rem += 20 end
| [.num, sacred[.rem-1] ] ;
 
# Input: Date or yyyy-mm-dd
def haab:
def ZERO_HAAB: "2019-04-02";
(days_between(ZERO_HAAB; yyyymmdd)) as $diff
| {rem: ($diff % 365)}
| if .rem < 0 then .rem += 365 end
| .month = civil[((.rem+1)/20)|floor]
| .last = (if .month == "Wayeb'" then 5 else 20 end)
| .d = .rem%20 + 1
| if .d < .last then [(.d|tostring), .month]
else ["Chum", .month]
end;
 
# Input: Date or yyyy-mm-dd
def longCount:
{diff: days_between(CREATION_TZOLKIN; yyyymmdd)}
| .diff += 13*400*360
| .baktun = ((.diff/(400*360)) | floor)
| .diff = .diff % (400*360)
| .katun = ((.diff/(20 * 360))|floor)
| .diff = .diff % (20*360)
| .tun = ((.diff/360)|floor)
| .diff = .diff % 360
| .winal = ((.diff/20)|floor)
| .kin = .diff % 20
| [.baktun, .katun, .tun, .winal, .kin]
| join(".");
 
# Input: Date or yyyy-mm-dd
def lord:
{diff: days_between(CREATION_TZOLKIN; yyyymmdd)}
| .rem = .diff % 9
| if .rem <= 0 then .rem += 9 end
| "G\(.rem)";
 
def dates: [
"1961-10-06",
"1963-11-21",
"2004-06-19",
"2012-12-18",
"2012-12-21",
"2019-01-19",
"2019-03-27",
"2020-02-29",
"2020-03-01",
"2071-05-16"
];
 
" Gregorian Tzolk’in Haab’ Long Lord of",
" Date # Name Day Month Count the Night",
"---------- -------- ------------- -------------- ---------",
# Date.default = Date.isoDate
(dates[]
| tzolkin as [$n, $s]
| haab as [$d, $m]
| [., ($n | lpad(4)), ($s | rpad(8)), ($d|lpad(4)), ($m|rpad(8)), (longCount|lpad(20)), (lord|lpad(6))]
| join(" ")
)
</syntaxhighlight>
{{output}}
<pre>
Gregorian Tzolk’in Haab’ Long Lord of
Date # Name Day Month Count the Night
---------- -------- ------------- -------------- ---------
1961-10-06 7 K’ib’ 14 Ch’en 12.17.8.0.16 G7
1963-11-21 3 Eb Chum Keh 12.17.10.3.12 G9
2004-06-19 4 Ben 16 Sotz’ 12.19.11.6.13 G7
2012-12-18 1 Kaban Chum K’ank’in 12.19.19.17.17 G6
2012-12-21 4 Ajaw 3 K’ank’in 13.0.0.0.0 G9
2019-01-19 1 Ajaw 13 Muwan’ 13.0.6.3.0 G6
2019-03-27 3 Manik’ Chum Wayeb’ 13.0.6.6.7 G1
2020-02-29 4 Kimi 14 K’ayab 13.0.7.5.6 G7
2020-03-01 5 Manik’ 15 K’ayab 13.0.7.5.7 G8
2071-05-16 1 Ok 18 Sip 13.2.19.4.10 G9
</pre>
 
=={{header|Julia}}==
{{trans|Go}}
<langsyntaxhighlight lang="julia">using Dates
 
const Tzolk´in = [
Line 628 ⟶ 1,057:
rpad(haab(date), 15), nightlord(date))
end
</syntaxhighlight>
</lang>{{out}}
{{out}}
<pre>
Gregorian Long Count Tzolk´in Haab´ Nightlord
Line 645 ⟶ 1,075:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">sacred = {"Imix\[CloseCurlyQuote]", "Ik\[CloseCurlyQuote]",
"Ak\[CloseCurlyQuote]bal", "K\[CloseCurlyQuote]an", "Chikchan",
"Kimi", "Manik\[CloseCurlyQuote]", "Lamat", "Muluk", "Ok",
Line 711 ⟶ 1,141:
longcountdata = LongCount /@ data;
lotndata = LordOfTheNight /@ data;
{DateObject /@ data, tzolkindata, haabdata, longcountdata, lotndata} // Transpose // Grid</langsyntaxhighlight>
{{out}}
<pre>Sat 19 Jun 2004 4 Ben 16 Sotz' 12.19.11.6.13 G7
Line 724 ⟶ 1,154:
=={{header|Nim}}==
{{trans|Julia}}
<langsyntaxhighlight Nimlang="nim">import math, strformat, times
 
const
Line 787 ⟶ 1,217:
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)}"</langsyntaxhighlight>
 
{{out}}
Line 806 ⟶ 1,236:
The module <code>Math::BaseArith</code> provides mixed-radix conversion via the <code>encode</code> routine (named as in '''APL''').
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use utf8;
Line 884 ⟶ 1,314:
printf "%10s %2s %-9s %4s %-10s %-14s G%d\n",
$date, mayan_calendar_round($date), join('.',mayan_long_count($date)), lord($date);
}</langsyntaxhighlight>
{{out}}
<pre> Gregorian Tzolk’in Haab’ Long Lord of
Line 902 ⟶ 1,332:
=={{header|Phix}}==
{{trans|Go}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">timedate</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 980 ⟶ 1,410:
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
<span style="color: #000000;">main</span><span style="color: #0000FF;">()</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 996 ⟶ 1,426:
2020-03-01 5 Manik' 15 K'ayab 13.0.7.5.7 G8
2071-05-16 1 Ok 18 Sip 13.2.19.4.10 G9
</pre>
 
=={{header|Python}}==
{{works with|Python|3.6}}
 
<syntaxhighlight lang="python">
import datetime
 
 
def g2m(date, gtm_correlation=True):
"""
Translates Gregorian date into Mayan date, see
https://rosettacode.org/wiki/Mayan_calendar
 
Input arguments:
 
date: string date in ISO-8601 format: YYYY-MM-DD
gtm_correlation: GTM correlation to apply if True, Astronomical correlation otherwise (optional, True by default)
 
Output arguments:
 
long_date: Mayan date in Long Count system as string
round_date: Mayan date in Calendar Round system as string
"""
 
# define some parameters and names
 
correlation = 584283 if gtm_correlation else 584285
 
long_count_days = [144000, 7200, 360, 20, 1]
 
tzolkin_months = ['Imix’', 'Ik’', 'Ak’bal', 'K’an', 'Chikchan', 'Kimi', 'Manik’', 'Lamat', 'Muluk', 'Ok', 'Chuwen',
'Eb', 'Ben', 'Hix', 'Men', 'K’ib’', 'Kaban', 'Etz’nab’', 'Kawak', 'Ajaw'] # tzolk'in
 
haad_months = ['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’'] # haab'
 
gregorian_days = datetime.datetime.strptime(date, '%Y-%m-%d').toordinal()
julian_days = gregorian_days + 1721425
 
# 1. calculate long count date
 
long_date = list()
remainder = julian_days - correlation
 
for days in long_count_days:
 
result, remainder = divmod(remainder, days)
long_date.append(int(result))
 
long_date = '.'.join(['{:02d}'.format(d) for d in long_date])
 
# 2. calculate round calendar date
 
tzolkin_month = (julian_days + 16) % 20
tzolkin_day = ((julian_days + 5) % 13) + 1
 
haab_month = int(((julian_days + 65) % 365) / 20)
haab_day = ((julian_days + 65) % 365) % 20
haab_day = haab_day if haab_day else 'Chum'
 
lord_number = (julian_days - correlation) % 9
lord_number = lord_number if lord_number else 9
 
round_date = f'{tzolkin_day} {tzolkin_months[tzolkin_month]} {haab_day} {haad_months[haab_month]} G{lord_number}'
 
return long_date, round_date
 
if __name__ == '__main__':
 
dates = ['2004-06-19', '2012-12-18', '2012-12-21', '2019-01-19', '2019-03-27', '2020-02-29', '2020-03-01']
 
for date in dates:
 
long, round = g2m(date)
print(date, long, round)
</syntaxhighlight>
 
{{out}}
<pre>
2004-06-19 12.19.11.06.13 4 Ben 16 Sotz’ G7
2012-12-18 12.19.19.17.17 1 Kaban Chum K’ank’in G6
2012-12-21 13.00.00.00.00 4 Ajaw 3 K’ank’in G9
2019-01-19 13.00.06.03.00 1 Ajaw 13 Muwan G6
2019-03-27 13.00.06.06.07 3 Manik’ Chum Wayeb’ G1
2020-02-29 13.00.07.05.06 4 Kimi 14 K’ayab G7
2020-03-01 13.00.07.05.07 5 Manik’ 15 K’ayab G8
</pre>
 
Line 1,002 ⟶ 1,519:
{{works with|Rakudo|2018.12}}
 
<syntaxhighlight lang="raku" perl6line>my @sacred = <Imix’ Ik’ Ak’bal K’an Chikchan Kimi Manik’ Lamat Muluk Ok
Chuwen Eb Ben Hix Men K’ib’ Kaban Etz’nab’ Kawak Ajaw>;
 
Line 1,073 ⟶ 1,590:
printf "%10s %2s %-9s %4s %-10s %-14s %6s\n", Date.new($date),
flat mayan-calendar-round($date), mayan-long-count($date).join('.'), lord($date);
}</langsyntaxhighlight>
{{out}}
<pre> Gregorian Tzolk’in Haab’ Long Lord of
Line 1,092 ⟶ 1,609:
{{libheader|Wren-date}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./date" for Date
import "./fmt" for Fmt
 
var sacred = "Imix’ Ik’ Ak’bal K’an Chikchan Kimi Manik’ Lamat Muluk Ok Chuwen Eb Ben Hix Men K’ib’ Kaban Etz’nab’ Kawak Ajaw".split(" ")
Line 1,171 ⟶ 1,688:
var l = lord.call(date)
Fmt.lprint("$s $2d $-8s $4s $-9s $-14s $s", [dt, n, s, d, m, lc, l])
}</langsyntaxhighlight>
 
{{out}}
Line 1,192 ⟶ 1,709:
=={{header|zkl}}==
{{trans|Go}}
<langsyntaxhighlight lang="zkl">var [const]
sacred=T("Imix'","Ik'","Ak'bal","K'an","Chikchan","Kimi","Manik'","Lamat","Muluk","Ok",
"Chuwen","Eb","Ben","Hix","Men","K'ib'","Kaban","Etz'nab'","Kawak","Ajaw"),
Line 1,240 ⟶ 1,757:
if(rem<=0) rem=9 + rem;
"G%d".fmt(rem)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">println(" Gregorian Tzolk'in Haab' Long Lord of");
println(" Date # Name Day Month Count the Night");
println("---------- -------- ------------- -------------- ---------");
Line 1,257 ⟶ 1,774:
longCount(ymd).concat("."),
lord(ymd)));
});</langsyntaxhighlight>
{{out}}
<pre>
2,442

edits