Skip to content

Commit

Permalink
refactor(apple): add dateutils class (nation3#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
aahna-ashina committed Oct 16, 2022
1 parent df21a4d commit 293fc43
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { NextApiRequest, NextApiResponse } from 'next'
import { config } from '../../../../../../../utils/Config'
import { supabase } from '../../../../../../../utils/SupabaseClient'
import { getDate, getTimeInSeconds } from '../../../../../../../utils/DateUtils'
import { DateUtils } from '../../../../../../../utils/DateUtils'

/**
* Get the List of Updatable Passes. Implementation of
Expand Down Expand Up @@ -74,20 +73,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(getTimeInSeconds(latestUpdateDate))
lastUpdated: String(DateUtils.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 ('1662889385') to Date
const passesUpdatedSinceDate: Date = getDate(Number(passesUpdatedSince))
const passesUpdatedSinceDate: Date = DateUtils.getDate(Number(passesUpdatedSince))
console.log('passesUpdatedSinceDate:', passesUpdatedSinceDate)

if (passesUpdatedSinceDate.getTime() < latestUpdateDate.getTime()) {
res.status(200).json({
serialNumbers: serialNumbers,
lastUpdated: String(getTimeInSeconds(latestUpdateDate))
lastUpdated: String(DateUtils.getTimeInSeconds(latestUpdateDate))
})
} else {
res.status(204).end()
Expand Down
43 changes: 23 additions & 20 deletions server/utils/DateUtils.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
/**
* 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
}
export class DateUtils {

/**
* Calculates a Date's epoch timestamp in seconds. Rounds to the nearest integer to avoid decimals.
*/
static 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
/**
* Converts from an epoch timestamp (e.g. 1662889385) to Date.
*
* @param timeInSeconds The UNIX timestamp in seconds, e.g. 1662889385.
*/
static getDate(timeInSeconds: number): Date {
console.log('getDate')
const timeInMilliseconds: number = timeInSeconds * 1000
const date: Date = new Date(timeInMilliseconds)
return date
}
}

0 comments on commit 293fc43

Please sign in to comment.