Changeset View
Changeset View
Standalone View
Standalone View
colorcorrection/suncalc.cpp
Show All 15 Lines | |||||
16 | 16 | | |||
17 | You should have received a copy of the GNU General Public License | 17 | You should have received a copy of the GNU General Public License | ||
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
19 | *********************************************************************/ | 19 | *********************************************************************/ | ||
20 | #include "suncalc.h" | 20 | #include "suncalc.h" | ||
21 | #include "constants.h" | 21 | #include "constants.h" | ||
22 | 22 | | |||
23 | #include <QDateTime> | 23 | #include <QDateTime> | ||
24 | #include <QTimeZone> | ||||
24 | #include <QtMath> | 25 | #include <QtMath> | ||
25 | 26 | | |||
26 | namespace KWin { | 27 | namespace KWin { | ||
27 | namespace ColorCorrect { | 28 | namespace ColorCorrect { | ||
28 | 29 | | |||
29 | #define TWILIGHT_NAUT -12.0 | 30 | #define TWILIGHT_NAUT -12.0 | ||
30 | #define TWILIGHT_CIVIL -6.0 | 31 | #define TWILIGHT_CIVIL -6.0 | ||
31 | #define SUN_RISE_SET -0.833 | 32 | #define SUN_RISE_SET -0.833 | ||
32 | #define SUN_HIGH 2.0 | 33 | #define SUN_HIGH 2.0 | ||
33 | 34 | | |||
34 | QPair<QTime, QTime> calculateSunTimings(QDate prompt, double latitude, double longitude, bool morning) | 35 | static QTime convertToLocalTime(const QDateTime &when, const QTime &utcTime) | ||
36 | { | ||||
37 | const QTimeZone timeZone = QTimeZone::systemTimeZone(); | ||||
38 | const int utcOffset = timeZone.offsetFromUtc(when); | ||||
39 | return utcTime.addSecs(utcOffset); | ||||
40 | } | ||||
41 | | ||||
42 | QPair<QDateTime, QDateTime> calculateSunTimings(const QDateTime &dateTime, double latitude, double longitude, bool morning) | ||||
35 | { | 43 | { | ||
36 | // calculations based on https://aa.quae.nl/en/reken/zonpositie.html | 44 | // calculations based on https://aa.quae.nl/en/reken/zonpositie.html | ||
37 | // accuracy: +/- 5min | 45 | // accuracy: +/- 5min | ||
38 | 46 | | |||
39 | // positioning | 47 | // positioning | ||
40 | const double rad = M_PI / 180.; | 48 | const double rad = M_PI / 180.; | ||
41 | const double earthObliquity = 23.4397; // epsilon | 49 | const double earthObliquity = 23.4397; // epsilon | ||
42 | 50 | | |||
43 | const double lat = latitude; // phi | 51 | const double lat = latitude; // phi | ||
44 | const double lng = -longitude; // lw | 52 | const double lng = -longitude; // lw | ||
45 | 53 | | |||
46 | // times | 54 | // times | ||
47 | const double juPrompt = prompt.toJulianDay(); // J | 55 | const QDateTime utcDateTime = dateTime.toUTC(); | ||
56 | const double juPrompt = utcDateTime.date().toJulianDay(); // J | ||||
48 | const double ju2000 = 2451545.; // J2000 | 57 | const double ju2000 = 2451545.; // J2000 | ||
49 | 58 | | |||
50 | // geometry | 59 | // geometry | ||
51 | auto mod360 = [](double number) -> double { | 60 | auto mod360 = [](double number) -> double { | ||
52 | return std::fmod(number, 360.); | 61 | return std::fmod(number, 360.); | ||
53 | }; | 62 | }; | ||
54 | 63 | | |||
55 | auto sin = [&rad](double angle) -> double { | 64 | auto sin = [&rad](double angle) -> double { | ||
▲ Show 20 Lines • Show All 80 Lines • ▼ Show 20 Line(s) | |||||
136 | } else { | 145 | } else { | ||
137 | begin = getSunEvening(SUN_HIGH, juNoon); | 146 | begin = getSunEvening(SUN_HIGH, juNoon); | ||
138 | end = getSunEvening(TWILIGHT_CIVIL, juNoon); | 147 | end = getSunEvening(TWILIGHT_CIVIL, juNoon); | ||
139 | } | 148 | } | ||
140 | // transform to QDateTime | 149 | // transform to QDateTime | ||
141 | begin += 0.5; | 150 | begin += 0.5; | ||
142 | end += 0.5; | 151 | end += 0.5; | ||
143 | 152 | | |||
144 | QTime timeBegin, timeEnd; | 153 | QDateTime dateTimeBegin; | ||
154 | QDateTime dateTimeEnd; | ||||
145 | 155 | | |||
146 | if (std::isnan(begin)) { | 156 | if (!std::isnan(begin)) { | ||
147 | timeBegin = QTime(); | 157 | const double dayFraction = begin - int(begin); | ||
romangg: Why call this variable fraction now what is less descriptive about what it represents? | |||||
The calculated number is fraction of a day. I'll change it to dayFraction. zzag: The calculated number is fraction of a day. I'll change it to dayFraction. | |||||
148 | } else { | 158 | const QTime utcTime = QTime::fromMSecsSinceStartOfDay(dayFraction * MSC_DAY); | ||
149 | double timePart = begin - (int)begin; | 159 | const QTime localTime = convertToLocalTime(dateTime, utcTime); | ||
150 | timeBegin = QTime::fromMSecsSinceStartOfDay((int)( timePart * MSC_DAY )); | 160 | dateTimeBegin = QDateTime(dateTime.date(), localTime); | ||
151 | } | 161 | } | ||
152 | if (std::isnan(end)) { | 162 | | ||
153 | timeEnd = QTime(); | 163 | if (!std::isnan(end)) { | ||
154 | } else { | 164 | const double dayFraction = end - int(end); | ||
romangg: Here as well. | |||||
155 | double timePart = end - (int)end; | 165 | const QTime utcTime = QTime::fromMSecsSinceStartOfDay(dayFraction * MSC_DAY); | ||
156 | timeEnd = QTime::fromMSecsSinceStartOfDay((int)( timePart * MSC_DAY )); | 166 | const QTime localTime = convertToLocalTime(dateTime, utcTime); | ||
167 | dateTimeEnd = QDateTime(dateTime.date(), localTime); | ||||
157 | } | 168 | } | ||
158 | 169 | | |||
159 | return QPair<QTime,QTime> (timeBegin, timeEnd); | 170 | return { dateTimeBegin, dateTimeEnd }; | ||
160 | } | 171 | } | ||
161 | 172 | | |||
162 | } | 173 | } | ||
163 | } | 174 | } |
Why call this variable fraction now what is less descriptive about what it represents?