pleiades-types 0.6.0

Shared typed vocabulary for the pleiades astrology workspace: angles, bodies, time scales, observers, coordinates, zodiac modes, house systems, and ayanamsas.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
use crate::*;
use core::time::Duration;

#[test]
fn instant_mean_obliquity_matches_the_shared_cubic_approximation() {
    let instant = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);

    assert_eq!(instant.mean_obliquity().degrees(), 23.439_291_111_111_11);
}

#[test]
fn instant_has_a_compact_display() {
    let instant = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);

    assert_eq!(instant.summary_line(), "JD 2451545 TDB");
    assert_eq!(instant.to_string(), "JD 2451545 TDB");
}

#[test]
fn time_scales_have_stable_display_names() {
    assert_eq!(TimeScale::Utc.to_string(), "UTC");
    assert_eq!(TimeScale::Ut1.to_string(), "UT1");
    assert_eq!(TimeScale::Tt.to_string(), "TT");
    assert_eq!(TimeScale::Tdb.to_string(), "TDB");
}

#[test]
fn time_scale_conversion_errors_use_stable_display_labels() {
    let error = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt)
        .tt_from_ut1(Duration::from_secs(1))
        .expect_err("TT is not UT1");

    assert_eq!(
        error.to_string(),
        "time-scale conversion expected UT1, got TT"
    );
}

