-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Add UTC mode with UTC plugin (#517)
- Loading branch information
Showing
11 changed files
with
394 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
export default (option, Dayjs, dayjs) => { | ||
const proto = Dayjs.prototype | ||
dayjs.utc = function (date, format) { | ||
const cfg = { date, utc: true, format } | ||
return new Dayjs(cfg) // eslint-disable-line no-use-before-define | ||
} | ||
|
||
proto.utc = function () { | ||
return dayjs(this.toDate(), { locale: this.$L, utc: true }) | ||
} | ||
|
||
proto.local = function () { | ||
return dayjs(this.toDate(), { locale: this.$L, utc: false }) | ||
} | ||
|
||
const oldParse = proto.parse | ||
proto.parse = function (cfg) { | ||
if (cfg.utc) { | ||
this.$u = true | ||
} | ||
oldParse.call(this, cfg) | ||
} | ||
|
||
const oldInit = proto.init | ||
proto.init = function () { | ||
if (this.$u) { | ||
const { $d } = this | ||
this.$y = $d.getUTCFullYear() | ||
this.$M = $d.getUTCMonth() | ||
this.$D = $d.getUTCDate() | ||
this.$W = $d.getUTCDay() | ||
this.$H = $d.getUTCHours() | ||
this.$m = $d.getUTCMinutes() | ||
this.$s = $d.getUTCSeconds() | ||
this.$ms = $d.getUTCMilliseconds() | ||
} else { | ||
oldInit.call(this) | ||
} | ||
} | ||
|
||
const oldUtcOffset = proto.utcOffset | ||
proto.utcOffset = function () { | ||
if (this.$u) { | ||
return 0 | ||
} | ||
return oldUtcOffset.call(this) | ||
} | ||
|
||
const oldFormat = proto.format | ||
const UTC_FORMAT_DEFAULT = 'YYYY-MM-DDTHH:mm:ss[Z]' | ||
proto.format = function (formatStr) { | ||
const str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : '') | ||
return oldFormat.call(this, str) | ||
} | ||
|
||
proto.isUTC = function () { | ||
return !!this.$u | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.