siderust-js 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (272) hide show
  1. package/.github/workflows/ci.yml +166 -0
  2. package/.gitmodules +9 -0
  3. package/CHANGELOG.md +26 -0
  4. package/LICENSE +661 -0
  5. package/README.md +138 -0
  6. package/package.json +12 -0
  7. package/qtty-js/.github/workflows/ci.yml +151 -0
  8. package/qtty-js/.gitmodules +3 -0
  9. package/qtty-js/CHANGELOG.md +31 -0
  10. package/qtty-js/LICENSE +661 -0
  11. package/qtty-js/README.md +132 -0
  12. package/qtty-js/package.json +20 -0
  13. package/qtty-js/qtty/.github/workflows/ci.yml +155 -0
  14. package/qtty-js/qtty/CHANGELOG.md +120 -0
  15. package/qtty-js/qtty/Cargo.lock +1462 -0
  16. package/qtty-js/qtty/Cargo.toml +12 -0
  17. package/qtty-js/qtty/LICENSE +661 -0
  18. package/qtty-js/qtty/README.md +9 -0
  19. package/qtty-js/qtty/qtty/Cargo.toml +41 -0
  20. package/qtty-js/qtty/qtty/README.md +8 -0
  21. package/qtty-js/qtty/qtty/examples/angles.rs +14 -0
  22. package/qtty-js/qtty/qtty/examples/astronomy.rs +17 -0
  23. package/qtty-js/qtty/qtty/examples/dimensional_arithmetic.rs +83 -0
  24. package/qtty-js/qtty/qtty/examples/python_integration.rs +61 -0
  25. package/qtty-js/qtty/qtty/examples/quickstart.rs +15 -0
  26. package/qtty-js/qtty/qtty/examples/ratios.rs +12 -0
  27. package/qtty-js/qtty/qtty/examples/serde_with_unit.rs +234 -0
  28. package/qtty-js/qtty/qtty/examples/serialization.rs +141 -0
  29. package/qtty-js/qtty/qtty/examples/serialization_advanced.rs +155 -0
  30. package/qtty-js/qtty/qtty/src/f32.rs +108 -0
  31. package/qtty-js/qtty/qtty/src/f64.rs +30 -0
  32. package/qtty-js/qtty/qtty/src/i128.rs +111 -0
  33. package/qtty-js/qtty/qtty/src/i16.rs +111 -0
  34. package/qtty-js/qtty/qtty/src/i32.rs +111 -0
  35. package/qtty-js/qtty/qtty/src/i64.rs +111 -0
  36. package/qtty-js/qtty/qtty/src/i8.rs +111 -0
  37. package/qtty-js/qtty/qtty/src/lib.rs +238 -0
  38. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.lock +83 -0
  39. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/Cargo.toml +10 -0
  40. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std/src/lib.rs +7 -0
  41. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.lock +83 -0
  42. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/Cargo.toml +10 -0
  43. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-no-std-alloc/src/lib.rs +7 -0
  44. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.lock +83 -0
  45. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/Cargo.toml +10 -0
  46. package/qtty-js/qtty/qtty/tests/fixtures/qtty-vec-std/src/lib.rs +5 -0
  47. package/qtty-js/qtty/qtty/tests/integration_tests.rs +529 -0
  48. package/qtty-js/qtty/qtty/tests/qtty_vec_feature_matrix.rs +58 -0
  49. package/qtty-js/qtty/qtty-core/Cargo.toml +41 -0
  50. package/qtty-js/qtty/qtty-core/README.md +8 -0
  51. package/qtty-js/qtty/qtty-core/examples/diesel_integration.rs +145 -0
  52. package/qtty-js/qtty/qtty-core/examples/quantity_db_serde.rs +215 -0
  53. package/qtty-js/qtty/qtty-core/src/dimension.rs +249 -0
  54. package/qtty-js/qtty/qtty-core/src/feature_diesel.rs +318 -0
  55. package/qtty-js/qtty/qtty-core/src/feature_pyo3.rs +27 -0
  56. package/qtty-js/qtty/qtty-core/src/feature_serde.rs +203 -0
  57. package/qtty-js/qtty/qtty-core/src/feature_tiberius.rs +28 -0
  58. package/qtty-js/qtty/qtty-core/src/lib.rs +744 -0
  59. package/qtty-js/qtty/qtty-core/src/macros.rs +93 -0
  60. package/qtty-js/qtty/qtty-core/src/quantity.rs +810 -0
  61. package/qtty-js/qtty/qtty-core/src/scalar.rs +1742 -0
  62. package/qtty-js/qtty/qtty-core/src/unit.rs +332 -0
  63. package/qtty-js/qtty/qtty-core/src/units/angular.rs +1228 -0
  64. package/qtty-js/qtty/qtty-core/src/units/area.rs +243 -0
  65. package/qtty-js/qtty/qtty-core/src/units/frequency.rs +179 -0
  66. package/qtty-js/qtty/qtty-core/src/units/length.rs +1270 -0
  67. package/qtty-js/qtty/qtty-core/src/units/mass.rs +488 -0
  68. package/qtty-js/qtty/qtty-core/src/units/mod.rs +26 -0
  69. package/qtty-js/qtty/qtty-core/src/units/power.rs +324 -0
  70. package/qtty-js/qtty/qtty-core/src/units/time.rs +667 -0
  71. package/qtty-js/qtty/qtty-core/src/units/unitless.rs +212 -0
  72. package/qtty-js/qtty/qtty-core/src/units/velocity.rs +210 -0
  73. package/qtty-js/qtty/qtty-core/src/units/volume.rs +269 -0
  74. package/qtty-js/qtty/qtty-core/tests/core.rs +628 -0
  75. package/qtty-js/qtty/qtty-core/tests/diesel.rs +461 -0
  76. package/qtty-js/qtty/qtty-core/tests/integers.rs +632 -0
  77. package/qtty-js/qtty/qtty-core/tests/no_cross_unit_ops.rs +35 -0
  78. package/qtty-js/qtty/qtty-core/tests/pyo3.rs +334 -0
  79. package/qtty-js/qtty/qtty-core/tests/quantity_f32.rs +276 -0
  80. package/qtty-js/qtty/qtty-core/tests/scalar_decimal.rs +258 -0
  81. package/qtty-js/qtty/qtty-core/tests/scalar_f32.rs +286 -0
  82. package/qtty-js/qtty/qtty-core/tests/scalar_f64_real.rs +287 -0
  83. package/qtty-js/qtty/qtty-core/tests/scalar_rational.rs +260 -0
  84. package/qtty-js/qtty/qtty-core/tests/serde.rs +256 -0
  85. package/qtty-js/qtty/qtty-core/tests/tiberius.rs +208 -0
  86. package/qtty-js/qtty/qtty-derive/Cargo.toml +23 -0
  87. package/qtty-js/qtty/qtty-derive/README.md +8 -0
  88. package/qtty-js/qtty/qtty-derive/src/lib.rs +340 -0
  89. package/qtty-js/qtty/qtty-ffi/ARCHITECTURE.md +3 -0
  90. package/qtty-js/qtty/qtty-ffi/Cargo.toml +31 -0
  91. package/qtty-js/qtty/qtty-ffi/README.md +9 -0
  92. package/qtty-js/qtty/qtty-ffi/build.rs +326 -0
  93. package/qtty-js/qtty/qtty-ffi/cbindgen.toml +105 -0
  94. package/qtty-js/qtty/qtty-ffi/include/qtty_ffi.h +1126 -0
  95. package/qtty-js/qtty/qtty-ffi/src/ffi.rs +1251 -0
  96. package/qtty-js/qtty/qtty-ffi/src/ffi_serde.rs +294 -0
  97. package/qtty-js/qtty/qtty-ffi/src/helpers.rs +310 -0
  98. package/qtty-js/qtty/qtty-ffi/src/lib.rs +229 -0
  99. package/qtty-js/qtty/qtty-ffi/src/macros.rs +121 -0
  100. package/qtty-js/qtty/qtty-ffi/src/registry.rs +274 -0
  101. package/qtty-js/qtty/qtty-ffi/src/types.rs +620 -0
  102. package/qtty-js/qtty/qtty-ffi/tests/integration_tests.rs +842 -0
  103. package/qtty-js/qtty/qtty-ffi/units.csv +156 -0
  104. package/qtty-js/qtty/qtty-ffi/units.csv.md +3 -0
  105. package/qtty-js/qtty-node/.prettierignore +6 -0
  106. package/qtty-js/qtty-node/.prettierrc.json +6 -0
  107. package/qtty-js/qtty-node/README.md +250 -0
  108. package/qtty-js/qtty-node/c8.config.json +11 -0
  109. package/qtty-js/qtty-node/eslint.config.js +31 -0
  110. package/qtty-js/qtty-node/examples/arithmetic.mjs +64 -0
  111. package/qtty-js/qtty-node/examples/astronomy.mjs +90 -0
  112. package/qtty-js/qtty-node/examples/quickstart.mjs +36 -0
  113. package/qtty-js/qtty-node/examples/serialization.mjs +125 -0
  114. package/qtty-js/qtty-node/examples/unit_factories.mjs +74 -0
  115. package/qtty-js/qtty-node/index.d.ts +219 -0
  116. package/qtty-js/qtty-node/index.js +323 -0
  117. package/qtty-js/qtty-node/lib/DerivedQuantity.js +122 -0
  118. package/qtty-js/qtty-node/lib/Quantity.js +151 -0
  119. package/qtty-js/qtty-node/lib/backend.js +25 -0
  120. package/qtty-js/qtty-node/native.cjs +306 -0
  121. package/qtty-js/qtty-node/package-lock.json +3223 -0
  122. package/qtty-js/qtty-node/package.json +70 -0
  123. package/qtty-js/qtty-node/units.d.ts +299 -0
  124. package/qtty-js/qtty-node/units.js +210 -0
  125. package/qtty-js/qtty-web/Cargo.lock +767 -0
  126. package/qtty-js/qtty-web/Cargo.toml +21 -0
  127. package/qtty-js/qtty-web/index.d.ts +140 -0
  128. package/qtty-js/qtty-web/index.js +20 -0
  129. package/qtty-js/qtty-web/lib/DerivedQuantity.js +58 -0
  130. package/qtty-js/qtty-web/lib/Quantity.js +75 -0
  131. package/qtty-js/qtty-web/lib/backend.js +80 -0
  132. package/qtty-js/qtty-web/package.json +45 -0
  133. package/qtty-js/qtty-web/src/lib.rs +111 -0
  134. package/qtty-js/scripts/ci.sh +73 -0
  135. package/scripts/ci.sh +123 -0
  136. package/siderust-core/Cargo.lock +787 -0
  137. package/siderust-core/Cargo.toml +18 -0
  138. package/siderust-core/DEDUPLICATION.md +124 -0
  139. package/siderust-core/src/body.rs +120 -0
  140. package/siderust-core/src/events.rs +184 -0
  141. package/siderust-core/src/lib.rs +20 -0
  142. package/siderust-core/src/observer.rs +55 -0
  143. package/siderust-core/src/position.rs +213 -0
  144. package/siderust-node/.prettierignore +7 -0
  145. package/siderust-node/.prettierrc.json +6 -0
  146. package/siderust-node/Cargo.lock +906 -0
  147. package/siderust-node/Cargo.toml +29 -0
  148. package/siderust-node/README.md +109 -0
  149. package/siderust-node/__test__/index.test.mjs +248 -0
  150. package/siderust-node/build.rs +5 -0
  151. package/siderust-node/c8.config.json +3 -0
  152. package/siderust-node/eslint.config.js +31 -0
  153. package/siderust-node/examples/01_basic_coordinates.mjs +24 -0
  154. package/siderust-node/examples/02_coordinate_transformations.mjs +25 -0
  155. package/siderust-node/examples/03_all_frames_conversions.mjs +26 -0
  156. package/siderust-node/examples/04_all_center_conversions.mjs +24 -0
  157. package/siderust-node/examples/05_target_tracking.mjs +22 -0
  158. package/siderust-node/examples/06_night_events.mjs +18 -0
  159. package/siderust-node/examples/07_moon_properties.mjs +21 -0
  160. package/siderust-node/examples/08_solar_system.mjs +19 -0
  161. package/siderust-node/examples/09_star_observability.mjs +22 -0
  162. package/siderust-node/examples/10_time_periods.mjs +9 -0
  163. package/siderust-node/examples/11_serialization.mjs +31 -0
  164. package/siderust-node/examples/12_runtime_ephemeris.mjs +27 -0
  165. package/siderust-node/examples/13_coordinate_operations.mjs +20 -0
  166. package/siderust-node/index.d.ts +623 -0
  167. package/siderust-node/index.js +79 -0
  168. package/siderust-node/lib/Observer.js +112 -0
  169. package/siderust-node/lib/Star.js +118 -0
  170. package/siderust-node/lib/backend.js +63 -0
  171. package/siderust-node/lib/wrappers.js +566 -0
  172. package/siderust-node/main.js +20 -0
  173. package/siderust-node/native.cjs +360 -0
  174. package/siderust-node/package-lock.json +3261 -0
  175. package/siderust-node/package.json +71 -0
  176. package/siderust-node/src/body.rs +74 -0
  177. package/siderust-node/src/coordinates.rs +372 -0
  178. package/siderust-node/src/ephemeris.rs +462 -0
  179. package/siderust-node/src/events.rs +577 -0
  180. package/siderust-node/src/lib.rs +43 -0
  181. package/siderust-node/src/observer.rs +132 -0
  182. package/siderust-node/src/phase.rs +218 -0
  183. package/siderust-node/src/position.rs +292 -0
  184. package/siderust-node/src/star.rs +200 -0
  185. package/siderust-web/Cargo.lock +855 -0
  186. package/siderust-web/Cargo.toml +34 -0
  187. package/siderust-web/README.md +100 -0
  188. package/siderust-web/__test__/index.test.mjs +118 -0
  189. package/siderust-web/examples/github-pages/README.md +31 -0
  190. package/siderust-web/examples/github-pages/index.html +135 -0
  191. package/siderust-web/index.d.ts +311 -0
  192. package/siderust-web/index.js +66 -0
  193. package/siderust-web/lib/Observer.js +103 -0
  194. package/siderust-web/lib/Star.js +116 -0
  195. package/siderust-web/lib/backend.js +400 -0
  196. package/siderust-web/lib/wrappers.js +512 -0
  197. package/siderust-web/package.json +55 -0
  198. package/siderust-web/src/body.rs +69 -0
  199. package/siderust-web/src/coordinates.rs +302 -0
  200. package/siderust-web/src/ephemeris.rs +456 -0
  201. package/siderust-web/src/events.rs +520 -0
  202. package/siderust-web/src/lib.rs +51 -0
  203. package/siderust-web/src/observer.rs +117 -0
  204. package/siderust-web/src/phase.rs +190 -0
  205. package/siderust-web/src/position.rs +291 -0
  206. package/siderust-web/src/star.rs +178 -0
  207. package/tempoch-js/.github/workflows/ci.yml +142 -0
  208. package/tempoch-js/.gitmodules +3 -0
  209. package/tempoch-js/CHANGELOG.md +25 -0
  210. package/tempoch-js/LICENSE +661 -0
  211. package/tempoch-js/README.md +126 -0
  212. package/tempoch-js/package.json +20 -0
  213. package/tempoch-js/scripts/ci.sh +73 -0
  214. package/tempoch-js/tempoch/.github/workflows/ci.yml +113 -0
  215. package/tempoch-js/tempoch/CHANGELOG.md +82 -0
  216. package/tempoch-js/tempoch/Cargo.lock +947 -0
  217. package/tempoch-js/tempoch/Cargo.toml +3 -0
  218. package/tempoch-js/tempoch/LICENSE +661 -0
  219. package/tempoch-js/tempoch/README.md +76 -0
  220. package/tempoch-js/tempoch/tempoch/Cargo.toml +27 -0
  221. package/tempoch-js/tempoch/tempoch/examples/periods.rs +45 -0
  222. package/tempoch-js/tempoch/tempoch/examples/quickstart.rs +13 -0
  223. package/tempoch-js/tempoch/tempoch/src/lib.rs +49 -0
  224. package/tempoch-js/tempoch/tempoch/tests/integration.rs +57 -0
  225. package/tempoch-js/tempoch/tempoch-core/Cargo.toml +24 -0
  226. package/tempoch-js/tempoch/tempoch-core/src/delta_t.rs +345 -0
  227. package/tempoch-js/tempoch/tempoch-core/src/instant.rs +811 -0
  228. package/tempoch-js/tempoch/tempoch-core/src/julian_date_ext.rs +142 -0
  229. package/tempoch-js/tempoch/tempoch-core/src/lib.rs +81 -0
  230. package/tempoch-js/tempoch/tempoch-core/src/period.rs +1168 -0
  231. package/tempoch-js/tempoch/tempoch-core/src/scales.rs +779 -0
  232. package/tempoch-js/tempoch/tempoch-ffi/Cargo.lock +889 -0
  233. package/tempoch-js/tempoch/tempoch-ffi/Cargo.toml +26 -0
  234. package/tempoch-js/tempoch/tempoch-ffi/build.rs +24 -0
  235. package/tempoch-js/tempoch/tempoch-ffi/cbindgen.toml +30 -0
  236. package/tempoch-js/tempoch/tempoch-ffi/src/error.rs +19 -0
  237. package/tempoch-js/tempoch/tempoch-ffi/src/lib.rs +82 -0
  238. package/tempoch-js/tempoch/tempoch-ffi/src/period.rs +101 -0
  239. package/tempoch-js/tempoch/tempoch-ffi/src/time.rs +711 -0
  240. package/tempoch-js/tempoch/tempoch-ffi/tests/ffi.rs +265 -0
  241. package/tempoch-js/tempoch-node/.prettierignore +6 -0
  242. package/tempoch-js/tempoch-node/.prettierrc.json +6 -0
  243. package/tempoch-js/tempoch-node/Cargo.lock +496 -0
  244. package/tempoch-js/tempoch-node/Cargo.toml +29 -0
  245. package/tempoch-js/tempoch-node/README.md +265 -0
  246. package/tempoch-js/tempoch-node/__test__/index.test.mjs +598 -0
  247. package/tempoch-js/tempoch-node/build.rs +5 -0
  248. package/tempoch-js/tempoch-node/c8.config.json +3 -0
  249. package/tempoch-js/tempoch-node/eslint.config.js +31 -0
  250. package/tempoch-js/tempoch-node/examples/periods.mjs +79 -0
  251. package/tempoch-js/tempoch-node/examples/quickstart.mjs +71 -0
  252. package/tempoch-js/tempoch-node/examples/timescales.mjs +92 -0
  253. package/tempoch-js/tempoch-node/index.d.ts +280 -0
  254. package/tempoch-js/tempoch-node/index.js +32 -0
  255. package/tempoch-js/tempoch-node/lib/JulianDate.js +176 -0
  256. package/tempoch-js/tempoch-node/lib/ModifiedJulianDate.js +156 -0
  257. package/tempoch-js/tempoch-node/lib/Period.js +133 -0
  258. package/tempoch-js/tempoch-node/lib/backend.js +38 -0
  259. package/tempoch-js/tempoch-node/lib/qttyCompat.js +92 -0
  260. package/tempoch-js/tempoch-node/native.cjs +317 -0
  261. package/tempoch-js/tempoch-node/package-lock.json +3223 -0
  262. package/tempoch-js/tempoch-node/package.json +56 -0
  263. package/tempoch-js/tempoch-node/src/lib.rs +573 -0
  264. package/tempoch-js/tempoch-web/Cargo.toml +23 -0
  265. package/tempoch-js/tempoch-web/index.d.ts +95 -0
  266. package/tempoch-js/tempoch-web/index.js +27 -0
  267. package/tempoch-js/tempoch-web/lib/JulianDate.js +170 -0
  268. package/tempoch-js/tempoch-web/lib/ModifiedJulianDate.js +145 -0
  269. package/tempoch-js/tempoch-web/lib/Period.js +121 -0
  270. package/tempoch-js/tempoch-web/lib/backend.js +118 -0
  271. package/tempoch-js/tempoch-web/package.json +46 -0
  272. package/tempoch-js/tempoch-web/src/lib.rs +184 -0
