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
16
17#include "config.hpp"
18#include "types.hpp"
19#include "constants.hpp"
20#include "MoonPhase.hpp"
21#include <cmath>
22
23namespace time_shield {
24
28 inline jd_t fts_to_jd(fts_t ts) noexcept {
29 // JD at Unix epoch:
30 // 1970-01-01 00:00:00 UTC -> 2440587.5
31 return static_cast<jd_t>(2440587.5)
32 + static_cast<jd_t>(ts) / static_cast<jd_t>(SEC_PER_DAY);
33 }
34
38 inline jd_t ts_to_jd(ts_t ts) noexcept {
39 return fts_to_jd(static_cast<fts_t>(ts));
40 }
41
47 inline jd_t gregorian_to_jd(double day, int64_t month, int64_t year) noexcept {
48 // Algorithm source (as per your original code): krutov.org Julianday
49 if (month == 1 || month == 2) {
50 year -= 1;
51 month += 12;
52 }
53 const double a = std::floor(static_cast<double>(year) / 100.0);
54 const double b = 2.0 - a + std::floor(a / 4.0);
55 const double jdn = std::floor(365.25 * (static_cast<double>(year) + 4716.0))
56 + std::floor(30.6000001 * (static_cast<double>(month) + 1.0))
57 + day + b - 1524.5;
58 return static_cast<jd_t>(jdn);
59 }
60
71 uint32_t day,
72 uint32_t month,
73 uint32_t year,
74 uint32_t hour,
75 uint32_t minute,
76 uint32_t second = 0,
77 uint32_t millisecond = 0) noexcept {
78 const double frac =
79 (static_cast<double>(hour) / 24.0) +
80 (static_cast<double>(minute) / (24.0 * 60.0)) +
81 ((static_cast<double>(second) + static_cast<double>(millisecond) / 1000.0)
82 / static_cast<double>(SEC_PER_DAY));
83 return gregorian_to_jd(static_cast<double>(day) + frac,
84 static_cast<int64_t>(month),
85 static_cast<int64_t>(year));
86 }
87
91 inline mjd_t fts_to_mjd(fts_t ts) noexcept {
92 return static_cast<mjd_t>(fts_to_jd(ts) - 2400000.5);
93 }
94
98 inline mjd_t ts_to_mjd(ts_t ts) noexcept {
99 return static_cast<mjd_t>(fts_to_mjd(static_cast<fts_t>(ts)));
100 }
101
108 inline jdn_t gregorian_to_jdn(uint32_t day, uint32_t month, uint32_t year) noexcept {
109 const uint64_t a = (14ULL - static_cast<uint64_t>(month)) / 12ULL;
110 const uint64_t y = static_cast<uint64_t>(year) + 4800ULL - a;
111 const uint64_t m = static_cast<uint64_t>(month) + 12ULL * a - 3ULL;
112 const uint64_t jdn = static_cast<uint64_t>(day)
113 + (153ULL * m + 2ULL) / 5ULL
114 + 365ULL * y
115 + y / 4ULL
116 - y / 100ULL
117 + y / 400ULL
118 - 32045ULL;
119 return static_cast<jdn_t>(jdn);
120 }
121
124 double phase_sin = 0.0;
125 double phase_cos = 0.0;
126 double phase_angle_rad = 0.0;
127 constexpr MoonPhaseSineCosine() = default;
128 constexpr MoonPhaseSineCosine(double phase_sin_value, double phase_cos_value, double phase_angle_rad_value) noexcept
129 : phase_sin(phase_sin_value),
130 phase_cos(phase_cos_value),
131 phase_angle_rad(phase_angle_rad_value) {}
132 };
133
139 inline double moon_phase_jd_approx(fts_t ts) noexcept {
140 double temp = (static_cast<double>(fts_to_jd(ts)) - 2451550.1) / 29.530588853;
141 temp = temp - std::floor(temp);
142 if (temp < 0.0) temp += 1.0;
143 return temp;
144 }
145
149 inline double moon_phase(fts_t ts) noexcept {
150 static const astronomy::MoonPhase calculator{};
151 return calculator.compute(static_cast<double>(ts)).phase;
152 }
153
158 static const astronomy::MoonPhase calculator{};
159 const auto result = calculator.compute(static_cast<double>(ts));
160 return MoonPhaseSineCosine{result.phase_sin, result.phase_cos, result.phase_angle_rad};
161 }
162
166 inline double moon_illumination(fts_t ts) noexcept {
167 static const astronomy::MoonPhase calculator{};
168 return calculator.compute(static_cast<double>(ts)).illumination;
169 }
170
176 inline double moon_age_days_jd_approx(fts_t ts) noexcept {
177 return moon_phase_jd_approx(ts) * 29.530588853;
178 }
179
183 inline double moon_age_days(fts_t ts) noexcept {
184 static const astronomy::MoonPhase calculator{};
185 return calculator.compute(static_cast<double>(ts)).age_days;
186 }
187
192 static const astronomy::MoonPhase calculator{};
193 return calculator.quarter_instants_unix(static_cast<double>(ts));
194 }
195
197 inline bool is_new_moon_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
198 static const astronomy::MoonPhase calculator{};
199 return calculator.is_new_moon_window(static_cast<double>(ts), window_seconds);
200 }
201
203 inline bool is_full_moon_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
204 static const astronomy::MoonPhase calculator{};
205 return calculator.is_full_moon_window(static_cast<double>(ts), window_seconds);
206 }
207
209 inline bool is_first_quarter_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
210 static const astronomy::MoonPhase calculator{};
211 return calculator.is_first_quarter_window(static_cast<double>(ts), window_seconds);
212 }
213
215 inline bool is_last_quarter_window(fts_t ts, double window_seconds = astronomy::MoonPhase::kDefaultQuarterWindow_s) noexcept {
216 static const astronomy::MoonPhase calculator{};
217 return calculator.is_last_quarter_window(static_cast<double>(ts), window_seconds);
218 }
219
220} // namespace time_shield
221
222#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.
Header file with time-related constants.
constexpr int64_t SEC_PER_DAY
Seconds per day.
TIME_SHIELD_CONSTEXPR ts_t ts(year_t year, int month, int day)
Alias for to_timestamp.
TIME_SHIELD_CONSTEXPR T year(ts_t ts=time_shield::ts())
Alias for year_of function.
int64_t ts_t
Unix timestamp in seconds since 1970‑01‑01T00:00:00Z.
Definition types.hpp:48
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:51
uint64_t jdn_t
Julian Day Number (whole days since Julian epoch).
Definition types.hpp:57
double mjd_t
Modified Julian Date (JD − 2400000.5).
Definition types.hpp:56
double jd_t
Julian Date (days since -4713‑11‑24T12:00:00Z).
Definition types.hpp:55
Main namespace for the Time Shield library.
mjd_t ts_to_mjd(ts_t ts) noexcept
Convert Unix timestamp (seconds) to Modified Julian Date (MJD).
jd_t gregorian_to_jd(double day, int64_t month, int64_t year) noexcept
Convert Gregorian date (with optional fractional day) to Julian Date (JD).
mjd_t fts_to_mjd(fts_t ts) noexcept
Convert Unix timestamp (floating seconds) to Modified Julian Date (MJD).
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).
jd_t ts_to_jd(ts_t ts) noexcept
Convert Unix timestamp (seconds) to Julian Date (JD).
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).
jdn_t gregorian_to_jdn(uint32_t day, uint32_t month, uint32_t year) noexcept
Convert Gregorian date to Julian Day Number (JDN).
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
Type definitions for time-related units and formats.