Skip to content

Commit

Permalink
fix: 🐛 fix bug with timezone differences
Browse files Browse the repository at this point in the history
  • Loading branch information
thkruz committed Jul 29, 2022
1 parent ca8d898 commit d796c65
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
7 changes: 5 additions & 2 deletions src/utils/sun-math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,11 @@ export class SunMath {
*/
static getSolarTime(date: Date, utcOffset: number, lon: number) {
// calculate the day of year
const start = new Date(date.getFullYear(), 0, 0);
const diff = date.getTime() - start.getTime() + (start.getTimezoneOffset() - date.getTimezoneOffset()) * 60 * 1000;
const start = new Date();

start.setUTCFullYear(date.getUTCFullYear(), 0, 1);
start.setUTCHours(0, 0, 0, 0);
const diff = date.getTime() - start.getTime();
const dayOfYear = Math.floor(diff / MS_PER_DAY);

const b = (360 / 365) * (dayOfYear - 81) * DEG2RAD;
Expand Down
14 changes: 8 additions & 6 deletions test/sat/sat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

import { Sat, Sensor } from '../../lib/ootk';

const dateObj = new Date(1661400000000);

const tle1 = '1 25544U 98067A 22203.46960946 .00003068 00000+0 61583-4 0 9996';
const tle2 = '2 25544 51.6415 161.8339 0005168 35.9781 54.7009 15.50067047350657';

Expand All @@ -24,7 +26,7 @@ describe('Basic Satellite functionality', () => {
});

it('should allow chaining', () => {
const eci = new Sat({ name: 'Test', tle1, tle2 }).propagateTo(new Date(2022, 7, 25)).getEci();
const eci = new Sat({ name: 'Test', tle1, tle2 }).propagateTo(dateObj).getEci();

expect(eci.x).toBeCloseTo(6512.640035319078);
expect(eci.y).toBeCloseTo(-1545.524934684146);
Expand All @@ -34,7 +36,7 @@ describe('Basic Satellite functionality', () => {
it('should allow getting eci coordinates', () => {
const sat = new Sat({ name: 'Test', tle1, tle2 });

const eci = sat.getEci(new Date(2022, 7, 25));
const eci = sat.getEci(dateObj);

expect(eci.x).toBeCloseTo(6512.640035319078);
expect(eci.y).toBeCloseTo(-1545.524934684146);
Expand All @@ -44,7 +46,7 @@ describe('Basic Satellite functionality', () => {
it('should allow getting ecf coordinates', () => {
const sat = new Sat({ name: 'Test', tle1, tle2 });

const eci = sat.getEcf(new Date(2022, 7, 25));
const eci = sat.getEcf(dateObj);

expect(eci.x).toBeCloseTo(4585.677469309576);
expect(eci.y).toBeCloseTo(-4875.929624270418);
Expand All @@ -54,7 +56,7 @@ describe('Basic Satellite functionality', () => {
it('should allow getting lla coordinates', () => {
const sat = new Sat({ name: 'Test', tle1, tle2 });

const eci = sat.getLla(new Date(2022, 7, 25));
const eci = sat.getLla(dateObj);

expect(eci.lat).toBeCloseTo(-0.17779469476167792);
expect(eci.lon).toBeCloseTo(-0.8160653803347542);
Expand All @@ -71,7 +73,7 @@ describe('Basic Satellite functionality', () => {
const sat = new Sat({ name: 'Test', tle1, tle2 });
const sensor = new Sensor({ name: 'Test', lat: 41, lon: -71, alt: 0 });

const rae = sat.getRae(sensor, new Date(2022, 7, 25));
const rae = sat.getRae(sensor, dateObj);

rae.az *= RAD2DEG;
rae.el *= RAD2DEG;
Expand All @@ -80,7 +82,7 @@ describe('Basic Satellite functionality', () => {
expect(rae.el).toBeCloseTo(-56.89115262874271);
expect(rae.rng).toBeCloseTo(11183.206102756607);

const rae2 = sensor.getRae(sat, new Date(2022, 7, 25));
const rae2 = sensor.getRae(sat, dateObj);

rae2.az *= RAD2DEG;
rae2.el *= RAD2DEG;
Expand Down
6 changes: 4 additions & 2 deletions test/sun-moon/sun-moon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
import { Utils } from '../../lib/ootk';

const { SunMath, MoonMath } = Utils;
const dateObj = new Date(2022, 7, 25);
// Use number of milliseconds since epoch instead of local year, month, day, etc for consistency across machines
const dateObj = new Date(1661400000000);

describe('Sun and Moon', () => {
test('SunMath Unit Tests', () => {
Expand All @@ -19,7 +20,8 @@ describe('Sun and Moon', () => {
});

test('Local Solar Time', () => {
const lst = SunMath.getSolarTime(new Date(2022, 6, 25, 23, 58), -5, -71);
// Use number of milliseconds since epoch instead of local year, month, day, etc for consistency across machines
const lst = SunMath.getSolarTime(new Date(1658807880000), -5, -71);

expect(lst.toUTCString()).toEqual('Tue, 26 Jul 2022 04:07:49 GMT');
});
Expand Down

0 comments on commit d796c65

Please sign in to comment.