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 3 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
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 toOffsetDateTime(date: Date): string;

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

Expand Down
11 changes: 11 additions & 0 deletions sdk/core/core-util/src/datetime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export function toOffsetDateTime(date: Date): string {
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";
17 changes: 17 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,17 @@
// 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("the timezone offset should be correct", function () {
const date = new Date("2022-01-05T19:08:10.000Z");
assert.strictEqual(
Date.parse(toOffsetDateTime(date)) - Date.parse(date.toISOString()),
60 * date.getTimezoneOffset() * 1000
Copy link
Member Author

Choose a reason for hiding this comment

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

A little unsure whether the difference should be 0 ?
if it's 2022-01-05T19:08:10.000Z In UTC time, then it should be 2022-01-06T03:08:10.000+08:00 in the +08:00 timezone. so maybe purely use something like the below in toOffsetDateTime is incorrect?
date.toISOString().replace(/Z$/, "") + diff + pad(tzOffset / 60) + ":" + pad(tzOffset % 60)

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.

I think the difference should be 0 here, no matter the ISO datetime or offset datetime they are just format differences and the underlying number should be the one.

Not sure it's easy to convert them. If not, we may take the offsetDateTime as utcDatetime in typespec.

I notice that we didn't import any library to handle date and time. Not sure this is mainly for performance consideration.

I know with momentjs we could easily format any date (docs).

> moment.defaultFormat
'YYYY-MM-DDTHH:mm:ssZ'
> moment.defaultFormatUtc
'YYYY-MM-DDTHH:mm:ss[Z]'

image

Copy link
Member

Choose a reason for hiding this comment

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

My understanding is that moment is an extremely heavy dependency and something that will eventually be replaced by an ecmascript standard /cc @bterlson

);
});
});
});