/// <summary> /// Calculate the current phase of the moon. /// Note: this calculation uses the last recorded new moon to calculate the cycles of /// of the moon since then. Any date in the past before 1920 might not work. /// </summary> /// <param name="utcDateTime"></param> /// <remarks>https://www.subsystems.us/uploads/9/8/9/4/98948044/moonphase.pdf</remarks> /// <returns></returns> public static PhaseResult Calculate(DateTime utcDateTime, Earth.Hemispheres viewFromEarth = Earth.Hemispheres.Northern) { const double julianConstant = 2415018.5; var julianDate = utcDateTime.ToOADate() + julianConstant; // London New Moon (1920) // https://www.timeanddate.com/moon/phases/uk/london?year=1920 var daysSinceLastNewMoon = new DateTime(1920, 1, 21, 5, 25, 00, DateTimeKind.Utc).ToOADate() + julianConstant; var newMoons = (julianDate - daysSinceLastNewMoon) / TotalLengthOfCycle; var intoCycle = (newMoons - Math.Truncate(newMoons)) * TotalLengthOfCycle; var phase = allPhases.First(p => intoCycle >= p.Start && intoCycle <= p.End); var index = allPhases.IndexOf(phase); var currentPhase = viewFromEarth switch { Earth.Hemispheres.Northern => NorthernHemisphere[index], _ => SouthernHemisphere[index] }; return(new PhaseResult ( phase.Name, currentPhase, Math.Round(intoCycle, 2), viewFromEarth, utcDateTime )); }
public PhaseResult(string name, string emoji, double daysIntoCycle, Earth.Hemispheres hemisphere, DateTime moment) { Name = name; Emoji = emoji; DaysIntoCycle = daysIntoCycle; Hemisphere = hemisphere; Moment = moment; }
public static PhaseResult Now(Earth.Hemispheres viewFromEarth = Earth.Hemispheres.Northern) { return(Calculate(DateTime.Now.ToUniversalTime(), viewFromEarth)); }
public static PhaseResult UtcNow(Earth.Hemispheres viewFromEarth = Earth.Hemispheres.Northern) { return(Calculate(DateTime.UtcNow, viewFromEarth)); }
public static PhaseResult PhaseFromUserTime(DateTime userTime, Earth.Hemispheres viewFromEarth = Earth.Hemispheres.Northern) // My addition { return(Calculate(userTime, viewFromEarth)); }