Time Shield Library
C++ library for working with time
Loading...
Searching...
No Matches
astronomy_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef _TIME_SHIELD_ASTRONOMY_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_ASTRONOMY_CONVERSIONS_HPP_INCLUDED
5
9
10#include "config.hpp"
12#include "MoonPhase.hpp"
13
14#include <cmath>
15
16namespace time_shield {
17
20 double phase_sin = 0.0;
21 double phase_cos = 0.0;
22 double phase_angle_rad = 0.0;
23 constexpr MoonPhaseSineCosine() = default;
24 constexpr MoonPhaseSineCosine(double phase_sin_value, double phase_cos_value, double phase_angle_rad_value) noexcept
25 : phase_sin(phase_sin_value),
26 phase_cos(phase_cos_value),
27 phase_angle_rad(phase_angle_rad_value) {}
28 };
29
35 inline double moon_phase_jd_approx(fts_t ts) noexcept {
36 double temp = (static_cast<double>(fts_to_jd(ts)) - 2451550.1) / 29.530588853;
37 temp = temp - std::floor(temp);
38 if (temp < 0.0) temp += 1.0;
39 return temp;
40 }
41
45 inline double moon_phase(fts_t ts) noexcept {
46 static const astronomy::MoonPhase calculator{};
47 return calculator.compute(static_cast<double>(ts)).phase;
48 }
49
54 static const astronomy::MoonPhase calculator{};
55 const auto result = calculator.compute(static_cast<double>(ts));
56 return MoonPhaseSineCosine{result.phase_sin, result.phase_cos, result.phase_angle_rad};
57 }
58
62 inline double moon_illumination(fts_t ts) noexcept {
63 static const astronomy::MoonPhase calculator{};
64 return calculator.compute(static_cast<double>(ts)).illumination;
65 }
66
72 inline double moon_age_days_jd_approx(fts_t ts) noexcept {
73 return moon_phase_jd_approx(ts) * 29.530588853;
74 }
75
79 inline double moon_age_days(fts_t ts) noexcept {
80 static const astronomy::MoonPhase calculator{};
81 return calculator.compute(static_cast<double>(ts)).age_days;
82 }
83
88 static const astronomy::MoonPhase calculator{};
89 return calculator.quarter_instants_unix(static_cast<double>(ts));
90 }
91
93 inline bool is_new_moon_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
94 static const astronomy::MoonPhase calculator{};
95 return calculator.is_new_moon_window(static_cast<double>(ts), window_seconds);
96 }
97
99 inline bool is_full_moon_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
100 static const astronomy::MoonPhase calculator{};
101 return calculator.is_full_moon_window(static_cast<double>(ts), window_seconds);
102 }
103
105 inline bool is_first_quarter_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
106 static const astronomy::MoonPhase calculator{};
107 return calculator.is_first_quarter_window(static_cast<double>(ts), window_seconds);
108 }
109
111 inline bool is_last_quarter_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
112 static const astronomy::MoonPhase calculator{};
113 return calculator.is_last_quarter_window(static_cast<double>(ts), window_seconds);
114 }
115
116} // namespace time_shield
117
118#endif // _TIME_SHIELD_ASTRONOMY_CONVERSIONS_HPP_INCLUDED
Geocentric Moon phase calculator and result helpers.
Moon phase calculator (geocentric approximation).
Definition MoonPhase.hpp:68
bool is_new_moon_window(double unix_utc_s, double window_seconds=kDefaultQuarterWindow_s) const noexcept
Check whether timestamp is inside a window around new moon.
bool is_first_quarter_window(double unix_utc_s, double window_seconds=kDefaultQuarterWindow_s) const noexcept
Check whether timestamp is inside a window around first quarter.
MoonPhaseResult compute(double unix_utc_s) const noexcept
Compute full set of Moon phase parameters for given UTC timestamp.
Definition MoonPhase.hpp:76
MoonQuarterInstants quarter_instants_unix(double unix_utc_s) const noexcept
Quarter instants around the provided timestamp as a structured result (Unix UTC seconds).
static constexpr double kDefaultQuarterWindow_s
Default window around phase events (12h).
Definition MoonPhase.hpp:71
bool is_last_quarter_window(double unix_utc_s, double window_seconds=kDefaultQuarterWindow_s) const noexcept
Check whether timestamp is inside a window around last quarter.
bool is_full_moon_window(double unix_utc_s, double window_seconds=kDefaultQuarterWindow_s) const noexcept
Check whether timestamp is inside a window around full moon.
Configuration macros for the library.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:52
Julian Date / MJD / JDN helpers using the proleptic Gregorian calendar.
Main namespace for the Time Shield library.
double moon_age_days_jd_approx(fts_t ts) noexcept
Get lunar age in days (~0..29.53) using a simple Julian Day approximation.
bool is_new_moon_window(fts_t ts, double window_seconds=astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept
Check if timestamp falls into the new moon window (default \pm12h).
jd_t fts_to_jd(fts_t ts) noexcept
Convert Unix timestamp (floating seconds) to Julian Date (JD).
bool is_last_quarter_window(fts_t ts, double window_seconds=astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept
Check if timestamp falls into the last quarter window (default \pm12h).
double moon_age_days(fts_t ts) noexcept
Get lunar age in days (~0..29.53).
double moon_phase(fts_t ts) noexcept
Get lunar phase in range [0..1) using the geocentric MoonPhase calculator.
double moon_phase_jd_approx(fts_t ts) noexcept
Get lunar phase in range [0..1) using a simple Julian Day approximation.
bool is_full_moon_window(fts_t ts, double window_seconds=astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept
Check if timestamp falls into the full moon window (default \pm12h).
astronomy::MoonQuarterInstants moon_quarters(fts_t ts) noexcept
Quarter instants around the provided timestamp.
double moon_illumination(fts_t ts) noexcept
Get illuminated fraction in range [0..1] using the geocentric MoonPhase calculator.
bool is_first_quarter_window(fts_t ts, double window_seconds=astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept
Check if timestamp falls into the first quarter window (default \pm12h).
MoonPhaseSineCosine moon_phase_sincos(fts_t ts) noexcept
Get sin/cos of the lunar phase angle (continuous signal without wrap-around).
sin/cos helper for the Moon phase angle.
double phase_sin
sin(phase angle), continuous around 0/2pi.
constexpr MoonPhaseSineCosine()=default
double phase_angle_rad
Phase angle in radians [0..2*pi).
double phase_cos
cos(phase angle), continuous around 0/2pi.
constexpr MoonPhaseSineCosine(double phase_sin_value, double phase_cos_value, double phase_angle_rad_value) noexcept
double phase
Phase fraction in [0..1). 0=new moon, 0.5=full moon.
Definition MoonPhase.hpp:23
double age_days
Age of the Moon in days since new moon (approx).
Definition MoonPhase.hpp:25
double illumination
Illuminated fraction in [0..1].
Definition MoonPhase.hpp:24
Lunar quarter instants (Unix UTC seconds, floating).
Definition MoonPhase.hpp:37