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 all commits
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
65 changes: 33 additions & 32 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion sdk/core/core-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@
"sinon": "^15.0.0",
"typescript": "~5.0.0",
"util": "^0.12.1",
"ts-node": "^10.0.0"
"ts-node": "^10.0.0",
"moment": "^2.29.4"
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the dev dependency, so I guess it's okay ?

},
"//metadata": {
"migrationDate": "2023-03-08T18:36:03.000Z"
Expand Down
6 changes: 6 additions & 0 deletions sdk/core/core-util/review/core-util.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,18 @@ export const isWebWorker: boolean;
// @public
export function objectHasProperty<Thing, PropertyName extends string>(thing: Thing, property: PropertyName): thing is Thing & Record<PropertyName, unknown>;

// @public (undocumented)
export type OffsetMillisecondsPrecision = 0 | 1 | 2 | 3;

// @public
export function randomUUID(): string;

// @public
export function stringToUint8Array(value: string, format: EncodingType): Uint8Array;

// @public (undocumented)
export function toOffsetDateTime(date: Date, precision?: OffsetMillisecondsPrecision): string;

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

Expand Down
32 changes: 32 additions & 0 deletions sdk/core/core-util/src/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
export type OffsetMillisecondsPrecision = 0 | 1 | 2 | 3;

export function toOffsetDateTime(date: Date, precision: OffsetMillisecondsPrecision = 0): string {
Copy link
Member Author

Choose a reason for hiding this comment

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

add an option as the precision of milliseconds we want to keep. by default it's 0 now, but let me know if there;s other opppinion.

const tzOffset = -date.getTimezoneOffset();
const diff = tzOffset >= 0 ? "+" : "-";
const pad = (n: number) => `${Math.floor(Math.abs(n))}`.padStart(2, "0");
const precisedMilliseconds = getPrecisedMilliseconds(date, precision);
return (
date.getFullYear() +
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering if this implementation would work if timezone is crossed year for example utc is in 2023, but with offset timezone it may 2022. The same question for crossed month and day.

Copy link
Member

Choose a reason for hiding this comment

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

"-" +
pad(date.getMonth() + 1) +
"-" +
pad(date.getDate()) +
"T" +
pad(date.getHours()) +
":" +
pad(date.getMinutes()) +
":" +
pad(date.getSeconds()) +
(precision !== 0 ? `.${precisedMilliseconds}`.padEnd(precision + 1, "0") : "") +
Copy link
Member Author

@qiaozha qiaozha Sep 14, 2023

Choose a reason for hiding this comment

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

padEnd first parameter is precison + 1 because there's a dot in the string.

diff +
pad(tzOffset / 60) +
":" +
pad(tzOffset % 60)
);
}

function getPrecisedMilliseconds(date: Date, precision: OffsetMillisecondsPrecision): number {
return Math.floor(Math.abs(date.getMilliseconds() / Math.pow(10, 3 - precision)));
}
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, OffsetMillisecondsPrecision } from "./datetime";
Loading
Loading