Skip to content

Commit 0e19925

Browse files
authored
Merge pull request #14 from jeskew/feature/protocol-timestamp
Add timestamp formatting package for use across multiple serializers
2 parents 91323bc + 3f05a81 commit 0e19925

File tree

5 files changed

+149
-0
lines changed

5 files changed

+149
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/node_modules/
2+
*.js
3+
*.js.map
4+
*.d.ts
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import {
2+
epoch,
3+
iso8601,
4+
rfc822,
5+
toDate,
6+
} from "../";
7+
8+
const toIsoString = '2017-05-22T19:33:14.175Z';
9+
const iso8601String = '2017-05-22T19:33:14Z';
10+
const rfc822String = 'Mon, 22 May 2017 19:33:14 GMT';
11+
const epochTs = 1495481594;
12+
13+
describe('iso8601', () => {
14+
it('should convert date objects to ISO-8601 strings', () => {
15+
expect(iso8601(new Date(toIsoString))).toBe(iso8601String);
16+
});
17+
18+
it('should convert parseable date strings to ISO-8601 strings', () => {
19+
let date = new Date(toIsoString);
20+
21+
expect(iso8601(date.toUTCString())).toBe(iso8601String);
22+
expect(iso8601(date.toISOString())).toBe(iso8601String);
23+
});
24+
25+
it(
26+
'should assume numbers are epoch timestamps and convert them to ISO-8601 strings accordingly',
27+
() => {
28+
expect(iso8601(epochTs)).toBe(iso8601String);
29+
}
30+
);
31+
});
32+
33+
describe('rfc822', () => {
34+
it('should convert date objects to RFC 822 strings', () => {
35+
expect(rfc822(new Date(toIsoString))).toBe(rfc822String);
36+
});
37+
38+
it('should convert parseable date strings to RFC 822 strings', () => {
39+
let date = new Date(toIsoString);
40+
41+
expect(rfc822(date.toUTCString())).toBe(rfc822String);
42+
expect(rfc822(date.toISOString())).toBe(rfc822String);
43+
});
44+
45+
it(
46+
'should assume numbers are epoch timestamps and convert them to RFC 822 strings accordingly',
47+
() => {
48+
expect(rfc822(epochTs)).toBe(rfc822String);
49+
}
50+
);
51+
});
52+
53+
describe('epoch', () => {
54+
it('should convert date objects to epoch timestamps', () => {
55+
expect(epoch(new Date(toIsoString))).toBe(epochTs);
56+
});
57+
58+
it('should convert parseable date strings to epoch timestamps', () => {
59+
let date = new Date(toIsoString);
60+
61+
expect(epoch(date.toUTCString())).toBe(epochTs);
62+
expect(epoch(date.toISOString())).toBe(epochTs);
63+
});
64+
65+
it(
66+
'should assume numbers are epoch timestamps and convert them to epoch timestamps accordingly',
67+
() => {
68+
expect(epoch(epochTs)).toBe(epochTs);
69+
}
70+
);
71+
});
72+
73+
describe('toDate', () => {
74+
it('should convert epoch timestamps to date objects', () => {
75+
const date = toDate(epochTs);
76+
expect(date).toBeInstanceOf(Date);
77+
expect(date.valueOf()).toBe(epochTs * 1000);
78+
});
79+
80+
it('should convert ISO-8601 strings to date objects', () => {
81+
const date = toDate(iso8601String);
82+
expect(date).toBeInstanceOf(Date);
83+
expect(date.valueOf()).toBe(epochTs * 1000);
84+
});
85+
86+
it('should convert RFC 822 strings to date objects', () => {
87+
const date = toDate(rfc822String);
88+
expect(date).toBeInstanceOf(Date);
89+
expect(date.valueOf()).toBe(epochTs * 1000);
90+
});
91+
});

packages/protocol-timestamp/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function iso8601(time: number|string|Date): string {
2+
return toDate(time).toISOString().replace(/\.\d{3}Z$/, 'Z');
3+
}
4+
5+
export function rfc822(time: number|string|Date): string {
6+
return toDate(time).toUTCString();
7+
}
8+
9+
export function epoch(time: number|string|Date): number {
10+
return Math.floor(toDate(time).valueOf() / 1000);
11+
}
12+
13+
export function toDate(time: number|string|Date): Date {
14+
if (typeof time === 'number') {
15+
return new Date(time * 1000);
16+
}
17+
18+
if (typeof time === 'string') {
19+
return new Date(time);
20+
}
21+
22+
return time;
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "@aws/protocol-timestamp",
3+
"private": true,
4+
"version": "0.0.1",
5+
"description": "A marshaller and unmarshaller for timestamps used by the AWS SDKs",
6+
"main": "lib/index.js",
7+
"scripts": {
8+
"prepublishOnly": "tsc",
9+
"pretest": "tsc",
10+
"test": "jest"
11+
},
12+
"author": "[email protected]",
13+
"license": "UNLICENSED",
14+
"dependencies": {
15+
"@aws/types": "^0.0.1"
16+
},
17+
"devDependencies": {
18+
"@types/jest": "^19.2.2",
19+
"jest": "^19.0.2",
20+
"typescript": "^2.3"
21+
}
22+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es5",
4+
"module": "commonjs",
5+
"declaration": true,
6+
"sourceMap": true,
7+
"strict": true
8+
}
9+
}

0 commit comments

Comments
 (0)