-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
to-offset-date-time #27087
Changes from 3 commits
4c31be5
5423738
48c658b
4a972a9
97cc99b
dbcd6c6
55722df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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) | ||
); | ||
} |
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 () { | ||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A little unsure whether the difference should be 0 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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).
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.