#[test]
fn caller_supplied_time_scale_offsets_shift_julian_days() {
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);
    let tt = ut1
        .tt_from_ut1(Duration::from_secs_f64(64.184))
        .expect("UT1 to TT conversion should accept UT1 input");

    assert_eq!(tt.scale, TimeScale::Tt);
    assert!((tt.julian_day.days() - 2_451_545.000_742_870_4).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_utc_to_tt() {
    let utc = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc);
    let tt = utc
        .tt_from_utc(Duration::from_secs_f64(64.184))
        .expect("UTC to TT conversion should accept UTC input");

    assert_eq!(tt.scale, TimeScale::Tt);
    assert!((tt.julian_day.days() - 2_451_545.000_742_870_4).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_utc_to_tt_with_signed_offset() {
    let utc = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc);
    let tt = utc
        .tt_from_utc_signed(64.184)
        .expect("UTC to TT conversion should accept signed UTC input");

    assert_eq!(tt.scale, TimeScale::Tt);
    assert!((tt.julian_day.days() - 2_451_545.000_742_870_4).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_tt_to_tdb() {
    let tt = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let tdb = tt
        .tdb_from_tt(Duration::from_secs_f64(0.001_657))
        .expect("TT to TDB conversion should accept TT input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 + 0.001_657 / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_tt_to_tdb_with_signed_offset() {
    let tt = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let tdb = tt
        .tdb_from_tt_signed(-0.001_657)
        .expect("TT to TDB conversion should accept signed TT input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 - 0.001_657 / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_tdb_to_tt() {
    let tdb = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);
    let tt = tdb
        .tt_from_tdb(-0.001_657)
        .expect("TDB to TT conversion should accept TDB input");

    assert_eq!(tt.scale, TimeScale::Tt);
    let expected = 2_451_545.0 - 0.001_657 / 86_400.0;
    assert!((tt.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_tdb_to_tt_with_signed_offset() {
    let tdb = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);
    let tt = tdb
        .tt_from_tdb_signed(-0.001_657)
        .expect("TDB to TT conversion should accept signed TDB input");

    assert_eq!(tt.scale, TimeScale::Tt);
    let expected = 2_451_545.0 - 0.001_657 / 86_400.0;
    assert!((tt.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_utc_to_tdb() {
    let utc = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc);
    let tdb = utc
        .tdb_from_utc(
            Duration::from_secs_f64(64.184),
            Duration::from_secs_f64(0.001_657),
        )
        .expect("UTC to TDB conversion should accept UTC input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 + (64.184 + 0.001_657) / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_utc_to_tdb_with_signed_offset() {
    let utc = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc);
    let tdb = utc
        .tdb_from_utc_signed(Duration::from_secs_f64(64.184), -0.001_657)
        .expect("UTC to TDB conversion should accept signed UTC input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 + (64.184 - 0.001_657) / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_ut1_to_tdb() {
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);
    let tdb = ut1
        .tdb_from_ut1(
            Duration::from_secs_f64(64.184),
            Duration::from_secs_f64(0.001_657),
        )
        .expect("UT1 to TDB conversion should accept UT1 input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 + (64.184 + 0.001_657) / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_ut1_to_tdb_with_signed_offset() {
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);
    let tdb = ut1
        .tdb_from_ut1_signed(Duration::from_secs_f64(64.184), -0.001_657)
        .expect("UT1 to TDB conversion should accept signed UT1 input");

    assert_eq!(tdb.scale, TimeScale::Tdb);
    let expected = 2_451_545.0 + (64.184 - 0.001_657) / 86_400.0;
    assert!((tdb.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn caller_supplied_time_scale_offsets_can_convert_ut1_to_tt_with_signed_offset() {
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);
    let tt = ut1
        .tt_from_ut1_signed(64.184)
        .expect("UT1 to TT conversion should accept signed UT1 input");

    assert_eq!(tt.scale, TimeScale::Tt);
    let expected = 2_451_545.0 + 64.184 / 86_400.0;
    assert!((tt.julian_day.days() - expected).abs() < 1e-12);
}

#[test]
fn time_scale_helpers_reject_the_wrong_source_scale() {
    let utc = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Utc);
    let ut1_error = utc
        .tt_from_ut1(Duration::from_secs(64))
        .expect_err("UTC is not UT1");

    assert!(matches!(
        ut1_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Ut1,
            actual: TimeScale::Utc,
        }
    ));

    let tdb_ut1_error = utc
        .tdb_from_ut1(Duration::from_secs(64), Duration::from_secs(1))
        .expect_err("UTC is not UT1 for UT1-to-TDB conversion");

    assert!(matches!(
        tdb_ut1_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Ut1,
            actual: TimeScale::Utc,
        }
    ));

    let tt = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let utc_error = tt
        .tt_from_utc(Duration::from_secs(64))
        .expect_err("TT is not UTC");

    assert!(matches!(
        utc_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Utc,
            actual: TimeScale::Tt,
        }
    ));

    let utc_signed_error = tt
        .tt_from_utc_signed(64.0)
        .expect_err("TT is not UTC for signed UTC-to-TT conversion");

    assert!(matches!(
        utc_signed_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Utc,
            actual: TimeScale::Tt,
        }
    ));

    let tdb_error = tt
        .tdb_from_utc(Duration::from_secs(64), Duration::from_secs(1))
        .expect_err("TT is not UTC for UTC-to-TDB conversion");

    assert!(matches!(
        tdb_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Utc,
            actual: TimeScale::Tt,
        }
    ));

    let tt_error = utc
        .tt_from_tdb(-0.001_657)
        .expect_err("UTC is not TDB for TDB-to-TT conversion");

    assert!(matches!(
        tt_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Tdb,
            actual: TimeScale::Utc,
        }
    ));

    let ut1_signed_error = utc
        .tt_from_ut1_signed(64.0)
        .expect_err("UTC is not UT1 for signed UT1-to-TT conversion");

    assert!(matches!(
        ut1_signed_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Ut1,
            actual: TimeScale::Utc,
        }
    ));

    let wrong_scale_error = tt
        .tt_from_tdb(-0.001_657)
        .expect_err("TT is not TDB for TDB-to-TT conversion");

    assert!(matches!(
        wrong_scale_error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Tdb,
            actual: TimeScale::Tt,
        }
    ));
}

#[test]
fn signed_time_scale_helpers_reject_non_finite_offsets() {
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);
    let tt_nan_error = ut1
        .tt_from_ut1_signed(f64::NAN)
        .expect_err("non-finite UT1 offsets should be rejected");

    assert!(matches!(
        tt_nan_error,
        TimeScaleConversionError::NonFiniteOffset
    ));

    let tt = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let tdb_inf_error = tt
        .tdb_from_tt_signed(f64::INFINITY)
        .expect_err("non-finite TDB offsets should be rejected");

    assert!(matches!(
        tdb_inf_error,
        TimeScaleConversionError::NonFiniteOffset
    ));

    let tdb = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);
    let tt_negative_inf_error = tdb
        .tt_from_tdb(f64::NEG_INFINITY)
        .expect_err("non-finite TDB-to-TT offsets should be rejected");

    assert!(matches!(
        tt_negative_inf_error,
        TimeScaleConversionError::NonFiniteOffset
    ));
}

#[test]
fn time_scale_conversion_errors_render_stable_summary_lines() {
    let expected = TimeScaleConversionError::Expected {
        expected: TimeScale::Tt,
        actual: TimeScale::Utc,
    };
    assert_eq!(
        expected.summary_line(),
        "time-scale conversion expected TT, got UTC"
    );
    assert_eq!(expected.to_string(), expected.summary_line());

    let non_finite = TimeScaleConversionError::NonFiniteOffset;
    assert_eq!(
        non_finite.summary_line(),
        "time-scale conversion offset must be finite"
    );
    assert_eq!(non_finite.to_string(), non_finite.summary_line());
}

#[test]
fn time_scale_conversion_policy_can_validate_and_apply_a_caller_supplied_rule() {
    let policy = TimeScaleConversion::new(TimeScale::Ut1, TimeScale::Tt, 64.184);
    let ut1 = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Ut1);

    assert!(policy.validate(ut1).is_ok());
    assert!(ut1.validate_time_scale_conversion(policy).is_ok());

    let converted = policy
        .apply(ut1)
        .expect("caller-supplied policy should convert the source instant");

    assert_eq!(converted.scale, TimeScale::Tt);
    assert!((converted.julian_day.days() - 2_451_545.000_742_870_4).abs() < 1e-12);
    assert_eq!(
        policy.summary_line(),
        "source=UT1; target=TT; offset_seconds=64.184 s"
    );
    assert_eq!(policy.to_string(), policy.summary_line());
}

#[test]
fn time_scale_conversion_policy_renders_signed_offsets_in_summary_lines() {
    let policy = TimeScaleConversion::new(TimeScale::Tdb, TimeScale::Tt, -0.001_657);

    assert_eq!(
        policy.summary_line(),
        "source=TDB; target=TT; offset_seconds=-0.001657 s"
    );
    assert_eq!(policy.to_string(), policy.summary_line());
}

#[test]
fn time_scale_conversion_policy_validated_summary_line_matches_the_plain_rendering() {
    let policy = TimeScaleConversion::new(TimeScale::Tdb, TimeScale::Tt, -0.001_657);
    let instant = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);

    assert_eq!(
        policy
            .validated_summary_line(instant)
            .expect("policy should validate"),
        policy.summary_line()
    );
}

#[test]
fn time_scale_conversion_policy_accepts_signed_tdb_to_tt_validation() {
    let policy = TimeScaleConversion::new(TimeScale::Tdb, TimeScale::Tt, -0.001_657);
    let tdb = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tdb);

    assert!(policy.validate(tdb).is_ok());
    assert!(tdb.validate_time_scale_conversion(policy).is_ok());

    let converted = policy
        .apply(tdb)
        .expect("signed TDB-to-TT policy should convert the source instant");

    assert_eq!(converted.scale, TimeScale::Tt);
    assert!(
        (converted.julian_day.days() - 2_451_544.999_999_981).abs() < 1e-12,
        "signed TDB-to-TT conversion should apply the caller-supplied offset"
    );
}

