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 "MayaLongCountCalendar.hpp"
21 #include "StelUtils.hpp"
22 #include "StelApp.hpp"
23 #include "StelCore.hpp"
24 
25 const long int MayaLongCountCalendar::mayanEpoch=fixedFromJD(584283, false);
26 
MayaLongCountCalendar(double jd)27 MayaLongCountCalendar::MayaLongCountCalendar(double jd): Calendar(jd)
28 {
29 	parts={0, 0, 0, 0, 0};
30 	retranslate();
31 }
32 
33 // Set a calendar date from the Julian day number
setJD(double JD)34 void MayaLongCountCalendar::setJD(double JD)
35 {
36 	this->JD=JD;
37 	const int rd=fixedFromJD(JD, true);
38 
39 	parts=mayaLongCountFromFixed(rd);
40 	emit partsChanged(parts);
41 }
42 
43 // get a stringlist of calendar date elements sorted from the largest to the smallest.
44 // baktun-katun-tun-uinal-kin
getDateStrings() const45 QStringList MayaLongCountCalendar::getDateStrings() const
46 {
47 	QStringList list;
48 	list << QString::number(parts.at(0));
49 	list << QString::number(parts.at(1));
50 	list << QString::number(parts.at(2));
51 	list << QString::number(parts.at(3));
52 	list << QString::number(parts.at(4));
53 
54 	return list;
55 }
56 
57 // get a formatted complete string for a date
getFormattedDateString() const58 QString MayaLongCountCalendar::getFormattedDateString() const
59 {
60 	return getDateStrings().join('.');
61 }
62 
63 // set date from a vector of calendar date elements sorted from the largest to the smallest.
64 // baktun-katun-tun-uinal-kin
setDate(QVector<int> parts)65 void MayaLongCountCalendar::setDate(QVector<int> parts)
66 {
67 	// Problem: This sets time to midnight. We need to keep and reset the fractional day.
68 	//const double dayFraction=JD-std::floor(JD-.5);
69 	//const double dayFraction=JD+0.5 -std::floor(JD);
70 
71 	this->parts=parts;
72 	int fixedFromMLC=fixedFromMayanLongCount(parts);
73 
74 	// restore time from JD!
75 	double frac=StelUtils::fmodpos(JD+0.5+StelApp::getInstance().getCore()->getUTCOffset(JD)/24., 1.);
76 	JD=jdFromFixed(fixedFromMLC+frac, true);
77 
78 	emit jdChanged(JD);
79 }
80 
81 //! get RD date from Long Count date
fixedFromMayanLongCount(QVector<int> longCount)82 int MayaLongCountCalendar::fixedFromMayanLongCount(QVector<int> longCount)
83 {
84 	const int baktun=longCount.at(0);
85 	const int katun =longCount.at(1);
86 	const int tun   =longCount.at(2);
87 	const int uinal =longCount.at(3);
88 	const int kin   =longCount.at(4);
89 
90 	return mayanEpoch+baktun*144000+katun*7200+tun*360+uinal*20+kin;
91 }
92 
93 //! get Long Count date from RD date
mayaLongCountFromFixed(int rd)94 QVector<int> MayaLongCountCalendar::mayaLongCountFromFixed(int rd)
95 {
96 	int longCount=rd-mayanEpoch;
97 	if (longCount<0) longCount-=144000; // trick to allow negative (but infinitely decreasing) baktun...
98 	const int baktun     =longCount / 144000;
99 	const int dayOfBaktun=StelUtils::imod(longCount, 144000); // ensure positive numbers from now...
100 	const int katun      =dayOfBaktun / 7200;
101 	const int dayOfKatun =dayOfBaktun % 7200;
102 	const int tun        =dayOfKatun / 360;
103 	const int dayOfTun   =dayOfKatun % 360;
104 	const int uinal      =dayOfTun / 20;
105 	const int kin        =dayOfTun % 20;
106 
107 	return { baktun, katun, tun, uinal, kin };
108 }
109