Skip to content

Commit e02ba37

Browse files
authored
fix:修复系统时区转换逻辑 (#3837)
1 parent 85edcff commit e02ba37

File tree

3 files changed

+49
-12
lines changed

3 files changed

+49
-12
lines changed

packages/renderless/src/picker/index.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*/
1212

13-
import { toDate1, getDateWithNewTimezone, getStrTimezone, getLocalTimezone } from '@opentiny/utils'
13+
import { toDate1, getDateWithNewTimezone, getStrTimezone, getLocalTimezone, paeseIso8601 } from '@opentiny/utils'
1414
import { isNumber, isDate } from '@opentiny/utils'
1515
import { userPopper } from '@opentiny/vue-hooks'
1616
import { DATEPICKER } from '@opentiny/utils'
@@ -172,6 +172,28 @@ export const parseAsFormatAndType =
172172
return parser(value, format, rangeSeparator)
173173
}
174174

175+
const ignoreTimezone = (value) => {
176+
let res = value
177+
178+
const ignoreTimezoneFn = (value) => {
179+
let date = value
180+
const iso8601 = paeseIso8601(value)
181+
if (iso8601) {
182+
const { year, month, day, hours, minutes, seconds } = iso8601
183+
date = new Date(year, month, day, hours, minutes, seconds)
184+
}
185+
return date
186+
}
187+
188+
if (Array.isArray(res)) {
189+
res = res.map((item) => ignoreTimezoneFn(item))
190+
} else {
191+
res = ignoreTimezoneFn(res)
192+
}
193+
194+
return res
195+
}
196+
175197
export const parsedValue =
176198
({ api, props, state, t }) =>
177199
() => {
@@ -191,18 +213,18 @@ export const parsedValue =
191213
return props.modelValue
192214
}
193215

216+
let value = ignoreTimezone(props.modelValue)
217+
194218
if (state.valueFormat) {
195-
let date = props.modelValue
219+
let date = value
196220

197-
if (isServiceTimezone) {
198-
if (Array.isArray(date)) {
199-
date = [].concat(date).map((item) => {
200-
return isDate(item) ? formatDate(item, state.valueFormat, t) : item
201-
})
202-
} else {
203-
if (state.valueFormat !== DATEPICKER.TimesTamp) {
204-
date = formatDate(date, state.valueFormat, t)
205-
}
221+
if (Array.isArray(date)) {
222+
date = [].concat(date).map((item) => {
223+
return isDate(item) ? formatDate(item, state.valueFormat, t) : item
224+
})
225+
} else {
226+
if (state.valueFormat !== DATEPICKER.TimesTamp && isDate(date)) {
227+
date = formatDate(date, state.valueFormat, t)
206228
}
207229
}
208230
const result = api.parseAsFormatAndType(date, state.valueFormat, state.type, props.rangeSeparator)

packages/utils/src/date/index.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,20 @@ const timezone2 = '-00:00,+00:00,+01:00,+02:00,+03:00,+03:30,+04:00,+04:30,+05:0
5656
const timezone3 = '+06:30,+07:00,+08:00,+09:00,+10:00,+10:30,+11:00,+11:30,+12:00,+12:45,+13:00,+14:00'
5757
const timezones: string[] = [...timezone1.split(','), ...timezone2.split(','), ...timezone3.split(',')]
5858

59+
export const paeseIso8601 = (str: string) => {
60+
const m = iso8601Reg.exec(str)
61+
if (m && m.length === 25) {
62+
const year = Number(m[1])
63+
const month = Number(m[2]) - 1
64+
const day = Number(m[6])
65+
const hours = m[12] || 0
66+
const minutes = m[14] || 0
67+
const seconds = m[17] || 0
68+
return { year, month, day, hours, minutes, seconds }
69+
}
70+
return undefined
71+
}
72+
5973
const getTimezone = (date) => {
6074
const timezoneoffset = 0 - date.getTimezoneOffset() / 60
6175
let timezone

packages/utils/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export {
1818
toDateStr,
1919
getWeekOfFirstDay,
2020
getLocalTimezone,
21-
getStrTimezone
21+
getStrTimezone,
22+
paeseIso8601
2223
} from './date'
2324

2425
// 与 date.ts 合并一下, 有几个重名变量,待整理, 如果功能一致就合并

0 commit comments

Comments
 (0)