You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
UTC time convert cause a wrong time offset
here is the problem, when I use dayjs convert a time that just in the daylight saving time converting period, it will cause the time offset equal to +(X-1):00 (X is the correct offset)
when I set my timezone to Australia/Adelaide, and convert UTC time: '2022-04-02T16:30:00Z'
the root cause of the problem
src/plugin/timezone/index.js
proto.tz=function(timezone=defaultTimezone,keepLocalTime){constoldOffset=this.utcOffset()constdate=this.toDate()consttarget=date.toLocaleString('en-US',{timeZone: timezone})// target within timeoffset cause wrong offset in DST convertingconstdiff=Math.round((date-newDate(target))/1000/60)// New Date(target) is wrongletins=d(target).$set(MS,this.$ms).utcOffset((-Math.round(date.getTimezoneOffset()/15)*15)-diff,true)if(keepLocalTime){constnewOffset=ins.utcOffset()ins=ins.add(oldOffset-newOffset,MIN)}ins.$x.$timezone=timezonereturnins}
Temporary Fix Method
functionisTimeZoneCorrect(dayjsDate,localTimeZone){lettimeZones=[];for(letiof[-2,-1,1,2]){// in order to judge the time, we use two groups of forward/backward time's offset timeZones.push(dayjsDate.add(i,'day').format('YYYY-MM-DD HH:mm:ss'));}timeZones=timeZones.map(function(i){// only use those timeoffset inforeturndayjs.tz(i,localTimeZone).format('Z');});if([...newSet(timeZones)].length>2){// if N > 2, it means there are wrong times surround current time, but current input time must be truereturntrue;}returntimeZones.includes(dayjsDate.format('Z'));// if our time offset in this group, it means correct time offset}functionrepairErrorTimeOffset(dayjsDate){returndayjsDate.utcOffset(dayjsDate.utcOffset()+60,true);}
The text was updated successfully, but these errors were encountered:
UTC time convert cause a wrong time offset
here is the problem, when I use dayjs convert a time that just in the daylight saving time converting period, it will cause the time offset equal to +(X-1):00 (X is the correct offset)
when I set my timezone to Australia/Adelaide, and convert UTC time: '2022-04-02T16:30:00Z'
output: '2022-04-03 00:30:00+07:00'
but Shanghai's timeOffset always stay in +8:00
here is an another case:
output: '2022-04-03 02:00:01+08:30'
correct: '2022-04-03 02:00:01+09:30'
Information
Day.js Version [v1.11.7]
OS: Windows 10 21H2
Time zone: [e.g. GMT+09:30 DST (Adelaide)]
the root cause of the problem
src/plugin/timezone/index.js
Temporary Fix Method
The text was updated successfully, but these errors were encountered: