Time Shield Library 2.0.0
C++ library for working with time
Loading...
Searching...
No Matches
julian_conversions.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2#pragma once
3#ifndef TIME_SHIELD_HEADER_ASTRONOMY_JULIAN_CONVERSIONS_HPP_INCLUDED
4#define TIME_SHIELD_HEADER_ASTRONOMY_JULIAN_CONVERSIONS_HPP_INCLUDED
5
16
17#include <time_shield/core.hpp>
18
19#include <cmath>
20#include <cstdint>
21
22namespace time_shield {
23
24 namespace detail {
25
26 inline jd_t gregorian_dmy_to_jd_unchecked(double day, int64_t month, int64_t year) noexcept {
27 if (month == 1 || month == 2) {
28 year -= 1;
29 month += 12;
30 }
31 const double a = std::floor(static_cast<double>(year) / 100.0);
32 const double b = 2.0 - a + std::floor(a / 4.0);
33 const double jd = std::floor(365.25 * (static_cast<double>(year) + 4716.0))
34 + std::floor(30.6000001 * (static_cast<double>(month) + 1.0))
35 + day + b - 1524.5;
36 return static_cast<jd_t>(jd);
37 }
38
39 inline jdn_t gregorian_dmy_to_jdn_unchecked(int64_t day, int64_t month, int64_t year) noexcept {
40 const int64_t a = (14LL - month) / 12LL;
41 const int64_t y = year + 4800LL - a;
42 const int64_t m = month + 12LL * a - 3LL;
43 const int64_t jdn = day
44 + (153LL * m + 2LL) / 5LL
45 + 365LL * y
46 + y / 4LL
47 - y / 100LL
48 + y / 400LL
49 - 32045LL;
50 return static_cast<jdn_t>(jdn);
51 }
52
53 inline double day_fraction_from_hms(
54 int hour,
55 int minute,
56 int second,
57 int millisecond) noexcept {
58 return (static_cast<double>(hour) / 24.0) +
59 (static_cast<double>(minute) / (24.0 * 60.0)) +
60 ((static_cast<double>(second) + static_cast<double>(millisecond) / 1000.0)
61 / static_cast<double>(SEC_PER_DAY));
62 }
63
64 } // namespace detail
65
69 inline jd_t fts_to_jd(fts_t ts) noexcept {
70 return static_cast<jd_t>(2440587.5)
71 + static_cast<jd_t>(ts) / static_cast<jd_t>(SEC_PER_DAY);
72 }
73
77 inline jd_t ts_to_jd(ts_t ts) noexcept {
78 return fts_to_jd(static_cast<fts_t>(ts));
79 }
80
92 int month,
93 int day,
94 int hour = 0,
95 int minute = 0,
96 int second = 0,
97 int millisecond = 0) noexcept {
99 static_cast<double>(day) + detail::day_fraction_from_hms(hour, minute, second, millisecond),
100 static_cast<int64_t>(month),
101 static_cast<int64_t>(year));
102 }
103
107 inline mjd_t fts_to_mjd(fts_t ts) noexcept {
108 return static_cast<mjd_t>(fts_to_jd(ts) - 2400000.5);
109 }
110
114 inline mjd_t ts_to_mjd(ts_t ts) noexcept {
115 return static_cast<mjd_t>(fts_to_mjd(static_cast<fts_t>(ts)));
116 }
117
124 inline jdn_t gregorian_ymd_to_jdn(year_t year, int month, int day) noexcept {
126 static_cast<int64_t>(day),
127 static_cast<int64_t>(month),
128 static_cast<int64_t>(year));
129 }
130
142 year_t year,
143 int month,
144 int day,
145 int hour,
146 int minute,
147 int second,
148 int millisecond,
149 jd_t& out) noexcept {
150 if (!is_valid_date(year, month, day) || !is_valid_time(hour, minute, second, millisecond)) {
151 return false;
152 }
153 out = gregorian_ymd_to_jd(year, month, day, hour, minute, second, millisecond);
154 return true;
155 }
156
164 year_t year,
165 int month,
166 int day,
167 jdn_t& out) noexcept {
168 if (!is_valid_date(year, month, day)) {
169 return false;
170 }
171 const int64_t a = (14LL - static_cast<int64_t>(month)) / 12LL;
172 const int64_t y = static_cast<int64_t>(year) + 4800LL - a;
173 const int64_t m = static_cast<int64_t>(month) + 12LL * a - 3LL;
174 const int64_t jdn = static_cast<int64_t>(day)
175 + (153LL * m + 2LL) / 5LL
176 + 365LL * y
177 + y / 4LL
178 - y / 100LL
179 + y / 400LL
180 - 32045LL;
181 if (jdn < 0) {
182 return false;
183 }
184 out = static_cast<jdn_t>(jdn);
185 return true;
186 }
187
188} // namespace time_shield
189
190#endif // TIME_SHIELD_HEADER_ASTRONOMY_JULIAN_CONVERSIONS_HPP_INCLUDED
constexpr int64_t SEC_PER_DAY
Seconds per day.
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
int64_t year_t
Year as an integer (e.g., 2024).
Definition types.hpp:40
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
ts_t ts() noexcept
Get the current UTC timestamp in seconds.
TIME_SHIELD_CONSTEXPR bool is_valid_date(T1 year, T2 month, T2 day) noexcept
Checks the correctness of the specified date.
TIME_SHIELD_CONSTEXPR bool is_valid_time(T1 hour, T1 min, T1 sec, T2 ms=0) noexcept
Checks the correctness of the specified time.
jdn_t gregorian_dmy_to_jdn_unchecked(int64_t day, int64_t month, int64_t year) noexcept
double day_fraction_from_hms(int hour, int minute, int second, int millisecond) noexcept
jd_t gregorian_dmy_to_jd_unchecked(double day, int64_t month, int64_t year) noexcept
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).
mjd_t fts_to_mjd(fts_t ts) noexcept
Convert Unix timestamp (floating seconds) to Modified Julian Date (MJD).
bool try_gregorian_ymd_to_jd(year_t year, int month, int day, int hour, int minute, int second, int millisecond, jd_t &out) noexcept
Try converting Gregorian date/time components to Julian Date (JD) using year-first order.
jd_t fts_to_jd(fts_t ts) noexcept
Convert Unix timestamp (floating seconds) to Julian Date (JD).
jd_t ts_to_jd(ts_t ts) noexcept
Convert Unix timestamp (seconds) to Julian Date (JD).
bool try_gregorian_ymd_to_jdn(year_t year, int month, int day, jdn_t &out) noexcept
Try converting Gregorian date to Julian Day Number (JDN) using year-first order.
jdn_t gregorian_ymd_to_jdn(year_t year, int month, int day) noexcept
Convert Gregorian date to Julian Day Number (JDN) using year-first order.
jd_t gregorian_ymd_to_jd(year_t year, int month, int day, int hour=0, int minute=0, int second=0, int millisecond=0) noexcept
Convert Gregorian date/time components to Julian Date (JD) using year-first order.