sunset.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Provides the ability to calculate the local time for sunrise,
  3. * sunset, and moonrise at any point in time at any location in the world
  4. *
  5. * Original work used with permission maintaining license
  6. * Copyright (GPL) 2004 Mike Chirico mchirico@comcast.net
  7. * Modifications copyright
  8. * Copyright (GPL) 2015 Peter Buelow
  9. *
  10. * This file is part of the Sunset library
  11. *
  12. * Sunset is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * Sunset is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with Foobar. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef __SUNPOSITION_H__
  26. #define __SUNPOSITION_H__
  27. #include <cmath>
  28. #include <ctime>
  29. #ifndef M_PI
  30. #define M_PI 3.14159265358979323846264338327950288
  31. #endif
  32. /**
  33. * \class SunSet
  34. *
  35. * This class controls all aspects of the operations. The math is done in private
  36. * functions, and the public API's allow for returning a sunrise/sunset value for the
  37. * given coordinates and timezone.
  38. *
  39. * The resulting calculations are relative to midnight of the day you set in the
  40. * setCurrentDate() function. It does not return a time_t value for delta from the
  41. * current epoch as that would not make sense as the sunrise/sunset can be calculated
  42. * thousands of years in the past. The library acts on a day timeframe, and doesn't
  43. * try to assume anything about any other unit of measurement other than the current
  44. * set day.
  45. *
  46. * You can instantiate this class a few different ways, depending on your needs. It's possible
  47. * to set your location one time and forget about doing that again if you don't plan to move.
  48. * Then you only need to change the date and timezone to get correct data. Or, you can
  49. * simply create an object with no location or time data and then do that later. This is
  50. * a good mechanism for the setup()/loop() construct.
  51. *
  52. * The most important thing to remember is to make sure the library knows the exact date and
  53. * timezone for the sunrise/sunset you are trying to calculate. Not doing so means you are going
  54. * to have very odd results. It's reasonably easy to know when you've forgotten one or the other
  55. * by looking at the time the sun would rise and noticing that it is X hours earlier or later.
  56. * That is, if you get a return of 128 minutes (2:08 AM) past midnight when the sun should rise
  57. * at 308 (6:08 AM), then you probably forgot to set your EST timezone.
  58. *
  59. * The library also has no idea about daylight savings time. If your timezone changes during the
  60. * year to account for savings time, you must update your timezone accordingly.
  61. */
  62. class SunSet {
  63. public:
  64. SunSet();
  65. SunSet(double, double, int);
  66. SunSet(double, double, double);
  67. ~SunSet();
  68. static constexpr double SUNSET_OFFICIAL = 90.833; /**< Standard sun angle for sunset */
  69. static constexpr double SUNSET_NAUTICAL = 102.0; /**< Nautical sun angle for sunset */
  70. static constexpr double SUNSET_CIVIL = 96.0; /**< Civil sun angle for sunset */
  71. static constexpr double SUNSET_ASTONOMICAL = 108.0; /**< Astronomical sun angle for sunset */
  72. void setPosition(double, double, int);
  73. void setPosition(double, double, double);
  74. void setTZOffset(int);
  75. void setTZOffset(double);
  76. double setCurrentDate(int, int, int);
  77. double calcNauticalSunrise() const;
  78. double calcNauticalSunset() const;
  79. double calcCivilSunrise() const;
  80. double calcCivilSunset() const;
  81. double calcAstronomicalSunrise() const;
  82. double calcAstronomicalSunset() const;
  83. double calcCustomSunrise(double) const;
  84. double calcCustomSunset(double) const;
  85. [[deprecated("UTC specific calls may not be supported in the future")]] double calcSunriseUTC();
  86. [[deprecated("UTC specific calls may not be supported in the future")]] double calcSunsetUTC();
  87. double calcSunrise() const;
  88. double calcSunset() const;
  89. int moonPhase(int) const;
  90. int moonPhase() const;
  91. private:
  92. double degToRad(double) const;
  93. double radToDeg(double) const;
  94. double calcMeanObliquityOfEcliptic(double) const;
  95. double calcGeomMeanLongSun(double) const;
  96. double calcObliquityCorrection(double) const;
  97. double calcEccentricityEarthOrbit(double) const;
  98. double calcGeomMeanAnomalySun(double) const;
  99. double calcEquationOfTime(double) const;
  100. double calcTimeJulianCent(double) const;
  101. double calcSunTrueLong(double) const;
  102. double calcSunApparentLong(double) const;
  103. double calcSunDeclination(double) const;
  104. double calcHourAngleSunrise(double, double, double) const;
  105. double calcHourAngleSunset(double, double, double) const;
  106. double calcJD(int,int,int) const;
  107. double calcJDFromJulianCent(double) const;
  108. double calcSunEqOfCenter(double) const;
  109. double calcAbsSunrise(double) const;
  110. double calcAbsSunset(double) const;
  111. double m_latitude;
  112. double m_longitude;
  113. double m_julianDate;
  114. double m_tzOffset;
  115. int m_year;
  116. int m_month;
  117. int m_day;
  118. };
  119. #endif