File indexing completed on 2024-12-01 03:39:56

0001 /*
0002     This file is part of the kholidays library.
0003 
0004     SPDX-FileCopyrightText: 2012 Allen Winter <winter@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-or-later
0007 */
0008 
0009 #include "testsunriseset.h"
0010 #include "sunriseset.h"
0011 using namespace KHolidays;
0012 using namespace SunRiseSet;
0013 
0014 #include <QTest>
0015 
0016 QTEST_MAIN(SunriseTest)
0017 
0018 void SunriseTest::TestSunrise()
0019 {
0020     // test bogus latitudes and longitudes
0021     QCOMPARE(utcSunrise(QDate(2012, 1, 1), 1000.0, 1000.0), QTime());
0022     QCOMPARE(utcSunrise(QDate(2012, 7, 1), -1000.0, -1000.0), QTime());
0023     QCOMPARE(utcSunrise(QDate(2012, 12, 31), 1000.0, -1000.0), QTime());
0024 
0025     // NYC
0026     QCOMPARE(utcSunrise(QDate(2012, 1, 1), 40.72, -74.02), QTime(12, 20));
0027     QCOMPARE(utcSunrise(QDate(2012, 7, 1), 40.72, -74.02), QTime(9, 29));
0028     QCOMPARE(utcSunrise(QDate(2012, 12, 31), 40.72, -74.02), QTime(12, 20));
0029 
0030     // LA
0031     QCOMPARE(utcSunrise(QDate(2012, 1, 1), 34.05, -118.23), QTime(14, 58));
0032     QCOMPARE(utcSunrise(QDate(2012, 7, 1), 34.05, -118.23), QTime(12, 45));
0033     QCOMPARE(utcSunrise(QDate(2012, 12, 31), 34.05, -118.23), QTime(14, 58));
0034 
0035     // Berlin
0036     QCOMPARE(utcSunrise(QDate(2012, 1, 1), 51.40, 7.38), QTime(7, 36));
0037     QCOMPARE(utcSunrise(QDate(2012, 7, 1), 51.40, 7.38), QTime(3, 19));
0038     QCOMPARE(utcSunrise(QDate(2012, 12, 31), 51.40, 7.38), QTime(7, 36));
0039 
0040     QCOMPARE(utcSunrise(QDate(2020, 12, 3), 52.5, 13.0), QTime(7, 0));
0041 
0042     // Tasmania
0043     QCOMPARE(utcSunrise(QDate(2012, 1, 1), -14.60, 133.77), QTime(20, 39));
0044     QCOMPARE(utcSunrise(QDate(2012, 7, 1), -14.60, 133.77), QTime(21, 31));
0045     QCOMPARE(utcSunrise(QDate(2012, 12, 31), -14.60, 133.77), QTime(20, 39));
0046 
0047     // Spitsbergen (affected by polar day/night)
0048     QCOMPARE(utcSunrise(QDate(2022, 6, 21), 78.2231, 15.6322), QTime());
0049     QCOMPARE(utcSunrise(QDate(2022, 12, 21), 78.2231, 15.6322), QTime());
0050     QCOMPARE(utcSunrise(QDate(2022, 3, 21), 78.2231, 15.6322), QTime(4, 42));
0051     QCOMPARE(utcSunrise(QDate(2022, 9, 21), 78.2231, 15.6322), QTime(4, 23));
0052 }
0053 
0054 void SunriseTest::TestSunset()
0055 {
0056     // NYC
0057     QCOMPARE(utcSunset(QDate(2012, 1, 1), 40.72, -74.02), QTime(21, 39));
0058     QCOMPARE(utcSunset(QDate(2012, 7, 1), 40.72, -74.02), QTime(00, 31));
0059     QCOMPARE(utcSunset(QDate(2012, 12, 31), 40.72, -74.02), QTime(21, 38));
0060 
0061     // LA
0062     QCOMPARE(utcSunset(QDate(2012, 1, 1), 34.05, -118.23), QTime(00, 54));
0063     QCOMPARE(utcSunset(QDate(2012, 7, 1), 34.05, -118.23), QTime(3, 8));
0064     QCOMPARE(utcSunset(QDate(2012, 12, 31), 34.05, -118.23), QTime(00, 54));
0065 
0066     // Berlin
0067     QCOMPARE(utcSunset(QDate(2012, 1, 1), 51.40, 7.38), QTime(15, 32));
0068     QCOMPARE(utcSunset(QDate(2012, 7, 1), 51.40, 7.38), QTime(19, 50));
0069     QCOMPARE(utcSunset(QDate(2012, 12, 31), 51.40, 7.38), QTime(15, 32));
0070 
0071     // Tasmania
0072     QCOMPARE(utcSunset(QDate(2012, 1, 1), -14.60, 133.77), QTime(9, 37));
0073     QCOMPARE(utcSunset(QDate(2012, 7, 1), -14.60, 133.77), QTime(8, 47));
0074     QCOMPARE(utcSunset(QDate(2012, 12, 31), -14.60, 133.77), QTime(9, 37));
0075 
0076     // Spitsbergen (affected by polar day/night)
0077     QCOMPARE(utcSunset(QDate(2022, 6, 21), 78.2231, 15.6322), QTime());
0078     QCOMPARE(utcSunset(QDate(2022, 12, 21), 78.2231, 15.6322), QTime());
0079     QCOMPARE(utcSunset(QDate(2022, 3, 21), 78.2231, 15.6322), QTime(17, 27));
0080     QCOMPARE(utcSunset(QDate(2022, 9, 21), 78.2231, 15.6322), QTime(17, 18));
0081 }
0082 
0083 void SunriseTest::TestDawn()
0084 {
0085     // NYC
0086     QCOMPARE(utcDawn(QDate(2012, 1, 1), 40.72, -74.02), QTime(11, 49));
0087     QCOMPARE(utcDawn(QDate(2012, 7, 1), 40.72, -74.02), QTime(8, 56));
0088     QCOMPARE(utcDawn(QDate(2012, 12, 31), 40.72, -74.02), QTime(11, 49));
0089 
0090     // LA
0091     QCOMPARE(utcDawn(QDate(2012, 1, 1), 34.05, -118.23), QTime(14, 31));
0092     QCOMPARE(utcDawn(QDate(2012, 7, 1), 34.05, -118.23), QTime(12, 16));
0093     QCOMPARE(utcDawn(QDate(2012, 12, 31), 34.05, -118.23), QTime(14, 31));
0094 
0095     // Berlin
0096     QCOMPARE(utcDawn(QDate(2012, 1, 1), 51.40, 7.38), QTime(6, 56));
0097     QCOMPARE(utcDawn(QDate(2012, 7, 1), 51.40, 7.38), QTime(2, 32));
0098     QCOMPARE(utcDawn(QDate(2012, 12, 31), 51.40, 7.38), QTime(6, 56));
0099 
0100     // Tasmania
0101     QCOMPARE(utcDawn(QDate(2012, 1, 1), -14.60, 133.77), QTime(20, 16));
0102     QCOMPARE(utcDawn(QDate(2012, 7, 1), -14.60, 133.77), QTime(21, 7));
0103     QCOMPARE(utcDawn(QDate(2012, 12, 31), -14.60, 133.77), QTime(20, 15));
0104 }
0105 
0106 void SunriseTest::TestDusk()
0107 {
0108     QCOMPARE(utcDusk(QDate(2012, 1, 1), 40.72, -74.02), QTime(22, 10));
0109     QCOMPARE(utcDusk(QDate(2012, 7, 1), 40.72, -74.02), QTime(1, 4));
0110     QCOMPARE(utcDusk(QDate(2012, 12, 31), 40.72, -74.02), QTime(22, 9));
0111 
0112     // LA
0113     QCOMPARE(utcDusk(QDate(2012, 1, 1), 34.05, -118.23), QTime(1, 22));
0114     QCOMPARE(utcDusk(QDate(2012, 7, 1), 34.05, -118.23), QTime(3, 37));
0115     QCOMPARE(utcDusk(QDate(2012, 12, 31), 34.05, -118.23), QTime(1, 22));
0116 
0117     // Berlin
0118     QCOMPARE(utcDusk(QDate(2012, 1, 1), 51.40, 7.38), QTime(16, 12));
0119     QCOMPARE(utcDusk(QDate(2012, 7, 1), 51.40, 7.38), QTime(20, 37));
0120     QCOMPARE(utcDusk(QDate(2012, 12, 31), 51.40, 7.38), QTime(16, 12));
0121 
0122     // Tasmania
0123     QCOMPARE(utcDusk(QDate(2012, 1, 1), -14.60, 133.77), QTime(10, 1));
0124     QCOMPARE(utcDusk(QDate(2012, 7, 1), -14.60, 133.77), QTime(9, 10));
0125     QCOMPARE(utcDusk(QDate(2012, 12, 31), -14.60, 133.77), QTime(10, 1));
0126 }
0127 
0128 static bool verifyPolarDayRange(QDate begin, QDate end, double latitude, bool expected)
0129 {
0130     for (QDate date = begin; date <= end; date = date.addDays(1)) {
0131         if (isPolarDay(date, latitude) != expected) {
0132             qDebug() << date << latitude << isPolarDay(date, latitude) << expected;
0133             return false;
0134         }
0135         if (expected && utcSunrise(date, latitude, 0.0).isValid() && utcSunset(date, latitude, 0.0).isValid()) {
0136             qDebug() << date << latitude << utcSunrise(date, latitude, 0.0) << utcSunset(date, latitude, 0.0);
0137             return false;
0138         }
0139     }
0140     return true;
0141 }
0142 
0143 static bool verifyPolarTwilightRange(QDate begin, QDate end, double latitude, bool expected)
0144 {
0145     for (QDate date = begin; date <= end; date = date.addDays(1)) {
0146         if (isPolarTwilight(date, latitude) != expected) {
0147             qDebug() << date << latitude << isPolarTwilight(date, latitude) << expected;
0148             return false;
0149         }
0150     }
0151     return true;
0152 }
0153 
0154 static bool verifyPolarNightRange(QDate begin, QDate end, double latitude, bool expected)
0155 {
0156     for (QDate date = begin; date <= end; date = date.addDays(1)) {
0157         if (isPolarNight(date, latitude) != expected) {
0158             qDebug() << date << latitude << isPolarNight(date, latitude) << expected;
0159             return false;
0160         }
0161         if (expected && utcSunrise(date, latitude, 0.0).isValid() && utcSunset(date, latitude, 0.0).isValid()) {
0162             qDebug() << date << latitude << utcSunrise(date, latitude, 0.0) << utcSunset(date, latitude, 0.0);
0163             return false;
0164         }
0165     }
0166     return true;
0167 }
0168 
0169 void SunriseTest::TestPolarDayNight()
0170 {
0171     // polar circle (north): polar day from 6th June to 6st July, no polar night
0172     QVERIFY(verifyPolarDayRange(QDate(2022, 1, 1), QDate(2022, 6, 5), 66.5, false));
0173     QVERIFY(verifyPolarDayRange(QDate(2022, 6, 6), QDate(2022, 7, 5), 66.5, true));
0174     QVERIFY(verifyPolarDayRange(QDate(2022, 7, 6), QDate(2022, 12, 31), 66.5, false));
0175     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 1), QDate(2022, 12, 31), 66.5, false));
0176 
0177     // polar circle (south): same in reverse
0178     QVERIFY(verifyPolarDayRange(QDate(2021, 1, 6), QDate(2021, 12, 7), -66.5, false));
0179     QVERIFY(verifyPolarDayRange(QDate(2021, 12, 8), QDate(2022, 1, 4), -66.5, true));
0180     QVERIFY(verifyPolarDayRange(QDate(2022, 1, 5), QDate(2022, 12, 7), -66.5, false));
0181     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 1), QDate(2022, 12, 31), -66.5, false));
0182 
0183     // 68°: polar twilight from Dec 9 to Jan 2, no polar night
0184     QVERIFY(verifyPolarTwilightRange(QDate(2021, 1, 3), QDate(2021, 12, 8), 68.0, false));
0185     QVERIFY(verifyPolarTwilightRange(QDate(2021, 12, 9), QDate(2022, 1, 2), 68.0, true));
0186     QVERIFY(verifyPolarTwilightRange(QDate(2022, 1, 3), QDate(2022, 12, 8), 68.0, false));
0187     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 1), QDate(2022, 12, 31), 68.0, false));
0188 
0189     // -68°: polar twilight from June 8 to July 3, no polar night
0190     QVERIFY(verifyPolarTwilightRange(QDate(2021, 1, 1), QDate(2021, 6, 7), -68.0, false));
0191     QVERIFY(verifyPolarTwilightRange(QDate(2021, 6, 8), QDate(2021, 7, 3), -68.0, true));
0192     QVERIFY(verifyPolarTwilightRange(QDate(2021, 7, 4), QDate(2021, 12, 31), -68.0, false));
0193     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 1), QDate(2022, 12, 31), -68.0, false));
0194 
0195     // 78.5°: polar night from Nov 11 to Jan 30
0196     QVERIFY(verifyPolarNightRange(QDate(2021, 1, 31), QDate(2021, 11, 10), 78.5, false));
0197     QVERIFY(verifyPolarNightRange(QDate(2021, 11, 11), QDate(2022, 1, 30), 78.5, true));
0198     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 31), QDate(2022, 11, 10), 78.5, false));
0199 
0200     // -78.5°: polar night from May 10 to Aug 2
0201     QVERIFY(verifyPolarNightRange(QDate(2022, 1, 1), QDate(2022, 5, 9), -78.5, false));
0202     QVERIFY(verifyPolarNightRange(QDate(2022, 5, 10), QDate(2022, 8, 2), -78.5, true));
0203     QVERIFY(verifyPolarNightRange(QDate(2022, 8, 3), QDate(2022, 12, 31), -78.5, false));
0204 }
0205 
0206 #include "moc_testsunriseset.cpp"