forked from aws-amplify/amplify-cli
-
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.
Merge pull request aws-amplify#2388 from yuth/issue-2300
fix(amplify-appsync-simulator): impl missing methods in velocity utils
- Loading branch information
Showing
5 changed files
with
206 additions
and
26 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
101 changes: 101 additions & 0 deletions
101
packages/amplify-appsync-simulator/src/__tests__/velocity/util/time.test.ts
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,101 @@ | ||
import { time as Time } from '../../../velocity/util/time'; | ||
|
||
describe('Velocity $context.util.time', () => { | ||
let dateNowSpy; | ||
let time; | ||
const FORMAT_CUSTOM_ZONED = 'yyyy-MM-dd HH:mm:ss.SSS Z'; | ||
const FORMAT_CUSTOM_UNZONED = 'yyyy-MM-dd HH:mm:ss.SSS'; | ||
const TEST_TIMESTAMP_MILLIS = 1267378472045; // 2010-02-28T17:34:32.045Z | ||
const TEST_TIMESTAMP_SECS = 1267378472; | ||
const TEST_TIMESTAMP_ZULU = '2010-02-28T17:34:32.045Z'; | ||
const TEST_TIMESTAMP_PLUS8 = '2010-03-01T01:34:32.045+08:00'; | ||
const TEST_TIMESTAMP_CUSTOM_UTC = '2010-02-28 17:34:32.045 +0000'; | ||
const TEST_TIMESTAMP_CUSTOM_PLUS8 = '2010-03-01 01:34:32.045 +0800'; | ||
const TEST_TIMESTAMP_CUSTOM_UTC_UNZONED = '2010-02-28 17:34:32.045'; | ||
const TEST_TIMESTAMP_CUSTOM_PLUS8_UNZONED = '2010-03-01 01:34:32.045'; | ||
|
||
beforeAll(() => { | ||
// freeze time | ||
time = Time(); | ||
dateNowSpy = jest.spyOn(Date, 'now').mockImplementation(() => TEST_TIMESTAMP_MILLIS); | ||
}); | ||
afterAll(() => { | ||
// unfreeze time | ||
dateNowSpy.mockRestore(); | ||
}); | ||
|
||
it('nowISO8601', () => { | ||
expect(time.nowISO8601()).toEqual(TEST_TIMESTAMP_ZULU); | ||
}); | ||
|
||
it('nowEpochSeconds', () => { | ||
expect(time.nowEpochSeconds()).toEqual(TEST_TIMESTAMP_SECS); | ||
}); | ||
|
||
it('nowEpochMilliSeconds', () => { | ||
expect(time.nowEpochMilliSeconds()).toEqual(TEST_TIMESTAMP_MILLIS); | ||
}); | ||
|
||
it('parseFormattedToEpochMilliSeconds', () => { | ||
expect( | ||
time.parseFormattedToEpochMilliSeconds(TEST_TIMESTAMP_CUSTOM_UTC, FORMAT_CUSTOM_ZONED) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
expect( | ||
time.parseFormattedToEpochMilliSeconds(TEST_TIMESTAMP_CUSTOM_PLUS8, FORMAT_CUSTOM_ZONED) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
expect( | ||
time.parseFormattedToEpochMilliSeconds(TEST_TIMESTAMP_CUSTOM_UTC, FORMAT_CUSTOM_ZONED) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
expect( | ||
time.parseFormattedToEpochMilliSeconds( | ||
TEST_TIMESTAMP_CUSTOM_PLUS8, | ||
FORMAT_CUSTOM_ZONED, | ||
'Australia/Perth' | ||
) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
expect( | ||
time.parseFormattedToEpochMilliSeconds( | ||
TEST_TIMESTAMP_CUSTOM_UTC_UNZONED, | ||
FORMAT_CUSTOM_UNZONED, | ||
'UTC' | ||
) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
|
||
expect( | ||
time.parseFormattedToEpochMilliSeconds( | ||
TEST_TIMESTAMP_CUSTOM_PLUS8_UNZONED, | ||
FORMAT_CUSTOM_UNZONED, | ||
'Australia/Perth' | ||
) | ||
).toEqual(TEST_TIMESTAMP_MILLIS); | ||
}); | ||
|
||
it('parseISO8601ToEpochMilliSeconds', () => { | ||
expect(time.parseISO8601ToEpochMilliSeconds(TEST_TIMESTAMP_ZULU)).toEqual( | ||
TEST_TIMESTAMP_MILLIS | ||
); | ||
expect(time.parseISO8601ToEpochMilliSeconds(TEST_TIMESTAMP_PLUS8)).toEqual( | ||
TEST_TIMESTAMP_MILLIS | ||
); | ||
}); | ||
it('epochMilliSecondsToSeconds', () => { | ||
expect(time.epochMilliSecondsToSeconds(TEST_TIMESTAMP_MILLIS)).toEqual(TEST_TIMESTAMP_SECS); | ||
}); | ||
it('epochMilliSecondsToISO8601', () => { | ||
expect(time.epochMilliSecondsToISO8601(TEST_TIMESTAMP_MILLIS)).toEqual(TEST_TIMESTAMP_ZULU); | ||
}); | ||
|
||
it('epochMilliSecondsToFormatted', () => { | ||
expect(time.epochMilliSecondsToFormatted(TEST_TIMESTAMP_MILLIS, FORMAT_CUSTOM_ZONED)).toEqual( | ||
TEST_TIMESTAMP_CUSTOM_UTC | ||
); | ||
|
||
expect( | ||
time.epochMilliSecondsToFormatted( | ||
TEST_TIMESTAMP_MILLIS, | ||
FORMAT_CUSTOM_ZONED, | ||
'Australia/Perth' | ||
) | ||
).toEqual(TEST_TIMESTAMP_CUSTOM_PLUS8); | ||
}); | ||
}); |
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
99 changes: 78 additions & 21 deletions
99
packages/amplify-appsync-simulator/src/velocity/util/time.ts
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 |
---|---|---|
@@ -1,32 +1,89 @@ | ||
import * as dateformat from 'dateformat'; | ||
import * as moment from 'moment'; | ||
import 'moment-timezone'; | ||
import 'moment-jdateformatparser'; | ||
|
||
export const time = now => ({ | ||
nowISO8601() { | ||
return now.toISOString(); | ||
declare module 'moment' { | ||
export interface Moment { | ||
toMomentFormatString: (format: string) => string; | ||
formatWithJDF: (format: string) => string; | ||
} | ||
} | ||
|
||
const parseTimestamp = (dateTime: string, format?: string, timezone?: string): moment.Moment => { | ||
if (!dateTime || !format) { | ||
return null; | ||
} | ||
try { | ||
const momentFormatString = moment().toMomentFormatString(format); | ||
|
||
return timezone | ||
? moment.tz(dateTime, momentFormatString, timezone) | ||
: moment(dateTime, momentFormatString); | ||
} catch (e) { | ||
return null; | ||
} | ||
} | ||
|
||
export const time = () => ({ | ||
nowISO8601(t): string { | ||
return moment().toISOString(); | ||
}, | ||
nowEpochSeconds() { | ||
return parseInt((now.valueOf() / 1000).toString(), 10); | ||
nowEpochSeconds(): number { | ||
return moment().unix(); | ||
}, | ||
nowEpochMilliSeconds() { | ||
return now.valueOf(); | ||
nowEpochMilliSeconds(): number { | ||
return moment().valueOf(); | ||
}, | ||
nowFormatted(format, timezone = null) { | ||
if (timezone) throw new Error('no support for setting timezone!'); | ||
return dateformat(now, format); | ||
nowFormatted(format: string, timezone: string = null): string | null { | ||
try { | ||
if (timezone) { | ||
return moment() | ||
.tz(timezone) | ||
.formatWithJDF(format); | ||
} | ||
|
||
return moment().formatWithJDF(format); | ||
} catch (e) { | ||
return null; | ||
} | ||
}, | ||
parseFormattedToEpochMilliSeconds() { | ||
throw new Error('not implemented'); | ||
parseFormattedToEpochMilliSeconds( | ||
dateTime: string, | ||
format: string, | ||
timezone?: string | ||
): number | null { | ||
const timestamp = parseTimestamp(dateTime, format, timezone); | ||
return timestamp ? timestamp.valueOf() : null; | ||
}, | ||
parseISO8601ToEpochMilliSeconds() { | ||
throw new Error('not implemented'); | ||
parseISO8601ToEpochMilliSeconds(dateTime): number | null { | ||
const timestamp = parseTimestamp(dateTime, 'YYYY-MM-DDTHH:mm:ss.SZ'); | ||
return timestamp ? timestamp.valueOf() : null; | ||
}, | ||
epochMilliSecondsToSeconds() { | ||
throw new Error('not implemented'); | ||
epochMilliSecondsToSeconds(milliseconds: number): number | null { | ||
try { | ||
return Math.floor(milliseconds / 1000); | ||
} catch (e) { | ||
return null; | ||
} | ||
}, | ||
epochMilliSecondsToISO8601() { | ||
throw new Error('not implemented'); | ||
epochMilliSecondsToISO8601(dateTime: number): string | null { | ||
try { | ||
return moment(dateTime).toISOString(); | ||
} catch (e) { | ||
return null; | ||
} | ||
}, | ||
epochMilliSecondsToFormatted() { | ||
throw new Error('not implemented'); | ||
epochMilliSecondsToFormatted( | ||
timestamp: number, | ||
format: string, | ||
timezone: string = 'UTC' | ||
): string | null { | ||
try { | ||
return moment(timestamp) | ||
.tz(timezone) | ||
.formatWithJDF(format); | ||
} catch (e) { | ||
return null; | ||
} | ||
}, | ||
}); |
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