Skip to content

Commit

Permalink
refactor(apple): converting to/from milliseconds (nation3#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahna-ashina committed Oct 13, 2022
1 parent ec77b14 commit df21a4d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -65,28 +66,28 @@ 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)

if (!passesUpdatedSince) {
// 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()
Expand Down
36 changes: 36 additions & 0 deletions server/utils/DateUtils.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})
22 changes: 22 additions & 0 deletions server/utils/DateUtils.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit df21a4d

Please sign in to comment.