Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to-offset-date-time #27087

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ export function randomUUID(): string;
// @public
export function stringToUint8Array(value: string, format: EncodingType): Uint8Array;

// @public (undocumented)
export function toISOStringWithTimezone(date: Date): string;

// @public
export function uint8ArrayToString(bytes: Uint8Array, format: EncodingType): string;

Expand Down
8 changes: 8 additions & 0 deletions sdk/core/core-util/src/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function toOffsetDateTime(date: Date) {
const tzOffset = -date.getTimezoneOffset();
const diff = tzOffset >= 0 ? "+" : "-";
const pad = (n: number) => `${Math.floor(Math.abs(n))}`.padStart(2, "0");
return (
date.toISOString().replace(/Z$/, "") + diff + pad(tzOffset / 60) + ":" + pad(tzOffset % 60)
);
}
1 change: 1 addition & 0 deletions sdk/core/core-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { isDefined, isObjectWithProperties, objectHasProperty } from "./typeGuar
export { randomUUID } from "./uuidUtils";
export { isBrowser, isBun, isNode, isDeno, isReactNative, isWebWorker } from "./checkEnvironment";
export { uint8ArrayToString, stringToUint8Array, EncodingType } from "./bytesEncoding";
export { toOffsetDateTime } from "./datetime";
15 changes: 15 additions & 0 deletions sdk/core/core-util/test/public/datetime.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { toOffsetDateTime } from "../../src/datetime";
import { assert } from "chai";

describe("utcDateTime with timezone offset", function () {
Copy link
Member

@MaryGao MaryGao Sep 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add more cases to cover corner situations e.g +0, -11?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As stated here #27087 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me know if you can figure out a way.


describe("work in +08:00 timezone", function () {
it("converts a base64 string to bytes", function () {
const date = new Date("2022-01-05T19:08:10Z");
assert.strictEqual(toOffsetDateTime(date), "2022-01-05T19:08:10.000+08:00");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@xirzec I don't think the test case is correct for all timezones, but have no idea how to set a specific timezone, so that it works

});
});
});
Loading