Skip to content

Commit

Permalink
Fix: solar automation (#6840)
Browse files Browse the repository at this point in the history
  • Loading branch information
bershanskiy committed Sep 21, 2021
1 parent 998c900 commit 04d7d4a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 58 deletions.
8 changes: 4 additions & 4 deletions src/background/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import TabManager from './tab-manager';
import UserStorage from './user-storage';
import {setWindowTheme, resetWindowTheme} from './window-theme';
import {getCommands, setShortcut, canInjectScript} from './utils/extension-api';
import {isInTimeIntervalLocal, nextTimeInterval, isNightAtLocation, nextNightAtLocation} from '../utils/time';
import {isInTimeIntervalLocal, nextTimeInterval, isNightAtLocation, nextTimeChangeAtLocation} from '../utils/time';
import {isURLInList, getURLHostOrProtocol, isURLEnabled, isPDF} from '../utils/url';
import ThemeEngines from '../generators/theme-engines';
import createCSSFilterStylesheet from '../generators/css-filter';
Expand Down Expand Up @@ -85,11 +85,11 @@ export class Extension {
}
}

private alarmListener(alarm: chrome.alarms.Alarm): void {
private alarmListener = (alarm: chrome.alarms.Alarm): void => {
if (alarm.name === Extension.ALARM_NAME) {
this.handleAutoCheck();
}
}
};

recalculateIsEnabled(): boolean {
if (!this.user.settings) {
Expand Down Expand Up @@ -124,7 +124,7 @@ export class Extension {

if (latitude != null && longitude != null) {
this.isEnabled = isNightAtLocation(latitude, longitude);
nextCheck = nextNightAtLocation(latitude, longitude);
nextCheck = nextTimeChangeAtLocation(latitude, longitude);
}
break;
}
Expand Down
42 changes: 16 additions & 26 deletions src/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ export function isNightAtLocation(
return isInTimeIntervalUTC(sunsetTime, sunriseTime, currentTime);
}

export function nextNightAtLocation(
export function nextTimeChangeAtLocation(
latitude: number,
longitude: number,
date: Date = new Date(),
Expand All @@ -263,41 +263,31 @@ export function nextNightAtLocation(
return date.getTime() + getDuration({days: 1});
}

const sunriseTime = time.sunriseTime;
const sunsetTime = time.sunsetTime;
const [firstTimeOnDay, lastTimeOnDay] = time.sunriseTime < time.sunsetTime ? [time.sunriseTime, time.sunsetTime] : [time.sunsetTime, time.sunriseTime];
const currentTime = (
date.getUTCHours() * getDuration({hours: 1}) +
date.getUTCMinutes() * getDuration({minutes: 1}) +
date.getUTCSeconds() * getDuration({seconds: 1}) +
date.getUTCMilliseconds()
);

if (sunriseTime < sunsetTime) {
// Timeline:
// --- sunrise <----> sunset ---
if ((sunriseTime < currentTime) && (currentTime < sunsetTime)) {
// Timeline:
// --- sunrise <----> sunset ---
// ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0, sunsetTime);
}
if (currentTime <= firstTimeOnDay) {
// Timeline:
// --- sunrise <----> sunset ---
// ^ ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + (sunsetTime < currentTime ? 1 : 0), 0, 0, 0, sunriseTime);
// --- firstTimeOnDay <---> lastTimeOnDay ---
// ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0, firstTimeOnDay);
}
if ((sunsetTime < currentTime) && (currentTime < sunriseTime)) {
if (currentTime <= lastTimeOnDay) {
// Timeline:
// --- sunset <----> sunrise ---
// ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0, sunriseTime);
// --- firstTimeOnDay <---> lastTimeOnDay ---
// ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate(), 0, 0, 0, lastTimeOnDay);
}
// Timeline:
// --- sunset <----> sunrise ---
// ^ ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + (sunriseTime < currentTime ? 1 : 0), 0, 0, 0, sunsetTime);
// --- firstTimeOnDay <---> lastTimeOnDay ---
// ^
// Current time
return Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() + 1, 0, 0, 0, firstTimeOnDay);
}
56 changes: 28 additions & 28 deletions tests/utils/time.tests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isInTimeIntervalLocal, nextTimeInterval, isNightAtLocation, nextNightAtLocation, parseTime, getDuration, getDurationInMinutes} from '../../src/utils/time';
import {isInTimeIntervalLocal, nextTimeInterval, isNightAtLocation, nextTimeChangeAtLocation, parseTime, getDuration, getDurationInMinutes} from '../../src/utils/time';

test('Time interval', () => {
// isInTimeIntervalLocal is time-zone dependent
Expand Down Expand Up @@ -94,35 +94,35 @@ test('Nigth check', () => {
test('Sunrise/sunset', () => {
const utcDate = (y: number, m: number, d: number, hh: number, mm: number) => new Date(Date.UTC(y, m, d, hh, mm));

expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 5, 24, 2, 501));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 5, 0))).toEqual(Date.UTC(2019, 8, 9, 5, 24, 2, 501));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 12, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 18, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 20, 0))).toEqual(Date.UTC(2019, 8, 10, 5, 24, 2, 501));
expect(nextNightAtLocation(52, 0, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 5, 24, 2, 501));

expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 3, 23, 54, 365));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 3, 0))).toEqual(Date.UTC(2019, 8, 9, 3, 23, 54, 365));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 5, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 10, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 16, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 18, 0))).toEqual(Date.UTC(2019, 8, 10, 3, 23, 54, 365));
expect(nextNightAtLocation(52, 30, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 3, 23, 54, 365));

expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 7, 24, 10, 637));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 7, 24, 10, 637));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 9, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 14, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 20, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 22, 0))).toEqual(Date.UTC(2019, 8, 10, 7, 24, 10, 637));
expect(nextNightAtLocation(52, -30, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 7, 24, 10, 637));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 5, 24, 2, 501));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 5, 0))).toEqual(Date.UTC(2019, 8, 9, 5, 24, 2, 501));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 12, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 18, 0))).toEqual(Date.UTC(2019, 8, 9, 18, 29, 46, 448));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 20, 0))).toEqual(Date.UTC(2019, 8, 10, 5, 24, 2, 501));
expect(nextTimeChangeAtLocation(52, 0, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 5, 24, 2, 501));

expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 3, 23, 54, 365));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 3, 0))).toEqual(Date.UTC(2019, 8, 9, 3, 23, 54, 365));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 5, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 10, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 16, 0))).toEqual(Date.UTC(2019, 8, 9, 16, 29, 58, 45));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 18, 0))).toEqual(Date.UTC(2019, 8, 10, 3, 23, 54, 365));
expect(nextTimeChangeAtLocation(52, 30, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 3, 23, 54, 365));

expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 7, 24, 10, 637));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 7, 24, 10, 637));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 9, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 14, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 20, 0))).toEqual(Date.UTC(2019, 8, 9, 20, 29, 34, 848));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 22, 0))).toEqual(Date.UTC(2019, 8, 10, 7, 24, 10, 637));
expect(nextTimeChangeAtLocation(52, -30, utcDate(2019, 8, 9, 23, 59))).toEqual(Date.UTC(2019, 8, 10, 7, 24, 10, 637));

// Polar day and night
expect(nextNightAtLocation(71, 0, utcDate(2019, 5, 15, 0, 0))).toEqual(Date.UTC(2019, 5, 16, 0, 0, 0, 0));
expect(nextNightAtLocation(-71, 0, utcDate(2019, 5, 15, 0, 0))).toEqual(Date.UTC(2019, 5, 16, 0, 0, 0, 0));
expect(nextTimeChangeAtLocation(71, 0, utcDate(2019, 5, 15, 0, 0))).toEqual(Date.UTC(2019, 5, 16, 0, 0, 0, 0));
expect(nextTimeChangeAtLocation(-71, 0, utcDate(2019, 5, 15, 0, 0))).toEqual(Date.UTC(2019, 5, 16, 0, 0, 0, 0));

// Places where sunset comes before sunrise (in UTC)
expect(nextNightAtLocation(0, 180, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 6, 0, 50, 152));
expect(nextNightAtLocation(0, 180, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 17, 54, 18, 610));
expect(nextTimeChangeAtLocation(0, 180, utcDate(2019, 8, 9, 0, 0))).toEqual(Date.UTC(2019, 8, 9, 6, 0, 50, 152));
expect(nextTimeChangeAtLocation(0, 180, utcDate(2019, 8, 9, 7, 0))).toEqual(Date.UTC(2019, 8, 9, 17, 54, 18, 610));
});

0 comments on commit 04d7d4a

Please sign in to comment.