KHolidays

lunarphase.cpp
1/*
2 This file is part of the kholidays library.
3
4 SPDX-FileCopyrightText: 2004, 2007, 2009 Allen Winter <winter@kde.org>
5
6 SPDX-License-Identifier: LGPL-2.0-or-later
7*/
8
9#include "lunarphase.h"
10
11#include <QCoreApplication>
12#include <QDateTime>
13#include <QTimeZone>
14
15#include <numbers>
16
17using namespace KHolidays;
18
20{
21 return phaseName(phaseAtDate(date));
22}
23
25{
26 switch (phase) {
27 case NewMoon:
28 return (QCoreApplication::translate("LunarPhase", "New Moon"));
29 case FullMoon:
30 return (QCoreApplication::translate("LunarPhase", "Full Moon"));
31 case FirstQuarter:
32 return (QCoreApplication::translate("LunarPhase", "First Quarter Moon"));
33 case LastQuarter:
34 return (QCoreApplication::translate("LunarPhase", "Last Quarter Moon"));
35 case None:
36 return QString();
37 case WaxingCrescent:
38 return (QCoreApplication::translate("LunarPhase", "Waxing Crescent"));
39 case WaxingGibbous:
40 return (QCoreApplication::translate("LunarPhase", "Waxing Gibbous"));
41 case WaningGibbous:
42 return (QCoreApplication::translate("LunarPhase", "Waning Gibbous"));
43 case WaningCrescent:
44 return (QCoreApplication::translate("LunarPhase", "Waning Crescent"));
45 }
46 return QString();
47}
48
49static double phaseAngle(int64_t unixmsec);
50
52{
53 Phase retPhase = None;
54
55 const QTime midnight(0, 0, 0);
56 const QDateTime todayStart(date, midnight, QTimeZone::utc());
57 const double startAngle = phaseAngle(todayStart.toMSecsSinceEpoch());
58 const QDateTime todayEnd(date.addDays(1), midnight, QTimeZone::utc());
59 const double endAngle = phaseAngle(todayEnd.toMSecsSinceEpoch());
60
61 if (startAngle > endAngle) {
62 retPhase = NewMoon;
63 } else if (startAngle < 90.0 && endAngle > 90.0) {
64 retPhase = FirstQuarter;
65 } else if (startAngle < 180.0 && endAngle > 180.0) {
66 retPhase = FullMoon;
67 } else if (startAngle < 270.0 && endAngle > 270.0) {
68 retPhase = LastQuarter;
69 } else if (endAngle < 90.0) {
70 retPhase = WaxingCrescent;
71 } else if (endAngle < 180.0) {
72 retPhase = WaxingGibbous;
73 } else if (endAngle < 270.0) {
74 retPhase = WaningGibbous;
75 } else if (endAngle < 360.0) {
76 retPhase = WaningCrescent;
77 }
78
79 return retPhase;
80}
81
82/*
83 Algorithm adapted from Moontool by John Walker.
84 Moontool is in the public domain: "Do what thou wilt shall be the whole of the law".
85 Unnecessary calculations have been removed.
86 See https://fourmilab.ch/moontool .
87*/
88
89#include <cmath>
90
91constexpr int64_t epoch = 315446400000; // "1980 January 0.0", a.k.a. midnight on 1979-12-31, in ms Unix time
92constexpr double elonge = 278.833540; // ecliptic longitude of sun at epoch
93constexpr double elongp = 282.596403; // ecliptic longitude of sun at perigee
94constexpr double earthEcc = 0.016718; // eccentricity of earth orbit
95static const double ecPrefactor = sqrt((1 + earthEcc) / (1 - earthEcc));
96
97constexpr double mmlong = 64.975464; // mean longitude of moon at epoch
98constexpr double mmlongp = 349.383063; // mean longitude of moon at perigee
99
100static double fixAngle(double degrees)
101{
102 return degrees - floor(degrees / 360.0) * 360.0;
103}
104
105static constexpr double radToDeg(double rad)
106{
107 return rad / std::numbers::pi * 180.0;
108}
109
110static constexpr double degToRad(double deg)
111{
112 return deg / 180.0 * std::numbers::pi;
113}
114
115constexpr double epsilon = 1e-6;
116
117static double kepler(double m, double ecc)
118{
119 double mrad = degToRad(m);
120 double e = mrad;
121 double delta;
122 do {
123 delta = e - ecc * sin(e) - mrad;
124 e -= delta / (1 - ecc * cos(e));
125 } while (abs(delta) > epsilon);
126 return e;
127}
128
129static double phaseAngle(int64_t unixmsec)
130{
131 int64_t msec = unixmsec - epoch;
132
133 double sunMeanAnomaly = fixAngle(msec * (360.0 / 365.2422 / 86400000.0) + elonge - elongp);
134 double trueAnomaly = 2 * radToDeg(atan(ecPrefactor * tan(kepler(sunMeanAnomaly, earthEcc) / 2)));
135 double geocentricEclipticLong = fixAngle(trueAnomaly + elongp);
136
137 double moonMeanLong = fixAngle(msec * (13.1763966 / 86400000.0) + mmlong);
138 double moonMeanAnomaly = fixAngle(moonMeanLong - msec * (0.1114041 / 86400000.0) - mmlongp);
139 double evection = 1.2739 * sin(degToRad(2 * (moonMeanLong - geocentricEclipticLong) - moonMeanAnomaly));
140 double annualEquation = 0.1858 * sin(degToRad(sunMeanAnomaly));
141 double a3 = 0.37 * sin(degToRad(sunMeanAnomaly));
142 double correctedAnomaly = moonMeanAnomaly + evection - annualEquation - a3;
143 double centreCorrection = 6.2886 * sin(degToRad(correctedAnomaly));
144 double a4 = 0.214 * sin(degToRad(2 * correctedAnomaly));
145 double correctedLong = moonMeanLong + evection + centreCorrection - annualEquation + a4;
146 double variation = 0.6583 * sin(degToRad(2 * (correctedLong - geocentricEclipticLong)));
147 double trueLong = correctedLong + variation;
148
149 return fixAngle(trueLong - geocentricEclipticLong);
150}
151
152#include "moc_lunarphase.cpp"
static QString phaseNameAtDate(const QDate &date)
Return the lunar phase as a text string for the specified date.
static Phase phaseAtDate(const QDate &date)
Return the lunar phase for the specified Gregorian date.
Phase
Phases of the moon, in traditional English notation.
Definition lunarphase.h:54
@ FullMoon
Full moon phase.
Definition lunarphase.h:58
@ FirstQuarter
First quarter of moon phase.
Definition lunarphase.h:56
@ None
Indication for error.
Definition lunarphase.h:59
@ LastQuarter
Last quarter of moon phase.
Definition lunarphase.h:57
@ NewMoon
New moon phase.
Definition lunarphase.h:55
static QString phaseName(Phase phase)
Return the string representation of phase.
constexpr double radToDeg(double rad)
constexpr double degToRad(double deg)
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
QDate addDays(qint64 ndays) const const
qint64 toMSecsSinceEpoch() const const
QTimeZone utc()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:11:11 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.