-
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 all 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 { | ||
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. 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() + | ||
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 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. 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. Just realize these interfaces use the local timezone |
||
"-" + | ||
pad(date.getMonth() + 1) + | ||
"-" + | ||
pad(date.getDate()) + | ||
"T" + | ||
pad(date.getHours()) + | ||
":" + | ||
pad(date.getMinutes()) + | ||
":" + | ||
pad(date.getSeconds()) + | ||
(precision !== 0 ? `.${precisedMilliseconds}`.padEnd(precision + 1, "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. 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))); | ||
} |
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.
this is the dev dependency, so I guess it's okay ?