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 "StelUtils.hpp"
22 #include "StelApp.hpp"
23 #include "StelCore.hpp"
24 
JulianCalendar(double jd)25 JulianCalendar::JulianCalendar(double jd): Calendar(jd)
26 {
27 	JulianCalendar::retranslate();
28 }
29 
30 QMap<int, QString> JulianCalendar::weekDayNames;
31 QMap<int, QString> JulianCalendar::monthNames;
32 
retranslate()33 void JulianCalendar::retranslate()
34 {
35 	// fill the name lists with translated month and day names
36 	weekDayNames={
37 		{0, qc_("Sunday"   , "long day name")},
38 		{1, qc_("Monday"   , "long day name")},
39 		{2, qc_("Tuesday"  , "long day name")},
40 		{3, qc_("Wednesday", "long day name")},
41 		{4, qc_("Thursday" , "long day name")},
42 		{5, qc_("Friday"   , "long day name")},
43 		{6, qc_("Saturday" , "long day name")}};
44 	monthNames={
45 		{ 1, qc_("January"  , "long month name")},
46 		{ 2, qc_("February" , "long month name")},
47 		{ 3, qc_("March"    , "long month name")},
48 		{ 4, qc_("April"    , "long month name")},
49 		{ 5, qc_("May"      , "long month name")},
50 		{ 6, qc_("June"     , "long month name")},
51 		{ 7, qc_("July"     , "long month name")},
52 		{ 8, qc_("August"   , "long month name")},
53 		{ 9, qc_("September", "long month name")},
54 		{10, qc_("October"  , "long month name")},
55 		{11, qc_("November" , "long month name")},
56 		{12, qc_("December" , "long month name")}};
57 }
58 
59 // Set a calendar date from the Julian day number
setJD(double JD)60 void JulianCalendar::setJD(double JD)
61 {
62 	this->JD=JD;
63 
64 	int rd=fixedFromJD(JD, true);
65 	parts=julianFromFixed(rd);
66 
67 	emit partsChanged(parts);
68 }
69 
70 // get a stringlist of calendar date elements sorted from the largest to the smallest.
71 // Year, Month, MonthName, Day, DayName
72 // Again, in this plugin only, note no year zero, and AD/BC counting.
getDateStrings() const73 QStringList JulianCalendar::getDateStrings() const
74 {
75 	QStringList list;
76 	list << QString("%1 %2").arg(abs(parts.at(0))).arg(parts.at(0)>0 ? q_("A.D.") : q_("B.C."));
77 	list << QString::number(parts.at(1));
78 	list << QString::number(parts.at(2));
79 	list << weekday(JD);
80 
81 	return list;
82 }
83 
84 // get a formatted complete string for a date
getFormattedDateString() const85 QString JulianCalendar::getFormattedDateString() const
86 {
87 	QStringList str=getDateStrings();
88 	// TODO: Maybe use QDate with user's localisation here? Weekday has to be taken from our results, though.
89 	return QString("%1, %2 - %3 (%4) - %5")
90 			.arg(str.at(3)) // weekday
91 			.arg(str.at(2)) // day
92 			.arg(str.at(1)) // month, numerical
93 			.arg(monthNames.value(parts.at(1))) // month, name
94 			.arg(str.at(0));// year
95 }
96 
97 // set date from a vector of calendar date elements sorted from the largest to the smallest.
98 // Year-Month[1...12]-Day[1...31]
99 // Time is not changed!
setDate(QVector<int> parts)100 void JulianCalendar::setDate(QVector<int> parts)
101 {
102 	//qDebug() << "JulianCalendar::setDate:" << parts;
103 	this->parts=parts;
104 	// For the Julian calendar, we really have no year 0 in this plugin.
105 	Q_ASSERT(parts.at(0) != 0);
106 
107 	double rd=fixedFromJulian(parts);
108 	// restore time from JD!
109 	double frac=StelUtils::fmodpos(JD+0.5+StelApp::getInstance().getCore()->getUTCOffset(JD)/24., 1.);
110 	JD=jdFromFixed(rd+frac, true);
111 
112 	emit jdChanged(JD);
113 }
114 
115 // returns true for leap years. Note that for a calendar without year 0, we must do it more complicated.
isLeap(int year)116 bool JulianCalendar::isLeap(int year)
117 {
118 	return (StelUtils::imod(year, 4) == (year > 0 ? 0 : 3) );
119 }
120 
121 // return name of week day
weekday(double jd)122 QString JulianCalendar::weekday(double jd)
123 {
124 	const int dow = StelUtils::getDayOfWeek(jd+StelApp::getInstance().getCore()->getUTCOffset(jd)/24.);
125 	return weekDayNames.value(dow);
126 }
127 
fixedFromJulian(QVector<int> julian)128 int JulianCalendar::fixedFromJulian(QVector<int> julian)
129 {
130 	const int year=julian.at(0);
131 	const int month=julian.at(1);
132 	const int day=julian.at(2);
133 	const int y=(year<0 ? year+1 : year);
134 
135 	int ret=julianEpoch-1+365*(y-1)+StelUtils::intFloorDiv(y-1, 4)
136 			+StelUtils::intFloorDiv(367*month-362, 12)+day;
137 	if (month>2)
138 		ret+= (isLeap(year) ? -1 : -2);
139 	return ret;
140 }
141 
julianFromFixed(int rd)142 QVector<int> JulianCalendar::julianFromFixed(int rd)
143 {
144 	const int approx=StelUtils::intFloorDiv(4*(rd-julianEpoch)+1464, 1461);
145 	const int year=(approx<=0 ? approx-1 : approx);
146 	const int priorDays=rd-fixedFromJulian({year, january, 1});
147 	int correction=0;
148 	if (rd>=fixedFromJulian({year, march, 1}))
149 		correction=(isLeap(year) ? 1 : 2);
150 	const int month=StelUtils::intFloorDiv(12*(priorDays+correction)+373, 367);
151 	const int day=rd-fixedFromJulian({year, month, 1})+1;
152 	return {year, month, day};
153 }
154