Time Shield Library
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_JULIAN_CONVERSIONS_HPP_INCLUDED
4#define _TIME_SHIELD_JULIAN_CONVERSIONS_HPP_INCLUDED
5
17
18#include "config.hpp"
19#include "types.hpp"
20#include "constants.hpp"
21#include "validation.hpp"
22
23#include <cmath>
24#include <cstdint>
25
26namespace time_shield {
27
28 namespace detail {
29
30 inline jd_t gregorian_dmy_to_jd_unchecked(double day, int64_t month, int64_t year) noexcept {
31 if (month == 1 || month == 2) {
32 year -= 1;
33 month += 12;
34 }
35 const double a = std::floor(static_cast<double>(year) / 100.0);
36 const double b = 2.0 - a + std::floor(a / 4.0);
37 const double jd = std::floor(365.25 * (static_cast<double>(year) + 4716.0))
38 + std::floor(30.6000001 * (static_cast<double>(month) + 1.0))
39 + day + b - 1524.5;
40 return static_cast<jd_t>(jd);
41 }
42
43 inline jdn_t gregorian_dmy_to_jdn_unchecked(int64_t day, int64_t month, int64_t year) noexcept {
44 const int64_t a = (14LL - month) / 12LL;
45 const int64_t y = year + 4800LL - a;
46 const int64_t m = month + 12LL * a - 3LL;
47 const int64_t jdn = day
48 + (153LL * m + 2LL) / 5LL
49 + 365LL * y
50 + y / 4LL
51 - y / 100LL
52 + y / 400LL
53 - 32045LL;
54 return static_cast<jdn_t>(jdn);
55 }
56
57 inline double day_fraction_from_hms(
58 int hour,
59 int minute,
60 int second,
61 int millisecond) noexcept {
62 return (static_cast<double>(hour) / 24.0) +
63 (static_cast<double>(minute) / (24.0 * 60.0)) +
64 ((static_cast<double>(second) + static_cast<double>(millisecond) / 1000.0)
65 / static_cast<double>(SEC_PER_DAY));
66 }
67
68 } // namespace detail
69
73 inline jd_t fts_to_jd(fts_t ts) noexcept {
74 return static_cast<jd_t>(2440587.5)
75 + static_cast<jd_t>(ts) / static_cast<jd_t>(SEC_PER_DAY);
76 }
77
81 inline jd_t ts_to_jd(ts_t ts) noexcept {
82 return fts_to_jd(static_cast<fts_t>(ts));
83 }
84
90 inline jd_t gregorian_to_jd(double day, int64_t month, int64_t year) noexcept {
92 }
93
104 uint32_t day,
105 uint32_t month,
106 uint32_t year,
107 uint32_t hour,
108 uint32_t minute,
109 uint32_t second = 0,
110 uint32_t millisecond = 0) noexcept {
112 static_cast<double>(day) + detail::day_fraction_from_hms(
113 static_cast<int>(hour),
114 static_cast<int>(minute),
115 static_cast<int>(second),
116 static_cast<int>(millisecond)),
117 static_cast<int64_t>(month),
118 static_cast<int64_t>(year));
119 }
120
131 year_t year,
132 int month,
133 int day,
134 int hour = 0,
135 int minute = 0,
136 int second = 0,
137 int millisecond = 0) noexcept {
139 static_cast<double>(day) + detail::day_fraction_from_hms(hour, minute, second, millisecond),
140 static_cast<int64_t>(month),
141 static_cast<int64_t>(year));
142 }
143
147 inline mjd_t fts_to_mjd(fts_t ts) noexcept {
148 return static_cast<mjd_t>(fts_to_jd(ts) - 2400000.5);
149 }
150
154 inline mjd_t ts_to_mjd(ts_t ts) noexcept {
155 return static_cast<mjd_t>(fts_to_mjd(static_cast<fts_t>(ts)));
156 }
157
164 inline jdn_t gregorian_to_jdn(uint32_t day, uint32_t month, uint32_t year) noexcept {
166 static_cast<int64_t>(day),
167 static_cast<int64_t>(month),
168 static_cast<int64_t>(year));
169 }
170
177 inline jdn_t gregorian_ymd_to_jdn(year_t year, int month, int day) noexcept {
179 static_cast<int64_t>(day),
180 static_cast<int64_t>(month),
181 static_cast<int64_t>(year));
182 }
183
195 year_t year,
196 int month,
197 int day,
198 int hour,
199 int minute,
200 int second,
201 int millisecond,
202 jd_t& out) noexcept {
203 if (!is_valid_date(year, month, day) || !is_valid_time(hour, minute, second, millisecond)) {
204 return false;
205 }
206 out = gregorian_ymd_to_jd(year, month, day, hour, minute, second, millisecond);
207 return true;
208 }
209
217 year_t year,
218 int month,
219 int day,
220 jdn_t& out) noexcept {
221 if (!is_valid_date(year, month, day)) {
222 return false;
223 }
224 const int64_t a = (14LL - static_cast<int64_t>(month)) / 12LL;
225 const int64_t y = static_cast<int64_t>(year) + 4800LL - a;
226 const int64_t m = static_cast<int64_t>(month) + 12LL * a - 3LL;
227 const int64_t jdn = static_cast<int64_t>(day)
228 + (153LL * m + 2LL) / 5LL
229 + 365LL * y
230 + y / 4LL
231 - y / 100LL
232 + y / 400LL
233 - 32045LL;
234 if (jdn < 0) {
235 return false;
236 }
237 out = static_cast<jdn_t>(jdn);
238 return true;
239 }
240
241} // namespace time_shield
242
243#endif // _TIME_SHIELD_JULIAN_CONVERSIONS_HPP_INCLUDED
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:49
double fts_t
Floating-point timestamp (fractional seconds since epoch).
Definition types.hpp:52
uint64_t jdn_t
Julian Day Number (whole days since Julian epoch).
Definition types.hpp:58
int64_t year_t
Year as an integer (e.g., 2024).
Definition types.hpp:41
double mjd_t
Modified Julian Date (JD − 2400000.5).
Definition types.hpp:57
double jd_t
Julian Date (days since -4713‑11‑24T12:00:00Z).
Definition types.hpp:56
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).
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).
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.
jdn_t gregorian_to_jdn(uint32_t day, uint32_t month, uint32_t year) noexcept
Convert Gregorian date to Julian Day Number (JDN).
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.
Type definitions for time-related units and formats.
Header file with time-related validation functions.