-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
index.js
149 lines (130 loc) · 3.96 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import { MILLISECONDS_A_MINUTE, MIN } from '../../constant'
const REGEX_VALID_OFFSET_FORMAT = /[+-]\d\d(?::?\d\d)?/g
const REGEX_OFFSET_HOURS_MINUTES_FORMAT = /([+-]|\d\d)/g
function offsetFromString(value = '') {
const offset = value.match(REGEX_VALID_OFFSET_FORMAT)
if (!offset) {
return null
}
const [indicator, hoursOffset, minutesOffset] = `${offset[0]}`.match(REGEX_OFFSET_HOURS_MINUTES_FORMAT) || ['-', 0, 0]
const totalOffsetInMinutes = (+hoursOffset * 60) + (+minutesOffset)
if (totalOffsetInMinutes === 0) {
return 0
}
return indicator === '+' ? totalOffsetInMinutes : -totalOffsetInMinutes
}
export default (option, Dayjs, dayjs) => {
const proto = Dayjs.prototype
dayjs.utc = function (date) {
const cfg = { date, utc: true, args: arguments } // eslint-disable-line prefer-rest-params
return new Dayjs(cfg) // eslint-disable-line no-use-before-define
}
proto.utc = function (keepLocalTime) {
const ins = dayjs(this.toDate(), { locale: this.$L, utc: true })
if (keepLocalTime) {
return ins.add(this.utcOffset(), MIN)
}
return ins
}
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
}
if (!this.$utils().u(cfg.$offset)) {
this.$offset = cfg.$offset
}
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 (input, keepLocalTime) {
const { u } = this.$utils()
if (u(input)) {
if (this.$u) {
return 0
}
if (!u(this.$offset)) {
return this.$offset
}
return oldUtcOffset.call(this)
}
if (typeof input === 'string') {
input = offsetFromString(input)
if (input === null) {
return this
}
}
const offset = Math.abs(input) <= 16 ? input * 60 : input
let ins = this
if (keepLocalTime) {
ins.$offset = offset
ins.$u = input === 0
return ins
}
if (input !== 0) {
const localTimezoneOffset = this.$u
? this.toDate().getTimezoneOffset() : -1 * this.utcOffset()
ins = this.local().add(offset + localTimezoneOffset, MIN)
ins.$offset = offset
ins.$x.$localOffset = localTimezoneOffset
} else {
ins = this.utc()
}
return ins
}
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.valueOf = function () {
const addedOffset = !this.$utils().u(this.$offset)
? this.$offset + (this.$x.$localOffset || (new Date()).getTimezoneOffset()) : 0
return this.$d.valueOf() - (addedOffset * MILLISECONDS_A_MINUTE)
}
proto.isUTC = function () {
return !!this.$u
}
proto.toISOString = function () {
return this.toDate().toISOString()
}
proto.toString = function () {
return this.toDate().toUTCString()
}
const oldToDate = proto.toDate
proto.toDate = function (type) {
if (type === 's' && this.$offset) {
return dayjs(this.format('YYYY-MM-DD HH:mm:ss:SSS')).toDate()
}
return oldToDate.call(this)
}
const oldDiff = proto.diff
proto.diff = function (input, units, float) {
if (input && this.$u === input.$u) {
return oldDiff.call(this, input, units, float)
}
const localThis = this.local()
const localInput = dayjs(input).local()
return oldDiff.call(localThis, localInput, units, float)
}
}