summaryrefslogtreecommitdiffstats
path: root/org/madore/ephem/Time.java
blob: b5af9c4216356680651f0b2557ae6d02f1771361 (plain)
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
package org.madore.ephem;

public final class Time {

    public static abstract class UtcRegion {
	public int mjd;
    }

    public static final class LeapSecond extends UtcRegion {
	public int offset;
	public LeapSecond(int mjd, int offset) {
	    this.mjd = mjd;
	    this.offset = offset;
	}
    }

    public static final class RubberSecond extends UtcRegion {
	public double offset0;
	public int mjd0;
	public double scale0;
	public RubberSecond(int mjd, double offset0, int mjd0, double scale0) {
	    this.mjd = mjd;
	    this.offset0 = offset0;
	    this.mjd0 = mjd0;
	    this.scale0 = scale0;
	}
    }

    public static final UtcRegion[] leapSecondsTable = new UtcRegion[] {
	// From <URL: http://maia.usno.navy.mil/ser7/tai-utc.dat >
	new RubberSecond(37300, 1.4228180, 37300, 0.001296 ),
	new RubberSecond(37512, 1.3728180, 37300, 0.001296 ),
	new RubberSecond(37665, 1.8458580, 37665, 0.0011232),
	new RubberSecond(38334, 1.9458580, 37665, 0.0011232),
	new RubberSecond(38395, 3.2401300, 38761, 0.001296 ),
	new RubberSecond(38486, 3.3401300, 38761, 0.001296 ),
	new RubberSecond(38639, 3.4401300, 38761, 0.001296 ),
	new RubberSecond(38761, 3.5401300, 38761, 0.001296 ),
	new RubberSecond(38820, 3.6401300, 38761, 0.001296 ),
	new RubberSecond(38942, 3.7401300, 38761, 0.001296 ),
	new RubberSecond(39004, 3.8401300, 38761, 0.001296 ),
	new RubberSecond(39126, 4.3131700, 39126, 0.002592 ),
	new RubberSecond(39887, 4.2131700, 39126, 0.002592 ),
	new LeapSecond(41317, 10),
	new LeapSecond(41499, 11),
	new LeapSecond(41683, 12),
	new LeapSecond(42048, 13),
	new LeapSecond(42413, 14),
	new LeapSecond(42778, 15),
	new LeapSecond(43144, 16),
	new LeapSecond(43509, 17),
	new LeapSecond(43874, 18),
	new LeapSecond(44239, 19),
	new LeapSecond(44786, 20),
	new LeapSecond(45151, 21),
	new LeapSecond(45516, 22),
	new LeapSecond(46247, 23),
	new LeapSecond(47161, 24),
	new LeapSecond(47892, 25),
	new LeapSecond(48257, 26),
	new LeapSecond(48804, 27),
	new LeapSecond(49169, 28),
	new LeapSecond(49534, 29),
	new LeapSecond(50083, 30),
	new LeapSecond(50630, 31),
	new LeapSecond(51179, 32),
	new LeapSecond(53736, 33),
	new LeapSecond(54832, 34),
	new LeapSecond(56109, 35),
    };

    public static double utcOffset(int utcMjd, double utcSeconds) {
	// Returns TAI-UTC in seconds
	int i0 = 0;  int i1 = leapSecondsTable.length;
	while ( i1-i0 > 1 ) {
	    int i = (i0+i1)/2;
	    if ( utcMjd >= leapSecondsTable[i].mjd )
		i0 = i;
	    else
		i1 = i;
	}
	double utcOffset;
	if ( leapSecondsTable[i0] instanceof LeapSecond )
	    utcOffset = ((LeapSecond)leapSecondsTable[i0]).offset;
	else
	    utcOffset = ((RubberSecond)leapSecondsTable[i0]).offset0
		+ (utcMjd - ((RubberSecond)leapSecondsTable[i0]).mjd0) * ((RubberSecond)leapSecondsTable[i0]).scale0;
	return utcOffset;
    }

    public static double utcToTt(int utcMjd, double utcSeconds) {
	// Returns JD in TT
	return ( utcSeconds + utcOffset(utcMjd, utcSeconds) + 32.184 )/86400. + utcMjd + 2400000.5;
    }

}