|
| 1 | +/**The MIT License (MIT) |
| 2 | +
|
| 3 | +Copyright (c) 2018 by Daniel Eichhorn, ThingPulse |
| 4 | +
|
| 5 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +of this software and associated documentation files (the "Software"), to deal |
| 7 | +in the Software without restriction, including without limitation the rights |
| 8 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +copies of the Software, and to permit persons to whom the Software is |
| 10 | +furnished to do so, subject to the following conditions: |
| 11 | +
|
| 12 | +The above copyright notice and this permission notice shall be included in all |
| 13 | +copies or substantial portions of the Software. |
| 14 | +
|
| 15 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +SOFTWARE. |
| 22 | +
|
| 23 | +See more at https://thingpulse.com |
| 24 | +*/ |
| 25 | +#include <time.h> |
| 26 | +#include "Astronomy.h" |
| 27 | + |
| 28 | +Astronomy::Astronomy() { |
| 29 | + |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | +* Convenience method to calculate moonphase by unix time stamp. |
| 34 | +* See calculateMoonPhase(int year, int month, int day) for more details. |
| 35 | +*/ |
| 36 | +uint8_t Astronomy::calculateMoonPhase(time_t timestamp) { |
| 37 | + struct tm* timeInfo; |
| 38 | + timeInfo = localtime(×tamp); |
| 39 | + return calculateMoonPhase(timeInfo->tm_year + 1900, timeInfo->tm_mon + 1, timeInfo->tm_mday); |
| 40 | +} |
| 41 | +/** |
| 42 | + * Calculates the moon phase for a given date, accurate to 1 segment. The result is in the range 0..7. |
| 43 | + * 0 => new moon |
| 44 | + * 4 => full moon |
| 45 | + * |
| 46 | + * Source: https://www.voidware.com/moon_phase.htm |
| 47 | + */ |
| 48 | +uint8_t Astronomy::calculateMoonPhase(uint16_t year, uint16_t month, uint16_t day) { |
| 49 | + uint16_t yearAsDays, monthAsDays; |
| 50 | + double daysSinceReferenceNewMoon; |
| 51 | + double moonCycles; |
| 52 | + uint16_t completedMoonCycles; |
| 53 | + double moonAge; |
| 54 | + uint8_t phase; |
| 55 | + |
| 56 | + if (month < 3) { |
| 57 | + year--; |
| 58 | + month += 12; |
| 59 | + } |
| 60 | + ++month; /* why incrementing the month? */ |
| 61 | + |
| 62 | + yearAsDays = 365.25 * year; /* 365.25 -> mean length of a calendar year */ |
| 63 | + monthAsDays = 30.6 * month; /* 30.6 -> mean length of a month */ |
| 64 | + daysSinceReferenceNewMoon = yearAsDays + monthAsDays + day - 694039.09; /* number of days since known new moon on 1900-01-01, 694039.09 -> days elapsed since zero */ |
| 65 | + moonCycles = daysSinceReferenceNewMoon / 29.53; /* 29.53 -> long-term average moon cycle duration in days */ |
| 66 | + completedMoonCycles = moonCycles; /* "casting" to int to get *completed* moon cycles i.e. only integer part */ |
| 67 | + moonAge = moonCycles - completedMoonCycles; /* subtract integer part to leave fractional part which represents the current moon age */ |
| 68 | + phase = moonAge * 8 + 0.5; /* scale fraction from 0-8 and round by adding 0.5 */ |
| 69 | + phase = phase & 7; /* 0 and 8 are the same so turn 8 into 0 */ |
| 70 | + return phase; |
| 71 | +} |
0 commit comments