1 /*
2  * Copyright (C) 2020 Georg Zotti
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335, USA.
17  */
18 
19 #include "StelTranslator.hpp"
20 #include "JulianCalendar.hpp"
21 #include "AztecXihuitlCalendar.hpp"
22 #include "AztecTonalpohualliCalendar.hpp"
23 #include "StelUtils.hpp"
24 #include "StelApp.hpp"
25 #include "StelCore.hpp"
26 
27 
28 const int AztecXihuitlCalendar::aztecCorrelation = JulianCalendar::fixedFromJulian({1521, JulianCalendar::august, 13});
29 const int AztecXihuitlCalendar::aztecXihuitlCorrelation = aztecCorrelation - aztecXihuitlOrdinal({11, 2});
30 
31 
AztecXihuitlCalendar(double jd)32 AztecXihuitlCalendar::AztecXihuitlCalendar(double jd): Calendar(jd)
33 {
34 	retranslate();
35 }
36 
37 QMap<int, QString> AztecXihuitlCalendar::monthNames;
38 
retranslate()39 void AztecXihuitlCalendar::retranslate()
40 {
41 	// fill the name lists with translated month and day names
42 	monthNames={
43 		{ 1, qc_("Izcalli (Sprout)"                 , "Aztec Xihuitl month name")},
44 		{ 2, qc_("Atlcahualo (Water left)"          , "Aztec Xihuitl month name")},
45 		{ 3, qc_("Tlacaxipehualiztli (Man flaying)" , "Aztec Xihuitl month name")},
46 		{ 4, qc_("Tozoztontli (1-Vigil)"            , "Aztec Xihuitl month name")},
47 		{ 5, qc_("Huei Tozoztli (2-Vigil)"          , "Aztec Xihuitl month name")},
48 		{ 6, qc_("Toxcatl (Drought)"                , "Aztec Xihuitl month name")},
49 		{ 7, qc_("Etzalcualiztli (Eating bean soup)", "Aztec Xihuitl month name")},
50 		{ 8, qc_("Tecuilhuitontli (1-Lord’s feast)" , "Aztec Xihuitl month name")},
51 		{ 9, qc_("Huei Tecuilhuitl (2-Lord’s feast)", "Aztec Xihuitl month name")},
52 		{10, qc_("Tlaxochimaco (Give flowers)"      , "Aztec Xihuitl month name")},
53 		{11, qc_("Xocotlhuetzi (Fruit falls)"       , "Aztec Xihuitl month name")},
54 		{12, qc_("Ochpaniztli (Road sweeping)"      , "Aztec Xihuitl month name")},
55 		{13, qc_("Teotleco (God arrives)"           , "Aztec Xihuitl month name")},
56 		{14, qc_("Tepeilhuitl (Mountain feast)"     , "Aztec Xihuitl month name")},
57 		{15, qc_("Quecholli (Macaw)"                , "Aztec Xihuitl month name")},
58 		{16, qc_("Panquetzaliztli (Flag raising)"   , "Aztec Xihuitl month name")},
59 		{17, qc_("Atemoztli (Falling water)"        , "Aztec Xihuitl month name")},
60 		{18, qc_("Tititl (Storm)"                   , "Aztec Xihuitl month name")},
61 		{19, qc_("Nemontemi (Full in vain)"         , "Aztec Xihuitl month name")}};
62 }
63 
64 // Set a calendar date from the Julian day number
setJD(double JD)65 void AztecXihuitlCalendar::setJD(double JD)
66 {
67 	this->JD=JD;
68 
69 	const int rd=fixedFromJD(JD, true);
70 	const int count=StelUtils::imod(rd-aztecXihuitlCorrelation, 365);
71 	const int day = count % 20 + 1;
72 	const int month=count/20 + 1;
73 
74 	parts = { month, day };
75 
76 	emit partsChanged(parts);
77 }
78 
79 // get a stringlist of calendar date elements sorted from the largest to the smallest.
80 // monthName-dayNumber
getDateStrings() const81 QStringList AztecXihuitlCalendar::getDateStrings() const
82 {
83 	QStringList list;
84 	list << monthNames.value(parts.at(0));
85 	list << QString::number(parts.at(1));
86 
87 	return list;
88 }
89 
90 
91 // get a formatted complete string for a date
getFormattedDateString() const92 QString AztecXihuitlCalendar::getFormattedDateString() const
93 {
94 	QStringList str=getDateStrings();
95 	return QString("%1 %2")
96 			.arg(str.at(1))
97 			.arg(str.at(0));
98 }
99 
100 // set date from a vector of calendar date elements sorted from the largest to the smallest.
101 // month-day
102 // We face a problem as the year is not unique. We can only find the date before current JD which matches the parts.
setDate(QVector<int> parts)103 void AztecXihuitlCalendar::setDate(QVector<int> parts)
104 {
105 	this->parts=parts;
106 
107 	const int rdOnOrBefore=aztecXihuitlOnOrBefore(parts, fixedFromJD(JD));
108 
109 	// restore time from JD!
110 	double frac=StelUtils::fmodpos(JD+0.5+StelApp::getInstance().getCore()->getUTCOffset(JD)/24., 1.);
111 	JD=jdFromFixed(rdOnOrBefore+frac, true);
112 
113 	emit jdChanged(JD);
114 }
115 
aztecXihuitlFromFixed(int rd)116 QVector<int> AztecXihuitlCalendar::aztecXihuitlFromFixed(int rd)
117 {
118 	const int count = StelUtils::imod(rd-aztecXihuitlCorrelation, 365);
119 	const int day = count % 20 + 1;
120 	const int month=count/20 + 1;
121 	return {month, day};
122 }
123 
aztecXihuitlOnOrBefore(QVector<int> xihuitl,int rd)124 int AztecXihuitlCalendar::aztecXihuitlOnOrBefore(QVector<int> xihuitl, int rd)
125 {
126 	return modInterval(aztecXihuitlCorrelation+aztecXihuitlOrdinal(xihuitl), rd, rd-365);
127 }
128 
129 // get RD of a combined date
aztecXihuitlTonalpohualliOnOrBefore(QVector<int> xihuitl,QVector<int> tonalpohualli,int rd)130 int AztecXihuitlCalendar::aztecXihuitlTonalpohualliOnOrBefore(QVector<int>xihuitl, QVector<int>tonalpohualli, int rd)
131 {
132 	const int xihuitlCount=aztecXihuitlOrdinal(xihuitl)+aztecXihuitlCorrelation;
133 	const int tonalpohualliCount=AztecTonalpohualliCalendar::aztecTonalpohualliOrdinal(tonalpohualli)+AztecTonalpohualliCalendar::aztecTonalpohualliCorrelation;
134 	const int diff=tonalpohualliCount-xihuitlCount;
135 	if (diff % 5 ==0)
136 		return modInterval(xihuitlCount+365*diff, rd, rd-18980);
137 	else
138 		return bogus;
139 }
140