-
Notifications
You must be signed in to change notification settings - Fork 4
/
EpochGPS.ts
51 lines (43 loc) · 1.55 KB
/
EpochGPS.ts
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
import { DataHandler } from '../data/DataHandler';
import { secondsPerWeek } from '../utils/constants';
import type { EpochUTC } from './EpochUTC';
// / Global Positioning System _(GPS)_ formatted epoch.
export class EpochGPS {
/**
* Create a new GPS epoch given the [week] since reference epoch, and number
* of [seconds] into the [week].
* @param week Number of weeks since the GPS reference epoch.
* @param seconds Number of seconds into the week.
* @param reference Reference epoch.
*/
constructor(public week: number, public seconds: number, reference: EpochUTC) {
if (week < 0) {
throw new Error('GPS week must be non-negative.');
}
if (seconds < 0 || seconds >= secondsPerWeek) {
throw new Error('GPS seconds must be within a week.');
}
EpochGPS.reference = reference;
}
// / Number of weeks since the GPS reference epoch.
static reference: EpochUTC;
// / GPS leap second difference from TAI/UTC offsets.
static offset = 19;
// / Get GPS week accounting for 10-bit rollover.
get week10Bit(): number {
return this.week % 2 ** 10;
}
// / Get GPS week accounting for 13-bit rollover.
get week13Bit(): number {
return this.week % 2 ** 13;
}
toString(): string {
return `${this.week}:${this.seconds.toFixed(3)}`;
}
// / Convert this to a UTC epoch.
toUTC(): EpochUTC {
const init = EpochGPS.reference.roll(this.week * secondsPerWeek + this.seconds);
const ls = DataHandler.getInstance().getLeapSeconds(init.toJulianDate());
return init.roll(-(ls - EpochGPS.offset));
}
}