Skip to content

Commit 1a2c6cc

Browse files
authored
Fix dateToRFC3339String parsing (#1201)
* Fix dateToRFC3339String parsing We can't use `getTimezoneOffset` as it's a local value independent of the Date object passed. Fixes #1200 * Template
1 parent 0644b18 commit 1a2c6cc

File tree

3 files changed

+15
-23
lines changed

3 files changed

+15
-23
lines changed

.generator/src/generator/templates/util.j2

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export function dateToRFC3339String(date: Date | DDate): string {
4848
const year = date.getFullYear() ;
4949
const month = date.getMonth();
5050
const day = date.getUTCDate();
51-
const hour = date.getUTCHours() + +tzHour;
52-
const minute = date.getUTCMinutes() + +tzMin;
51+
const hour = date.getUTCHours() + tzHour;
52+
const minute = date.getUTCMinutes() + tzMin;
5353
const second = date.getUTCSeconds();
5454

5555
let msec = date.getUTCMilliseconds().toString();
@@ -81,13 +81,5 @@ function getRFC3339TimezoneOffset(date: Date | DDate): string {
8181
if (date instanceof DDate && date.rfc3339TzOffset) {
8282
return date.rfc3339TzOffset;
8383
}
84-
85-
let offset = date.getTimezoneOffset()
86-
if (offset === 0) {
87-
return "Z";
88-
}
89-
90-
const offsetSign = (offset > 0) ? "+" : "-";
91-
offset = Math.abs(offset);
92-
return offsetSign + pad(Math.floor(offset / 60)) + ":" + pad(offset % 60);
84+
return "Z";
9385
}

packages/datadog-api-client-common/util.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export function dateToRFC3339String(date: Date | DDate): string {
5252
const year = date.getFullYear();
5353
const month = date.getMonth();
5454
const day = date.getUTCDate();
55-
const hour = date.getUTCHours() + +tzHour;
56-
const minute = date.getUTCMinutes() + +tzMin;
55+
const hour = date.getUTCHours() + tzHour;
56+
const minute = date.getUTCMinutes() + tzMin;
5757
const second = date.getUTCSeconds();
5858

5959
let msec = date.getUTCMilliseconds().toString();
@@ -92,13 +92,5 @@ function getRFC3339TimezoneOffset(date: Date | DDate): string {
9292
if (date instanceof DDate && date.rfc3339TzOffset) {
9393
return date.rfc3339TzOffset;
9494
}
95-
96-
let offset = date.getTimezoneOffset();
97-
if (offset === 0) {
98-
return "Z";
99-
}
100-
101-
const offsetSign = offset > 0 ? "+" : "-";
102-
offset = Math.abs(offset);
103-
return offsetSign + pad(Math.floor(offset / 60)) + ":" + pad(offset % 60);
95+
return "Z";
10496
}

tests/api/date_time.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { MonthlyUsageAttributionResponse } from '../../packages/datadog-api-client-v1/models/MonthlyUsageAttributionResponse';
22
import { ObjectSerializer as ObjectSerializerV1 } from '../../packages/datadog-api-client-v1/models/ObjectSerializer';
3+
import { dateToRFC3339String } from "../../packages/datadog-api-client-common/util";
34

45
test('TestDeserializationOfInvalidDates', () => {
56
const data = `
@@ -42,4 +43,11 @@ test('TestDeserializationOfInvalidDates', () => {
4243
expect(result).toBeInstanceOf(MonthlyUsageAttributionResponse)
4344
expect(result.usage[0].updatedAt).toBeInstanceOf(Date);
4445
}
45-
);
46+
);
47+
48+
test(`Test3339Dates`, () => {
49+
const date = new Date("2023-06-13T21:30:48-10:00");
50+
const result = dateToRFC3339String(date);
51+
expect(result).toBe("2023-06-14T07:30:48Z");
52+
}
53+
);

0 commit comments

Comments
 (0)