Fixes timeZone() by no longer needing timeZone() (#646)
* Fixes timeZone() by no longer needing timeZone() * Added simple testcase to demonstrate the behaviour of parseZone() vs dayjs() * Fixed eslint errors
This commit is contained in:
1 parent
eb25ef266a
commit
d3fa6cec80
6 files changed
+69
-28
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
const ISO8601_OFFSET_FORMAT = /^(.*)([+-])(\d{2}):(\d{2})|(Z)$/;
|
||||
|
||||
// @see https://github.com/iamkun/dayjs/issues/651#issuecomment-763033265
|
||||
// decorates dayjs in order to keep the utcOffset of the given date string
|
||||
// ; natively dayjs auto-converts to local time & losing utcOffset info.
|
||||
export function parseZone(
|
||||
date?: dayjs.ConfigType,
|
||||
format?: dayjs.OptionType,
|
||||
locale?: string,
|
||||
strict?: boolean
|
||||
) {
|
||||
if (typeof date !== "string") {
|
||||
return dayjs(date, format, locale, strict);
|
||||
}
|
||||
const match = date.match(ISO8601_OFFSET_FORMAT);
|
||||
if (match === null) {
|
||||
return;
|
||||
}
|
||||
if (match[0] === "Z") {
|
||||
return dayjs(
|
||||
date,
|
||||
{
|
||||
utc: true,
|
||||
...format,
|
||||
},
|
||||
locale,
|
||||
strict
|
||||
);
|
||||
}
|
||||
const [, dateTime, sign, tzHour, tzMinute] = match;
|
||||
const uOffset: number = tzHour * 60 + parseInt(tzMinute, 10);
|
||||
const offset = sign === "+" ? uOffset : -uOffset;
|
||||
|
||||
return dayjs(
|
||||
dateTime,
|
||||
{
|
||||
$offset: offset,
|
||||
...format,
|
||||
} as dayjs.OptionType,
|
||||
locale,
|
||||
strict
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user