@@ -0,0 +1,779 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ // Copyright (C) 2026 Vallés Puig, Ramon
3
+
4
+ //! Time-scale marker types.
5
+ //!
6
+ //! Each zero-sized type identifies a specific time scale and encodes how
7
+ //! values in that scale relate to the canonical **Julian Date in TT**
8
+ //! (Terrestrial Time).
9
+ //!
10
+ //! # Epoch counters
11
+ //!
12
+ //! | Marker | Description | Epoch (JD) |
13
+ //! |--------|-------------|------------|
14
+ //! | [`JD`] | Julian Date | 0.0 |
15
+ //! | [`JDE`] | Julian Ephemeris Day | 0.0 |
16
+ //! | [`MJD`] | Modified Julian Date | 2 400 000.5 |
17
+ //! | [`UnixTime`] | Seconds since 1970-01-01 | 2 440 587.5 |
18
+ //! | [`GPS`] | GPS Time (days) | 2 444 244.5 |
19
+ //!
20
+ //! # Physical / relativistic scales
21
+ //!
22
+ //! | Marker | Description |
23
+ //! |--------|-------------|
24
+ //! | [`TDB`] | Barycentric Dynamical Time (canonical) |
25
+ //! | [`TT`] | Terrestrial Time |
26
+ //! | [`TAI`] | International Atomic Time |
27
+ //! | [`TCG`] | Geocentric Coordinate Time (IAU 2000) |
28
+ //! | [`TCB`] | Barycentric Coordinate Time (IAU 2006) |
29
+
30
+ use super::instant::TimeScale;
31
+ use qtty::Days;
32
+
33
+ // ---------------------------------------------------------------------------
34
+ // Epoch counters
35
+ // ---------------------------------------------------------------------------
36
+
37
+ /// Julian Date — the identity scale.
38
+ ///
39
+ /// `to_jd_tt(v) = v`, i.e. the quantity *is* a Julian Day number.
40
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
41
+ pub struct JD;
42
+
43
+ impl TimeScale for JD {
44
+ const LABEL: &'static str = "Julian Day:";
45
+
46
+ #[inline(always)]
47
+ fn to_jd_tt(value: Days) -> Days {
48
+ value
49
+ }
50
+
51
+ #[inline(always)]
52
+ fn from_jd_tt(jd_tt: Days) -> Days {
53
+ jd_tt
54
+ }
55
+ }
56
+
57
+ /// Julian Ephemeris Day — dynamic Julian day used by ephemerides.
58
+ ///
59
+ /// Numerically this is an absolute Julian day on the TT axis in this crate.
60
+ /// It is a semantic label for ephemeris formulas, not a UT→TT conversion.
61
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
62
+ pub struct JDE;
63
+
64
+ impl TimeScale for JDE {
65
+ const LABEL: &'static str = "JDE";
66
+
67
+ #[inline(always)]
68
+ fn to_jd_tt(value: Days) -> Days {
69
+ value
70
+ }
71
+
72
+ #[inline(always)]
73
+ fn from_jd_tt(jd_tt: Days) -> Days {
74
+ jd_tt
75
+ }
76
+ }
77
+
78
+ /// Modified Julian Date — JD minus 2 400 000.5.
79
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
80
+ pub struct MJD;
81
+
82
+ /// The constant offset between JD and MJD: `JD = MJD + MJD_EPOCH`.
83
+ const MJD_EPOCH: Days = Days::new(2_400_000.5);
84
+
85
+ impl TimeScale for MJD {
86
+ const LABEL: &'static str = "MJD";
87
+
88
+ #[inline(always)]
89
+ fn to_jd_tt(value: Days) -> Days {
90
+ value + MJD_EPOCH
91
+ }
92
+
93
+ #[inline(always)]
94
+ fn from_jd_tt(jd_tt: Days) -> Days {
95
+ jd_tt - MJD_EPOCH
96
+ }
97
+ }
98
+
99
+ // ---------------------------------------------------------------------------
100
+ // Physical / relativistic scales
101
+ // ---------------------------------------------------------------------------
102
+
103
+ /// Barycentric Dynamical Time.
104
+ ///
105
+ /// TDB differs from TT by a periodic correction of ≈1.7 ms amplitude
106
+ /// (Fairhead & Bretagnon 1990). This implementation applies the four
107
+ /// largest periodic terms automatically in `to_jd_tt` / `from_jd_tt`,
108
+ /// achieving accuracy better than 30 μs for dates within ±10 000 years
109
+ /// of J2000.
110
+ ///
111
+ /// ## References
112
+ /// * Fairhead & Bretagnon (1990), A&A 229, 240
113
+ /// * USNO Circular 179, eq. 2.6
114
+ /// * IAU 2006 Resolution B3
115
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
116
+ pub struct TDB;
117
+
118
+ /// Compute TDB − TT in days using the Fairhead & Bretagnon (1990) 4-term
119
+ /// expression. Accuracy: better than 30 μs for |t| < 100 centuries.
120
+ #[inline]
121
+ pub(crate) fn tdb_minus_tt_days(jd_tt: Days) -> Days {
122
+ // Julian centuries from J2000.0 on the TT axis
123
+ let t = (jd_tt.value() - 2_451_545.0) / 36_525.0;
124
+
125
+ // Earth's mean anomaly (radians)
126
+ let m_e = (357.5291092 + 35999.0502909 * t).to_radians();
127
+ // Mean anomaly of Jupiter (radians)
128
+ let m_j = (246.4512 + 3035.2335 * t).to_radians();
129
+ // Mean elongation of the Moon from the Sun (radians)
130
+ let d = (297.8502042 + 445267.1115168 * t).to_radians();
131
+ // Mean longitude of lunar ascending node (radians)
132
+ let om = (125.0445550 - 1934.1362091 * t).to_radians();
133
+
134
+ // TDB − TT in seconds (Fairhead & Bretagnon largest terms):
135
+ let dt_sec = 0.001_657 * (m_e + 0.01671 * m_e.sin()).sin()
136
+ + 0.000_022 * (d - m_e).sin()
137
+ + 0.000_014 * (2.0 * d).sin()
138
+ + 0.000_005 * m_j.sin()
139
+ + 0.000_005 * om.sin();
140
+
141
+ Days::new(dt_sec / 86_400.0)
142
+ }
143
+
144
+ impl TimeScale for TDB {
145
+ const LABEL: &'static str = "TDB";
146
+
147
+ #[inline]
148
+ fn to_jd_tt(tdb_value: Days) -> Days {
149
+ // JD(TT) = JD(TDB) - (TDB - TT)
150
+ // First approximation: use tdb_value as TT to compute the correction.
151
+ // The correction is < 2 ms so one iteration is sufficient for f64.
152
+ tdb_value - tdb_minus_tt_days(tdb_value)
153
+ }
154
+
155
+ #[inline]
156
+ fn from_jd_tt(jd_tt: Days) -> Days {
157
+ // JD(TDB) = JD(TT) + (TDB - TT)
158
+ jd_tt + tdb_minus_tt_days(jd_tt)
159
+ }
160
+ }
161
+
162
+ /// Terrestrial Time — the basis for astronomical ephemerides.
163
+ ///
164
+ /// Numerically identical to JD when the Julian Day number already represents
165
+ /// TT (which is the convention used throughout this crate).
166
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
167
+ pub struct TT;
168
+
169
+ impl TimeScale for TT {
170
+ const LABEL: &'static str = "TT";
171
+
172
+ #[inline(always)]
173
+ fn to_jd_tt(value: Days) -> Days {
174
+ value // value is already JD(TT)
175
+ }
176
+
177
+ #[inline(always)]
178
+ fn from_jd_tt(jd_tt: Days) -> Days {
179
+ jd_tt
180
+ }
181
+ }
182
+
183
+ /// International Atomic Time.
184
+ ///
185
+ /// `TT = TAI + 32.184 s`.
186
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
187
+ pub struct TAI;
188
+
189
+ /// `TT = TAI + 32.184 s` expressed in days.
190
+ const TT_MINUS_TAI: Days = Days::new(32.184 / 86_400.0);
191
+
192
+ impl TimeScale for TAI {
193
+ const LABEL: &'static str = "TAI";
194
+
195
+ #[inline(always)]
196
+ fn to_jd_tt(value: Days) -> Days {
197
+ // TAI → TT: add 32.184 s
198
+ value + TT_MINUS_TAI
199
+ }
200
+
201
+ #[inline(always)]
202
+ fn from_jd_tt(jd_tt: Days) -> Days {
203
+ // TT → TAI: subtract 32.184 s
204
+ jd_tt - TT_MINUS_TAI
205
+ }
206
+ }
207
+
208
+ // ---------------------------------------------------------------------------
209
+ // Coordinate time scales (IAU 2000 / 2006)
210
+ // ---------------------------------------------------------------------------
211
+
212
+ /// Geocentric Coordinate Time — the coordinate time for the GCRS.
213
+ ///
214
+ /// TCG is the proper time experienced by a clock at the geocenter, free from
215
+ /// the gravitational time dilation of the Earth's potential.
216
+ ///
217
+ /// The defining relation (IAU 2000 Resolution B1.9) is:
218
+ ///
219
+ /// ```text
220
+ /// dTT / dTCG = 1 − L_G
221
+ /// ```
222
+ ///
223
+ /// where **L_G = 6.969 290 134 × 10⁻¹⁰** is an IAU defining constant.
224
+ /// Integrating:
225
+ ///
226
+ /// ```text
227
+ /// TT = TCG − L_G × (JD_TCG − T₀)
228
+ /// ```
229
+ ///
230
+ /// with **T₀ = 2 443 144.500 372 5** (JD of 1977 January 1.0 TAI in the TCG scale).
231
+ ///
232
+ /// ## References
233
+ /// * IAU 2000 Resolution B1.9
234
+ /// * IERS Conventions (2010), §1.2
235
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
236
+ pub struct TCG;
237
+
238
+ /// IAU defining constant L_G (IAU 2000 Resolution B1.9).
239
+ ///
240
+ /// Defines the rate difference between TT and TCG:
241
+ /// `dTT/dTCG = 1 − L_G`.
242
+ const L_G: f64 = 6.969_290_134e-10;
243
+
244
+ /// TCG epoch T₀: JD(TCG) of 1977 January 1.0 TAI.
245
+ const TCG_EPOCH_T0: f64 = 2_443_144.500_372_5;
246
+
247
+ impl TimeScale for TCG {
248
+ const LABEL: &'static str = "TCG";
249
+
250
+ #[inline]
251
+ fn to_jd_tt(tcg_value: Days) -> Days {
252
+ // TT = TCG − L_G × (JD_TCG − T₀)
253
+ let jd_tcg = tcg_value.value();
254
+ Days::new(jd_tcg - L_G * (jd_tcg - TCG_EPOCH_T0))
255
+ }
256
+
257
+ #[inline]
258
+ fn from_jd_tt(jd_tt: Days) -> Days {
259
+ // JD_TCG = (JD_TT + L_G × T₀) / (1 − L_G)
260
+ // ≈ JD_TT + L_G × (JD_TT − T₀) (first-order, adequate for f64)
261
+ let tt = jd_tt.value();
262
+ Days::new(tt + L_G * (tt - TCG_EPOCH_T0) / (1.0 - L_G))
263
+ }
264
+ }
265
+
266
+ /// Barycentric Coordinate Time — the coordinate time for the BCRS.
267
+ ///
268
+ /// TCB is the time coordinate complementary to barycentric spatial coordinates.
269
+ /// It relates to TDB via a linear drift:
270
+ ///
271
+ /// ```text
272
+ /// TDB = TCB − L_B × (JD_TCB − T₀) + TDB₀
273
+ /// ```
274
+ ///
275
+ /// where:
276
+ /// * **L_B = 1.550 519 768 × 10⁻⁸** (IAU 2006 Resolution B3, defining constant)
277
+ /// * **TDB₀ = −6.55 × 10⁻⁵ s** (IAU 2006 Resolution B3)
278
+ /// * **T₀ = 2 443 144.500 372 5** (JD of 1977 January 1.0 TAI)
279
+ ///
280
+ /// Since TDB ≈ TT for route-through-JD(TT) purposes (the ≈1.7 ms periodic
281
+ /// difference is handled separately), we use the TDB relation directly.
282
+ ///
283
+ /// ## References
284
+ /// * IAU 2006 Resolution B3
285
+ /// * IERS Conventions (2010), §1.2
286
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
287
+ pub struct TCB;
288
+
289
+ /// IAU defining constant L_B (IAU 2006 Resolution B3).
290
+ ///
291
+ /// Defines the rate difference between TDB and TCB:
292
+ /// `TDB = TCB − L_B × (JD_TCB − T₀) + TDB₀`.
293
+ const L_B: f64 = 1.550_519_768e-8;
294
+
295
+ impl TimeScale for TCB {
296
+ const LABEL: &'static str = "TCB";
297
+
298
+ #[inline]
299
+ fn to_jd_tt(tcb_value: Days) -> Days {
300
+ // TDB = TCB − L_B × (JD_TCB − T₀)
301
+ // Treating TDB ≈ TT (periodic ≈1.7 ms difference handled separately).
302
+ // Matches SOFA iauTcbtdb.
303
+ let jd_tcb = tcb_value.value();
304
+ Days::new(jd_tcb - L_B * (jd_tcb - TCG_EPOCH_T0))
305
+ }
306
+
307
+ #[inline]
308
+ fn from_jd_tt(jd_tt: Days) -> Days {
309
+ // JD_TCB = JD_TDB + L_B × (JD_TDB − T₀)
310
+ // Matches SOFA iauTdbtcb.
311
+ let tt = jd_tt.value();
312
+ Days::new(tt + L_B * (tt - TCG_EPOCH_T0))
313
+ }
314
+ }
315
+
316
+ // ---------------------------------------------------------------------------
317
+ // Navigation counters
318
+ // ---------------------------------------------------------------------------
319
+
320
+ /// GPS Time — continuous day count since 1980-01-06T00:00:00 UTC.
321
+ ///
322
+ /// GPS time has a fixed offset from TAI: `GPS = TAI − 19 s`.
323
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
324
+ pub struct GPS;
325
+
326
+ /// JD(TT) of the GPS epoch (1980-01-06T00:00:00 UTC).
327
+ /// GPS = TAI − 19s, and TT = TAI + 32.184s, so TT = GPS + 51.184s.
328
+ /// GPS epoch in JD(TT): JD 2444244.5 + 51.184/86400.
329
+ const GPS_EPOCH_JD_TT: Days = Days::new(2_444_244.5 + 51.184 / 86_400.0);
330
+
331
+ impl TimeScale for GPS {
332
+ const LABEL: &'static str = "GPS";
333
+
334
+ #[inline(always)]
335
+ fn to_jd_tt(value: Days) -> Days {
336
+ value + GPS_EPOCH_JD_TT
337
+ }
338
+
339
+ #[inline(always)]
340
+ fn from_jd_tt(jd_tt: Days) -> Days {
341
+ jd_tt - GPS_EPOCH_JD_TT
342
+ }
343
+ }
344
+
345
+ /// Unix Time — seconds since 1970-01-01T00:00:00 UTC, stored as **days**.
346
+ ///
347
+ /// This scale applies the cumulative leap-second offset from IERS Bulletin C
348
+ /// to convert between UTC-epoch Unix timestamps and the uniform TT axis.
349
+ /// The leap-second table covers 1972–2017 (28 insertions). Prior to 1972
350
+ /// (before the leap-second system) the offset is fixed at 10 s (the initial
351
+ /// TAI − UTC value adopted on 1972-01-01).
352
+ ///
353
+ /// ## References
354
+ /// * IERS Bulletin C (leap second announcements)
355
+ /// * POSIX.1-2017 §4.16 (definition of Unix time)
356
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
357
+ pub struct UnixTime;
358
+
359
+ /// JD of the Unix epoch (1970-01-01T00:00:00Z).
360
+ const UNIX_EPOCH_JD: Days = Days::new(2_440_587.5);
361
+
362
+ /// Leap-second table: (JD of leap-second insertion, cumulative TAI−UTC after).
363
+ /// Source: IERS Bulletin C. Entries are the JD of 00:00:00 UTC on the day
364
+ /// the leap second takes effect (i.e. the second is inserted at the end
365
+ /// of the previous day).
366
+ ///
367
+ /// TAI = UTC + leap_seconds (for times at or after each entry).
368
+ /// TT = TAI + 32.184 s.
369
+ const LEAP_SECONDS: [(f64, f64); 28] = [
370
+ (2_441_317.5, 10.0), // 1972-01-01
371
+ (2_441_499.5, 11.0), // 1972-07-01
372
+ (2_441_683.5, 12.0), // 1973-01-01
373
+ (2_442_048.5, 13.0), // 1974-01-01
374
+ (2_442_413.5, 14.0), // 1975-01-01
375
+ (2_442_778.5, 15.0), // 1976-01-01
376
+ (2_443_144.5, 16.0), // 1977-01-01
377
+ (2_443_509.5, 17.0), // 1978-01-01
378
+ (2_443_874.5, 18.0), // 1979-01-01
379
+ (2_444_239.5, 19.0), // 1980-01-01
380
+ (2_444_786.5, 20.0), // 1981-07-01
381
+ (2_445_151.5, 21.0), // 1982-07-01
382
+ (2_445_516.5, 22.0), // 1983-07-01
383
+ (2_446_247.5, 23.0), // 1985-07-01
384
+ (2_447_161.5, 24.0), // 1988-01-01
385
+ (2_447_892.5, 25.0), // 1990-01-01
386
+ (2_448_257.5, 26.0), // 1991-01-01
387
+ (2_448_804.5, 27.0), // 1992-07-01
388
+ (2_449_169.5, 28.0), // 1993-07-01
389
+ (2_449_534.5, 29.0), // 1994-07-01
390
+ (2_450_083.5, 30.0), // 1996-01-01
391
+ (2_450_630.5, 31.0), // 1997-07-01
392
+ (2_451_179.5, 32.0), // 1999-01-01
393
+ (2_453_736.5, 33.0), // 2006-01-01
394
+ (2_454_832.5, 34.0), // 2009-01-01
395
+ (2_456_109.5, 35.0), // 2012-07-01
396
+ (2_457_204.5, 36.0), // 2015-07-01
397
+ (2_457_754.5, 37.0), // 2017-01-01
398
+ ];
399
+
400
+ /// Look up cumulative TAI−UTC (leap seconds) for a JD on the UTC axis.
401
+ ///
402
+ /// Returns the number of leap seconds (TAI − UTC) in effect at the given
403
+ /// Julian Date on the UTC time axis. The table covers 1972–2017
404
+ /// (28 insertions). Before 1972 the conventional initial offset of 10 s
405
+ /// is returned.
406
+ ///
407
+ /// # Arguments
408
+ ///
409
+ /// * `jd_utc` - Julian Date number on the UTC axis.
410
+ #[inline]
411
+ pub fn tai_minus_utc(jd_utc: f64) -> f64 {
412
+ // Binary search for the last entry <= jd_utc
413
+ let mut lo = 0usize;
414
+ let mut hi = LEAP_SECONDS.len();
415
+ while lo < hi {
416
+ let mid = lo + (hi - lo) / 2;
417
+ if LEAP_SECONDS[mid].0 <= jd_utc {
418
+ lo = mid + 1;
419
+ } else {
420
+ hi = mid;
421
+ }
422
+ }
423
+ if lo == 0 {
424
+ // Before 1972-01-01: use the initial offset of 10 s
425
+ // (this is an approximation; pre-1972 UTC had fractional offsets)
426
+ 10.0
427
+ } else {
428
+ LEAP_SECONDS[lo - 1].1
429
+ }
430
+ }
431
+
432
+ /// TT = TAI + 32.184 s, and TAI = UTC + leap_seconds.
433
+ /// So TT = UTC + leap_seconds + 32.184 s.
434
+ const TT_MINUS_TAI_SECS: f64 = 32.184;
435
+
436
+ impl TimeScale for UnixTime {
437
+ const LABEL: &'static str = "Unix";
438
+
439
+ #[inline]
440
+ fn to_jd_tt(value: Days) -> Days {
441
+ // value is Unix days (days since 1970-01-01 on the UTC axis)
442
+ let jd_utc = value.value() + UNIX_EPOCH_JD.value();
443
+ let ls = tai_minus_utc(jd_utc);
444
+ // JD(TT) = JD(UTC) + (TAI−UTC + 32.184) / 86400
445
+ Days::new(jd_utc + (ls + TT_MINUS_TAI_SECS) / 86_400.0)
446
+ }
447
+
448
+ #[inline]
449
+ fn from_jd_tt(jd_tt: Days) -> Days {
450
+ // Approximate JD(UTC) by subtracting the largest plausible offset,
451
+ // then refine with the correct leap-second count.
452
+ let approx_utc = jd_tt.value() - (37.0 + TT_MINUS_TAI_SECS) / 86_400.0;
453
+ let ls = tai_minus_utc(approx_utc);
454
+ let jd_utc = jd_tt.value() - (ls + TT_MINUS_TAI_SECS) / 86_400.0;
455
+ Days::new(jd_utc - UNIX_EPOCH_JD.value())
456
+ }
457
+ }
458
+
459
+ // ---------------------------------------------------------------------------
460
+ // Universal Time (Earth-rotation based)
461
+ // ---------------------------------------------------------------------------
462
+
463
+ /// Universal Time — the civil time scale tied to Earth's rotation.
464
+ ///
465
+ /// Unlike [`JD`], [`JDE`], and [`TT`] (which all live on the uniform TT
466
+ /// axis), `UT` encodes a Julian Day on the **UT** axis. The conversion
467
+ /// to JD(TT) adds the epoch-dependent **ΔT** correction from Meeus (1998)
468
+ /// ch. 9, and the inverse uses a three-iteration fixed-point solver
469
+ /// with sub-microsecond accuracy.
470
+ ///
471
+ /// [`Time::from_utc`](super::instant::Time::from_utc) routes through this
472
+ /// scale automatically, so callers rarely need to construct `Time<UT>` directly.
473
+ #[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
474
+ pub struct UT;
475
+
476
+ impl TimeScale for UT {
477
+ const LABEL: &'static str = "UT";
478
+
479
+ #[inline]
480
+ fn to_jd_tt(ut_value: Days) -> Days {
481
+ let jd_ut = super::instant::Time::<JD>::from_days(ut_value);
482
+ let dt_secs = super::delta_t::delta_t_seconds_from_ut(jd_ut);
483
+ ut_value + dt_secs.to::<qtty::Day>()
484
+ }
485
+
486
+ #[inline]
487
+ fn from_jd_tt(jd_tt: Days) -> Days {
488
+ // Solve ut + ΔT(ut)/86400 = tt via fixed-point iteration.
489
+ // dΔT/dJD ≈ 3×10⁻⁸, so convergence is immediate.
490
+ let mut ut = jd_tt;
491
+ for _ in 0..3 {
492
+ let jd_ut = super::instant::Time::<JD>::from_days(ut);
493
+ let dt_days = super::delta_t::delta_t_seconds_from_ut(jd_ut).to::<qtty::Day>();
494
+ ut = jd_tt - dt_days;
495
+ }
496
+ ut
497
+ }
498
+ }
499
+
500
+ // ---------------------------------------------------------------------------
501
+ // Cross-scale From/Into and .to::<T>() (generated by macro)
502
+ // ---------------------------------------------------------------------------
503
+
504
+ /// Generate pairwise `From<Time<A>> for Time<B>` implementations.
505
+ macro_rules! impl_time_conversions {
506
+ // Base case: single scale, nothing left.
507
+ ($single:ty) => {};
508
+
509
+ // Recursive: generate pairs between $first and every $rest, then recurse.
510
+ ($first:ty, $($rest:ty),+ $(,)?) => {
511
+ $(
512
+ impl From<super::instant::Time<$first>> for super::instant::Time<$rest> {
513
+ #[inline]
514
+ fn from(t: super::instant::Time<$first>) -> Self {
515
+ t.to::<$rest>()
516
+ }
517
+ }
518
+
519
+ impl From<super::instant::Time<$rest>> for super::instant::Time<$first> {
520
+ #[inline]
521
+ fn from(t: super::instant::Time<$rest>) -> Self {
522
+ t.to::<$first>()
523
+ }
524
+ }
525
+ )+
526
+
527
+ impl_time_conversions!($($rest),+);
528
+ };
529
+ }
530
+
531
+ impl_time_conversions!(JD, JDE, MJD, TDB, TT, TAI, TCG, TCB, GPS, UnixTime, UT);
532
+
533
+ #[cfg(test)]
534
+ mod tests {
535
+ use super::super::instant::Time;
536
+ use super::*;
537
+ use qtty::{Day, Second, Seconds};
538
+
539
+ #[test]
540
+ fn jd_mjd_roundtrip() {
541
+ let jd = Time::<JD>::new(2_451_545.0);
542
+ let mjd: Time<MJD> = jd.to::<MJD>();
543
+ assert!((mjd.quantity() - Days::new(51_544.5)).abs() < Days::new(1e-10));
544
+ let back: Time<JD> = mjd.to::<JD>();
545
+ assert!((back.quantity() - Days::new(2_451_545.0)).abs() < Days::new(1e-10));
546
+ }
547
+
548
+ #[test]
549
+ fn jd_mjd_from_into() {
550
+ let jd = Time::<JD>::new(2_451_545.0);
551
+ let mjd: Time<MJD> = jd.into();
552
+ assert!((mjd.quantity() - Days::new(51_544.5)).abs() < Days::new(1e-10));
553
+ let back: Time<JD> = Time::from(mjd);
554
+ assert!((back.quantity() - Days::new(2_451_545.0)).abs() < Days::new(1e-10));
555
+ }
556
+
557
+ #[test]
558
+ fn tai_tt_offset() {
559
+ // TT = TAI + 32.184s
560
+ let tai = Time::<TAI>::new(2_451_545.0);
561
+ let tt: Time<TT> = tai.to::<TT>();
562
+ let expected_offset = Seconds::new(32.184).to::<Day>();
563
+ assert!((tt.quantity() - (tai.quantity() + expected_offset)).abs() < Days::new(1e-15));
564
+ }
565
+
566
+ #[test]
567
+ fn gps_epoch_roundtrip() {
568
+ // GPS epoch is JD 2444244.5 (in UTC); in TT it is shifted by 51.184s
569
+ let gps_zero = Time::<GPS>::new(0.0);
570
+ let jd: Time<JD> = gps_zero.to::<JD>();
571
+ let expected = Days::new(2_444_244.5) + Seconds::new(51.184).to::<Day>();
572
+ assert!((jd.quantity() - expected).abs() < Days::new(1e-12));
573
+ }
574
+
575
+ #[test]
576
+ fn unix_epoch_roundtrip() {
577
+ // Unix epoch (1970-01-01) has 10 s TAI−UTC and 32.184 s TT−TAI = 42.184 s TT−UTC
578
+ let unix_zero = Time::<UnixTime>::new(0.0);
579
+ let jd: Time<JD> = unix_zero.to::<JD>();
580
+ let expected = Days::new(2_440_587.5) + Seconds::new(42.184).to::<Day>();
581
+ assert!(
582
+ (jd.quantity() - expected).abs() < Days::new(1e-10),
583
+ "Unix epoch JD(TT) = {}, expected {}",
584
+ jd.quantity(),
585
+ expected
586
+ );
587
+ }
588
+
589
+ #[test]
590
+ fn unix_2020_leap_seconds() {
591
+ // 2020-01-01 00:00:00 UTC: TAI−UTC = 37 s, TT−UTC = 69.184 s
592
+ // JD(UTC) of 2020-01-01 = 2458849.5
593
+ // Unix days = 2458849.5 - 2440587.5 = 18262.0
594
+ let unix_2020 = Time::<UnixTime>::new(18262.0);
595
+ let jd: Time<JD> = unix_2020.to::<JD>();
596
+ let expected = Days::new(2_458_849.5) + Seconds::new(69.184).to::<Day>();
597
+ assert!(
598
+ (jd.quantity() - expected).abs() < Days::new(1e-10),
599
+ "2020 Unix JD(TT) = {}, expected {}",
600
+ jd.quantity(),
601
+ expected
602
+ );
603
+ }
604
+
605
+ #[test]
606
+ fn tdb_tt_offset() {
607
+ // TDB − TT periodic correction is ~1.7 ms at maximum.
608
+ // At J2000.0 t=0, the correction should be small but non-zero.
609
+ let jd = Time::<JD>::new(2_451_545.0);
610
+ let tdb: Time<TDB> = jd.to::<TDB>();
611
+ let offset_secs = (tdb.quantity() - jd.quantity()).to::<Second>();
612
+ // Should be within ±2 ms
613
+ assert!(
614
+ offset_secs.abs() < Seconds::new(0.002),
615
+ "TDB−TT offset at J2000 = {} s, expected < 2 ms",
616
+ offset_secs
617
+ );
618
+ }
619
+
620
+ #[test]
621
+ fn tdb_tt_roundtrip() {
622
+ let jd = Time::<JD>::new(2_451_545.0);
623
+ let tdb: Time<TDB> = jd.to::<TDB>();
624
+ let back: Time<JD> = tdb.to::<JD>();
625
+ assert!(
626
+ (back.quantity() - jd.quantity()).abs() < Days::new(1e-14),
627
+ "TDB→TT roundtrip error: {} days",
628
+ (back.quantity() - jd.quantity()).abs()
629
+ );
630
+ }
631
+
632
+ #[test]
633
+ fn tcg_tt_offset_at_j2000() {
634
+ // At J2000.0 (JD 2451545.0), TCG runs ahead of TT.
635
+ // TT = TCG − L_G × (JD_TCG − T₀)
636
+ // The offset at J2000 should be ~L_G × (2451545 − 2443144.5) × 86400 s
637
+ // ≈ 6.97e-10 × 8400.5 × 86400 ≈ 0.506 s
638
+ let tt = Time::<TT>::new(2_451_545.0);
639
+ let tcg: Time<TCG> = tt.to::<TCG>();
640
+ let offset_days = tcg.quantity() - tt.quantity();
641
+ let offset_secs = offset_days.to::<Second>();
642
+ // TCG should be ahead of TT by ~0.506 s at J2000
643
+ assert!(
644
+ (offset_secs - Seconds::new(0.506)).abs() < Seconds::new(0.01),
645
+ "TCG−TT offset at J2000 = {} s, expected ~0.506 s",
646
+ offset_secs
647
+ );
648
+ }
649
+
650
+ #[test]
651
+ fn tcg_tt_roundtrip() {
652
+ let tt = Time::<TT>::new(2_451_545.0);
653
+ let tcg: Time<TCG> = tt.to::<TCG>();
654
+ let back: Time<TT> = tcg.to::<TT>();
655
+ assert!(
656
+ (back.quantity() - tt.quantity()).abs() < Days::new(1e-12),
657
+ "TCG→TT roundtrip error: {} days",
658
+ (back.quantity() - tt.quantity()).abs()
659
+ );
660
+ }
661
+
662
+ #[test]
663
+ fn tcb_tdb_offset_at_j2000() {
664
+ // TCB runs significantly ahead of TDB.
665
+ // Offset ≈ L_B × (2451545 − 2443144.5) × 86400 s
666
+ // ≈ 1.55e-8 × 8400.5 × 86400 ≈ 11.25 s
667
+ let tt = Time::<TT>::new(2_451_545.0);
668
+ let tcb: Time<TCB> = tt.to::<TCB>();
669
+ let offset_days = tcb.quantity() - tt.quantity();
670
+ let offset_secs = offset_days.to::<Second>();
671
+ // TCB should be ahead of TT/TDB by ~11.25 s at J2000
672
+ assert!(
673
+ (offset_secs - Seconds::new(11.25)).abs() < Seconds::new(0.5),
674
+ "TCB−TT offset at J2000 = {} s, expected ~11.25 s",
675
+ offset_secs
676
+ );
677
+ }
678
+
679
+ #[test]
680
+ fn tcb_tt_roundtrip() {
681
+ let tt = Time::<TT>::new(2_458_000.0);
682
+ let tcb: Time<TCB> = tt.to::<TCB>();
683
+ let back: Time<TT> = tcb.to::<TT>();
684
+ assert!(
685
+ (back.quantity() - tt.quantity()).abs() < Days::new(1e-10),
686
+ "TCB→TT roundtrip error: {} days",
687
+ (back.quantity() - tt.quantity()).abs()
688
+ );
689
+ }
690
+
691
+ #[test]
692
+ fn ut_to_jd_applies_delta_t() {
693
+ let ut = Time::<UT>::new(2_451_545.0);
694
+ let jd: Time<JD> = ut.to::<JD>();
695
+ // ΔT at J2000 ≈ 63.83 s
696
+ let offset_secs = (jd.quantity() - ut.quantity()).to::<Second>();
697
+ assert!(
698
+ (offset_secs - Seconds::new(63.83)).abs() < Seconds::new(1.0),
699
+ "UT→JD offset = {} s, expected ~63.83 s",
700
+ offset_secs
701
+ );
702
+ }
703
+
704
+ #[test]
705
+ fn ut_jd_roundtrip() {
706
+ let jd = Time::<JD>::new(2_451_545.0);
707
+ let ut: Time<UT> = jd.to::<UT>();
708
+ let back: Time<JD> = ut.to::<JD>();
709
+ assert!(
710
+ (back.quantity() - jd.quantity()).abs() < Days::new(1e-12),
711
+ "roundtrip error: {} days",
712
+ (back.quantity() - jd.quantity()).abs()
713
+ );
714
+ }
715
+
716
+ #[test]
717
+ fn ut_from_into() {
718
+ let ut = Time::<UT>::new(2_451_545.0);
719
+ let jd: Time<JD> = ut.into();
720
+ let back: Time<UT> = jd.into();
721
+ assert!((back.quantity() - ut.quantity()).abs() < Days::new(1e-12));
722
+ }
723
+
724
+ // ── New coverage tests ────────────────────────────────────────────
725
+
726
+ #[test]
727
+ fn jde_roundtrip() {
728
+ // JDE is numerically identical to JD; test both conversion directions.
729
+ let jde = Time::<JDE>::new(2_451_545.0);
730
+ let jd: Time<JD> = jde.to::<JD>();
731
+ assert!((jd.quantity() - Days::new(2_451_545.0)).abs() < Days::new(1e-10));
732
+ let back: Time<JDE> = jd.to::<JDE>();
733
+ assert!((back.quantity() - jde.quantity()).abs() < Days::new(1e-10));
734
+ }
735
+
736
+ #[test]
737
+ fn tt_to_tai() {
738
+ // TT = TAI + 32.184 s ⟹ TAI = TT − 32.184 s.
739
+ // This exercises TAI::from_jd_tt.
740
+ let tt = Time::<TT>::new(2_451_545.0);
741
+ let tai: Time<TAI> = tt.to::<TAI>();
742
+ // Round-trip: TAI → TT should recover the original TT value.
743
+ let back: Time<TT> = tai.to::<TT>();
744
+ assert!(
745
+ (back.quantity() - tt.quantity()).abs() < Days::new(1e-15),
746
+ "TT → TAI → TT roundtrip error: {}",
747
+ (back.quantity() - tt.quantity()).abs()
748
+ );
749
+ }
750
+
751
+ #[test]
752
+ fn gps_from_jd() {
753
+ // Round-trip JD → GPS → JD; exercises GPS::from_jd_tt.
754
+ let gps_zero = Time::<GPS>::new(0.0);
755
+ let jd: Time<JD> = gps_zero.to::<JD>();
756
+ let back: Time<GPS> = jd.to::<GPS>();
757
+ assert!((back.quantity() - gps_zero.quantity()).abs() < Days::new(1e-12));
758
+ }
759
+
760
+ #[test]
761
+ fn unix_from_jd() {
762
+ // Round-trip UnixTime → JD → UnixTime; exercises UnixTime::from_jd_tt.
763
+ let unix_2020 = Time::<UnixTime>::new(18262.0); // 2020-01-01 UTC
764
+ let jd: Time<JD> = unix_2020.to::<JD>();
765
+ let back: Time<UnixTime> = jd.to::<UnixTime>();
766
+ assert!(
767
+ (back.quantity() - unix_2020.quantity()).abs() < Days::new(1e-10),
768
+ "UnixTime roundtrip error: {} days",
769
+ (back.quantity() - unix_2020.quantity()).abs()
770
+ );
771
+ }
772
+
773
+ #[test]
774
+ fn tai_minus_utc_pre_1972_returns_10() {
775
+ // Before 1972-01-01 (JD 2 441 317.5) the leap-second table has no
776
+ // applicable entry, so the conventional initial 10 s offset is returned.
777
+ assert_eq!(tai_minus_utc(2_400_000.0), 10.0);
778
+ }
779
+ }