diff --git a/ts/background.ts b/ts/background.ts index 1ae6d91040..dd7ff439db 100644 --- a/ts/background.ts +++ b/ts/background.ts @@ -28,7 +28,7 @@ import { filter } from './util/iterables'; import { isNotNil } from './util/isNotNil'; import { senderCertificateService } from './services/senderCertificate'; import { routineProfileRefresh } from './routineProfileRefresh'; -import { isMoreRecentThan, isOlderThan } from './util/timestamp'; +import { isMoreRecentThan, isOlderThan, toDayMillis } from './util/timestamp'; import { isValidReactionEmoji } from './reactions/isValidReactionEmoji'; import { ConversationModel } from './models/conversations'; import { getMessageById } from './messages/getMessageById'; @@ -672,7 +672,7 @@ export async function startApp(): Promise { webFrame.setZoomFactor(window.Events.getZoomFactor()); // How long since we were last running? - const lastHeartbeat = window.storage.get('lastHeartbeat', 0); + const lastHeartbeat = toDayMillis(window.storage.get('lastHeartbeat', 0)); const previousLastStartup = window.storage.get('lastStartup'); await window.storage.put('lastStartup', Date.now()); @@ -685,10 +685,10 @@ export async function startApp(): Promise { } // Start heartbeat timer - window.storage.put('lastHeartbeat', Date.now()); + window.storage.put('lastHeartbeat', toDayMillis(Date.now())); const TWELVE_HOURS = 12 * 60 * 60 * 1000; setInterval( - () => window.storage.put('lastHeartbeat', Date.now()), + () => window.storage.put('lastHeartbeat', toDayMillis(Date.now())), TWELVE_HOURS ); diff --git a/ts/test-both/util/timestamp_test.ts b/ts/test-both/util/timestamp_test.ts index 7ff94a41e9..f920da3be4 100644 --- a/ts/test-both/util/timestamp_test.ts +++ b/ts/test-both/util/timestamp_test.ts @@ -3,7 +3,11 @@ import { assert } from 'chai'; -import { isOlderThan, isMoreRecentThan } from '../../util/timestamp'; +import { + isOlderThan, + isMoreRecentThan, + toDayMillis, +} from '../../util/timestamp'; const ONE_HOUR = 3600 * 1000; const ONE_DAY = 24 * ONE_HOUR; @@ -34,4 +38,17 @@ describe('timestamp', () => { ); }); }); + + describe('toDayMillis', () => { + const now = new Date(); + const today = new Date(toDayMillis(now.valueOf())); + + assert.strictEqual(today.getUTCMilliseconds(), 0); + assert.strictEqual(today.getUTCHours(), 0); + assert.strictEqual(today.getUTCMinutes(), 0); + assert.strictEqual(today.getUTCSeconds(), 0); + assert.strictEqual(today.getUTCDate(), now.getUTCDate()); + assert.strictEqual(today.getUTCMonth(), now.getUTCMonth()); + assert.strictEqual(today.getUTCFullYear(), now.getUTCFullYear()); + }); }); diff --git a/ts/util/timestamp.ts b/ts/util/timestamp.ts index 5fdf406ace..eb23eac6b1 100644 --- a/ts/util/timestamp.ts +++ b/ts/util/timestamp.ts @@ -1,6 +1,8 @@ // Copyright 2021 Signal Messenger, LLC // SPDX-License-Identifier: AGPL-3.0-only +const ONE_DAY = 24 * 3600 * 1000; + export function isMoreRecentThan(timestamp: number, delta: number): boolean { return timestamp > Date.now() - delta; } @@ -16,3 +18,7 @@ export function isInPast(timestamp: number): boolean { export function isInFuture(timestamp: number): boolean { return isMoreRecentThan(timestamp, 0); } + +export function toDayMillis(timestamp: number): number { + return timestamp - (timestamp % ONE_DAY); +}