#[test]
fn instant_validate_time_scale_conversion_rejects_mismatched_scales_and_non_finite_offsets() {
    let instant = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let policy = TimeScaleConversion::new(TimeScale::Ut1, TimeScale::Tt, 64.184);

    let error = instant
        .validate_time_scale_conversion(policy)
        .expect_err("policy should reject the wrong source scale");

    assert!(matches!(
        error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Ut1,
            actual: TimeScale::Tt,
        }
    ));

    let non_finite = TimeScaleConversion::new(TimeScale::Tt, TimeScale::Tdb, f64::NAN);
    let error = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt)
        .validate_time_scale_conversion(non_finite)
        .expect_err("policy should reject non-finite offsets");

    assert!(matches!(error, TimeScaleConversionError::NonFiniteOffset));
}

#[test]
fn time_scale_conversion_policy_rejects_mismatched_scales_and_non_finite_offsets() {
    let policy = TimeScaleConversion::new(TimeScale::Ut1, TimeScale::Tt, 64.184);
    let tt = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let error = policy
        .validate(tt)
        .expect_err("policy should reject the wrong source scale");

    assert!(matches!(
        error,
        TimeScaleConversionError::Expected {
            expected: TimeScale::Ut1,
            actual: TimeScale::Tt,
        }
    ));

    let non_finite = TimeScaleConversion::new(TimeScale::Tt, TimeScale::Tdb, f64::NAN);
    let error = non_finite
        .validate(Instant::new(
            JulianDay::from_days(2_451_545.0),
            TimeScale::Tt,
        ))
        .expect_err("policy should reject non-finite offsets");

    assert!(matches!(error, TimeScaleConversionError::NonFiniteOffset));
    assert!(matches!(
        non_finite.apply(Instant::new(
            JulianDay::from_days(2_451_545.0),
            TimeScale::Tt
        )),
        Err(TimeScaleConversionError::NonFiniteOffset)
    ));
}

#[test]
fn instant_with_time_scale_offset_checked_rejects_non_finite_offsets() {
    let instant = Instant::new(JulianDay::from_days(2_451_545.0), TimeScale::Tt);
    let error = instant
        .with_time_scale_offset_checked(TimeScale::Tdb, f64::NEG_INFINITY)
        .expect_err("checked offset conversion should reject non-finite offsets");

    assert!(matches!(error, TimeScaleConversionError::NonFiniteOffset));

    let converted = instant
        .with_time_scale_offset_checked(TimeScale::Tdb, 0.001_657)
        .expect("checked offset conversion should accept finite offsets");

    assert_eq!(converted.scale, TimeScale::Tdb);
    assert!((converted.julian_day.days() - 2_451_545.000_000_019).abs() < 1e-12);
}

#[test]
fn mean_obliquity_matches_published_cubic_off_epoch() {
    // t = (jd - 2451545.0) / 36525.0. These two epochs make jd - 2451545
    // exactly +/-36525.0, so t is exactly +/-1.0 (JD-grid representable).
    // Expected values come from the published IAU-1976 mean-obliquity cubic
    // evaluated outside the code (23d26m21.448s - 46.8150"t - 0.00059"t^2
    // + 0.001813"t^3), not from mean_obliquity itself.
    let plus_one = Instant::new(JulianDay::from_days(2_488_070.0), TimeScale::Tt);
    assert!(
        (plus_one.mean_obliquity().degrees() - 23.426_287_284_166_67).abs() < 1e-12,
        "t=+1 obliquity was {}",
        plus_one.mean_obliquity().degrees()
    );

    let minus_one = Instant::new(JulianDay::from_days(2_415_020.0), TimeScale::Tt);
    assert!(
        (minus_one.mean_obliquity().degrees() - 23.452_294_610_277_775).abs() < 1e-12,
        "t=-1 obliquity was {}",
        minus_one.mean_obliquity().degrees()
    );
}