forked from nation3/mobile-passport
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(apple): converting to/from milliseconds (nation3#76)
- Loading branch information
1 parent
ec77b14
commit df21a4d
Showing
3 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |