From df21a4d1d418e4d3e608024ab65e22a3307a55a1 Mon Sep 17 00:00:00 2001 From: aahna-ashina <95955389+aahna-ashina@users.noreply.github.com> Date: Thu, 13 Oct 2022 20:36:03 +0800 Subject: [PATCH] refactor(apple): converting to/from milliseconds (#76) --- .../registrations/[passTypeIdentifier].ts | 11 +++--- server/utils/DateUtils.test.ts | 36 +++++++++++++++++++ server/utils/DateUtils.ts | 22 ++++++++++++ 3 files changed, 64 insertions(+), 5 deletions(-) create mode 100644 server/utils/DateUtils.test.ts create mode 100644 server/utils/DateUtils.ts diff --git a/server/pages/api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier].ts b/server/pages/api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier].ts index 09fd8bd..fa08abb 100644 --- a/server/pages/api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier].ts +++ b/server/pages/api/apple/v1/devices/[deviceLibraryIdentifier]/registrations/[passTypeIdentifier].ts @@ -1,6 +1,7 @@ import { NextApiRequest, NextApiResponse } from 'next' import { config } from '../../../../../../../utils/Config' import { supabase } from '../../../../../../../utils/SupabaseClient' +import { getDate, getTimeInSeconds } from '../../../../../../../utils/DateUtils' /** * Get the List of Updatable Passes. Implementation of @@ -65,7 +66,7 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { error: 'Internal Server Error: ' + latest_updates_result.error.message }) } else { - // Convert from ISO string to Date + // Convert from ISO string ('2022-09-30T12:12:17') to Date const latestUpdateDate: Date = new Date(latest_updates_result.data['time']) console.log('latestUpdateDate:', latestUpdateDate) @@ -73,20 +74,20 @@ export default function handler(req: NextApiRequest, res: NextApiResponse) { // The passes on this device have not been updated previously, so return all passes. res.status(200).json({ serialNumbers: serialNumbers, - lastUpdated: String(Math.round(latestUpdateDate.getTime() / 1000)) + lastUpdated: String(getTimeInSeconds(latestUpdateDate)) }) } else { // The passes on this device have been updated previously, so only return passes that // were updated before the most recent Nation3 update in the `latest_updates` database table. - // Convert from epoch timestamp string to Date - const passesUpdatedSinceDate: Date = new Date(Number(passesUpdatedSince) * 1000) + // Convert from epoch timestamp string ('1662889385') to Date + const passesUpdatedSinceDate: Date = getDate(Number(passesUpdatedSince)) console.log('passesUpdatedSinceDate:', passesUpdatedSinceDate) if (passesUpdatedSinceDate.getTime() < latestUpdateDate.getTime()) { res.status(200).json({ serialNumbers: serialNumbers, - lastUpdated: String(Math.round(latestUpdateDate.getTime() / 1000)) + lastUpdated: String(getTimeInSeconds(latestUpdateDate)) }) } else { res.status(204).end() diff --git a/server/utils/DateUtils.test.ts b/server/utils/DateUtils.test.ts new file mode 100644 index 0000000..60b4369 --- /dev/null +++ b/server/utils/DateUtils.test.ts @@ -0,0 +1,36 @@ +import { describe, expect, test } from '@jest/globals' +import { getTimeInSeconds, getDate } from './DateUtils' + +describe('getTimeInSeconds', () => { + test('getTimeInSeconds - 2022-09-30T04:12:17Z', () => { + const date: Date = new Date('2022-09-30T04:12:17Z') + console.log('date:', date) + expect(getTimeInSeconds(date)).toBe(1664511137) + }) + + test('getTimeInSeconds - 2022-09-30T04:12:17.000Z', () => { + const date: Date = new Date('2022-09-30T04:12:17.000Z') + console.log('date:', date) + expect(getTimeInSeconds(date)).toBe(1664511137) + }) + + test('getTimeInSeconds - 2022-09-30T04:12:17.499Z', () => { + const date: Date = new Date('2022-09-30T04:12:17.499Z') + console.log('date:', date) + expect(getTimeInSeconds(date)).toBe(1664511137) + }) + + test('getTimeInSeconds - 2022-09-30T04:12:17.500Z', () => { + const date: Date = new Date('2022-09-30T04:12:17.500Z') + console.log('date:', date) + expect(getTimeInSeconds(date)).toBe(1664511138) + }) +}) + +describe('getDate', () => { + test('getDate - 1662889385', () => { + const date: Date = getDate(1662889385) + console.log('date:', date) + expect(date.toISOString()).toBe('2022-09-11T09:43:05.000Z') + }) +}) diff --git a/server/utils/DateUtils.ts b/server/utils/DateUtils.ts new file mode 100644 index 0000000..81409c5 --- /dev/null +++ b/server/utils/DateUtils.ts @@ -0,0 +1,22 @@ +/** + * Calculates a Date's epoch timestamp in seconds. Rounds to the nearest integer to avoid decimals. + */ +export function getTimeInSeconds(date: Date): number { + console.log('getTimeInSeconds') + const timeInMilliseconds: number = date.getTime() + const timeInSeconds: number = timeInMilliseconds / 1000 + const timeInSecondsRoundedToNearestInteger: number = Math.round(timeInSeconds) + return timeInSecondsRoundedToNearestInteger +} + +/** + * Converts from an epoch timestamp (e.g. 1662889385) to Date. + * + * @param timeInSeconds The UNIX timestamp in seconds, e.g. 1662889385. + */ +export function getDate(timeInSeconds: number): Date { + console.log('getDate') + const timeInMilliseconds: number = timeInSeconds * 1000 + const date: Date = new Date(timeInMilliseconds) + return date +}