Skip to content

Commit a86ce16

Browse files
committed
Add timestamp formatting/parsing package for use across multiple marshallers/unmarshallers
1 parent f3b3808 commit a86ce16

File tree

5 files changed

+137
-0
lines changed

5 files changed

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

packages/protocol-timestamp/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
export function iso8601(time: number | string | Date): string {
2+
return toDate(time)
3+
.toISOString()
4+
.replace(/\.\d{3}Z$/, "Z");
5+
}
6+
7+
export function rfc822(time: number | string | Date): string {
8+
return toDate(time).toUTCString();
9+
}
10+
11+
export function epoch(time: number | string | Date): number {
12+
return Math.floor(toDate(time).valueOf() / 1000);
13+
}
14+
15+
export function toDate(time: number | string | Date): Date {
16+
if (typeof time === "number") {
17+
return new Date(time * 1000);
18+
}
19+
20+
if (typeof time === "string") {
21+
return new Date(time);
22+
}
23+
24+
return time;
25+
}
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)