Path: blob/main/src/resources/library/dayjs/plugins/utc.js
12923 views
import { MILLISECONDS_A_MINUTE, MIN } from "./constant.js";12const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g;3const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g;45function offsetFromString(value = "") {6const offset = value.match(REGEX_VALID_OFFSET_FORMAT);78if (!offset) {9return null;10}1112const [indicator, hoursOffset, minutesOffset] = `${offset[0]}`.match(13REGEX_OFFSET_HOURS_MINUTES_FORMAT14) || ["-", 0, 0];15const totalOffsetInMinutes = +hoursOffset * 60 + +minutesOffset;1617if (totalOffsetInMinutes === 0) {18return 0;19}2021return indicator === "+" ? totalOffsetInMinutes : -totalOffsetInMinutes;22}2324export default (option, Dayjs, dayjs) => {25const proto = Dayjs.prototype;26dayjs.utc = function (date) {27const cfg = { date, utc: true, args: arguments }; // eslint-disable-line prefer-rest-params28return new Dayjs(cfg); // eslint-disable-line no-use-before-define29};3031proto.utc = function (keepLocalTime) {32const ins = dayjs(this.toDate(), { locale: this.$L, utc: true });33if (keepLocalTime) {34return ins.add(this.utcOffset(), MIN);35}36return ins;37};3839proto.local = function () {40return dayjs(this.toDate(), { locale: this.$L, utc: false });41};4243const oldParse = proto.parse;44proto.parse = function (cfg) {45if (cfg.utc) {46this.$u = true;47}48if (!this.$utils().u(cfg.$offset)) {49this.$offset = cfg.$offset;50}51oldParse.call(this, cfg);52};5354const oldInit = proto.init;55proto.init = function () {56if (this.$u) {57const { $d } = this;58this.$y = $d.getUTCFullYear();59this.$M = $d.getUTCMonth();60this.$D = $d.getUTCDate();61this.$W = $d.getUTCDay();62this.$H = $d.getUTCHours();63this.$m = $d.getUTCMinutes();64this.$s = $d.getUTCSeconds();65this.$ms = $d.getUTCMilliseconds();66} else {67oldInit.call(this);68}69};7071const oldUtcOffset = proto.utcOffset;72proto.utcOffset = function (input, keepLocalTime) {73const { u } = this.$utils();74if (u(input)) {75if (this.$u) {76return 0;77}78if (!u(this.$offset)) {79return this.$offset;80}81return oldUtcOffset.call(this);82}83if (typeof input === "string") {84input = offsetFromString(input);85if (input === null) {86return this;87}88}89const offset = Math.abs(input) <= 16 ? input * 60 : input;90let ins = this;91if (keepLocalTime) {92ins.$offset = offset;93ins.$u = input === 0;94return ins;95}96if (input !== 0) {97const localTimezoneOffset = this.$u98? this.toDate().getTimezoneOffset()99: -1 * this.utcOffset();100ins = this.local().add(offset + localTimezoneOffset, MIN);101ins.$offset = offset;102ins.$x.$localOffset = localTimezoneOffset;103} else {104ins = this.utc();105}106return ins;107};108109const oldFormat = proto.format;110const UTC_FORMAT_DEFAULT = "YYYY-MM-DDTHH:mm:ss[Z]";111proto.format = function (formatStr) {112const str = formatStr || (this.$u ? UTC_FORMAT_DEFAULT : "");113return oldFormat.call(this, str);114};115116proto.valueOf = function () {117const addedOffset = !this.$utils().u(this.$offset)118? this.$offset + (this.$x.$localOffset || this.$d.getTimezoneOffset())119: 0;120return this.$d.valueOf() - addedOffset * MILLISECONDS_A_MINUTE;121};122123proto.isUTC = function () {124return !!this.$u;125};126127proto.toISOString = function () {128return this.toDate().toISOString();129};130131proto.toString = function () {132return this.toDate().toUTCString();133};134135const oldToDate = proto.toDate;136proto.toDate = function (type) {137if (type === "s" && this.$offset) {138return dayjs(this.format("YYYY-MM-DD HH:mm:ss:SSS")).toDate();139}140return oldToDate.call(this);141};142const oldDiff = proto.diff;143proto.diff = function (input, units, float) {144if (input && this.$u === input.$u) {145return oldDiff.call(this, input, units, float);146}147const localThis = this.local();148const localInput = dayjs(input).local();149return oldDiff.call(localThis, localInput, units, float);150};151};152153154