Skip to content

Commit 2e4f55d

Browse files
authored
bugfix: properly pad milliseconds (#44)
1 parent 24ae0d3 commit 2e4f55d

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-7
lines changed

src/statement/hydrateDate.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,46 @@ describe("hydrate Date", () => {
6565
expect(minute).toEqual(0);
6666
expect(second).toEqual(10);
6767
});
68+
it("hydrate milliseconds", () => {
69+
const input = "2033-02-13 12:00:10.123+01:00";
70+
const date = hydrateDate(input);
71+
72+
const year = date?.getUTCFullYear();
73+
const month = date?.getUTCMonth();
74+
const day = date?.getUTCDate();
75+
const hour = date?.getUTCHours();
76+
const minute = date?.getUTCMinutes();
77+
const second = date?.getUTCSeconds();
78+
const milliseconds = date?.getUTCMilliseconds();
79+
80+
expect(year).toEqual(2033);
81+
expect(month).toEqual(1);
82+
expect(day).toEqual(13);
83+
expect(hour).toEqual(11);
84+
expect(minute).toEqual(0);
85+
expect(second).toEqual(10);
86+
expect(milliseconds).toEqual(123);
87+
});
88+
it("hydrate microseconds", () => {
89+
const input = "2033-02-13 12:00:10.123456+01:00";
90+
const date = hydrateDate(input);
91+
92+
const year = date?.getUTCFullYear();
93+
const month = date?.getUTCMonth();
94+
const day = date?.getUTCDate();
95+
const hour = date?.getUTCHours();
96+
const minute = date?.getUTCMinutes();
97+
const second = date?.getUTCSeconds();
98+
const milliseconds = date?.getUTCMilliseconds();
99+
100+
expect(year).toEqual(2033);
101+
expect(month).toEqual(1);
102+
expect(day).toEqual(13);
103+
expect(hour).toEqual(11);
104+
expect(minute).toEqual(0);
105+
expect(second).toEqual(10);
106+
expect(milliseconds).toEqual(123);
107+
});
68108
it("hydrate timestamptz type 2", () => {
69109
const input = "2033-02-13 12:00:10-01:00";
70110
const date = hydrateDate(input);

src/statement/hydrateDate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const hydrateDate = (value: string) => {
4040
const hour = zeroPad(match[4] || "00", 2);
4141
const minute = zeroPad(match[5] || "00", 2);
4242
const second = zeroPad(match[6] || "00", 2);
43-
const msec = zeroPad(match[7] || "000", 6);
43+
const msec = zeroPad(match[7] || "000", 6, "right");
4444

4545
// tz
4646
const sign = match[8] || "-";

test/unit/statement.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe("format query", () => {
9393
const queryFormatter = new QueryFormatter();
9494
const query = "select ? from table";
9595
const formattedQuery = queryFormatter.formatQuery(query, [
96-
new Date("2022-05-04 17:37:19")
96+
new Date("2022-05-04 17:37:19 UTC")
9797
]);
9898
expect(formattedQuery).toMatchInlineSnapshot(
9999
`"select '2022-05-04 17:37:19' from table"`
@@ -104,7 +104,7 @@ describe("format query", () => {
104104
const queryFormatter = new QueryFormatter();
105105
const query = "select ? from table";
106106
const formattedQuery = queryFormatter.formatQuery(query, [
107-
new Date("1000-01-01 12:21:21")
107+
new Date("1000-01-01 12:21:21 UTC")
108108
]);
109109
expect(formattedQuery).toMatchInlineSnapshot(
110110
`"select '1000-01-01 12:21:21' from table"`
@@ -165,7 +165,7 @@ describe("format query", () => {
165165
const queryFormatter = new QueryFormatter();
166166
const query = "insert into foo values(?, ?)";
167167
const formattedQuery = queryFormatter.formatQuery(query, [
168-
new TimestampTZ("2023-12-12 00:00:00", { timeZone: "UTC" }),
168+
new TimestampTZ("2023-12-12 00:00:00 UTC", { timeZone: "UTC" }),
169169
"str"
170170
]);
171171
expect(formattedQuery).toMatchInlineSnapshot(
@@ -176,7 +176,7 @@ describe("format query", () => {
176176
const queryFormatter = new QueryFormatter();
177177
const query = "insert into foo values(?, ?)";
178178
const formattedQuery = queryFormatter.formatQuery(query, [
179-
new TimestampTZ("2023-12-12 00:00:00.123", { timeZone: "UTC" }),
179+
new TimestampTZ("2023-12-12 00:00:00.123 UTC", { timeZone: "UTC" }),
180180
"str"
181181
]);
182182
expect(formattedQuery).toMatchInlineSnapshot(
@@ -187,7 +187,7 @@ describe("format query", () => {
187187
const queryFormatter = new QueryFormatter();
188188
const query = "insert into foo values(?, ?)";
189189
const formattedQuery = queryFormatter.formatQuery(query, [
190-
new TimestampNTZ("2023-12-12 00:00:00"),
190+
new TimestampNTZ("2023-12-12 00:00:00 UTC"),
191191
"str"
192192
]);
193193
expect(formattedQuery).toMatchInlineSnapshot(
@@ -198,7 +198,7 @@ describe("format query", () => {
198198
const queryFormatter = new QueryFormatter();
199199
const query = "insert into foo values(?, ?)";
200200
const formattedQuery = queryFormatter.formatQuery(query, [
201-
new TimestampNTZ("2023-12-12 00:00:00.123"),
201+
new TimestampNTZ("2023-12-12 00:00:00.123 UTC"),
202202
"str"
203203
]);
204204
expect(formattedQuery).toMatchInlineSnapshot(

0 commit comments

Comments
 (0)