11import { dateFormat } from '@/date' ;
2- import { TzDate } from '@/date' ;
2+ import { TimezoneDate } from '@/date' ;
33
4- const gmtOrder = TzDate . getOrder ( ) ;
5- console . log ( `当前时区 GMT${ gmtOrder > 0 ? '+' : '' } ${ gmtOrder } ` ) ;
4+ const utcOrder = TimezoneDate . getUTCOffset ( ) ;
5+ console . log ( `当前时区 GMT${ utcOrder > 0 ? '+' : '' } ${ utcOrder } ` ) ;
66
77describe ( '0 时区' , ( ) => {
8- const offset = TzDate . getOffset ( 0 ) ;
8+ const utcOffset = 0 ;
99
1010 it ( '空入参' , ( ) => {
1111 // 时间戳与时区无关
12- const tzNow = new TzDate ( { offset } ) . getTime ( ) ;
12+ const tzNow = new TimezoneDate ( { utcOffset } ) . getTime ( ) ;
1313 const now = new Date ( ) . getTime ( ) ;
1414 expect ( tzNow ) . toBeLessThanOrEqual ( now ) ;
1515 // 误差小于 10ms
@@ -19,18 +19,18 @@ describe('0 时区', () => {
1919 it ( '时间戳' , ( ) => {
2020 // 时间戳与时区无关
2121 const now = Date . now ( ) ;
22- const td = new TzDate ( { offset , timestamp : now } ) ;
22+ const td = new TimezoneDate ( { utcOffset , timestamp : now } ) ;
2323 const dt = new Date ( now ) ;
2424
2525 expect ( td . getTime ( ) ) . toBe ( dt . getTime ( ) ) ;
26- expect ( td . getTimezoneOrder ( ) ) . toBe ( 0 ) ;
26+ expect ( td . getUTCOffset ( ) ) . toBe ( 0 ) ;
2727 } ) ;
2828
2929 it ( '年月日' , ( ) => {
3030 const localOffset = new Date ( ) . getTimezoneOffset ( ) * 60 * 1000 ;
3131 const targetOffset = 0 ;
3232 const value = [ 2025 , 5 , 3 , 12 , 34 , 56 , 789 ] as const ;
33- const td = new TzDate ( { offset , value } ) ;
33+ const td = new TimezoneDate ( { utcOffset , value } ) ;
3434
3535 expect ( td . toISOString ( ) ) . toEqual ( '2025-06-03T12:34:56.789Z' ) ;
3636
@@ -79,11 +79,11 @@ describe('0 时区', () => {
7979} ) ;
8080
8181describe ( '东 8 时区' , ( ) => {
82- const offset = TzDate . getOffset ( 8 ) ;
82+ const utcOffset = 8 ;
8383
8484 it ( '空入参' , ( ) => {
8585 // 时间戳与时区无关
86- const tzNow = new TzDate ( { offset } ) . getTime ( ) ;
86+ const tzNow = new TimezoneDate ( { utcOffset } ) . getTime ( ) ;
8787 const now = new Date ( ) . getTime ( ) ;
8888 expect ( tzNow ) . toBeLessThanOrEqual ( now ) ;
8989 // 误差小于 10ms
@@ -93,18 +93,18 @@ describe('东 8 时区', () => {
9393 it ( '时间戳' , ( ) => {
9494 // 时间戳与时区无关
9595 const now = Date . now ( ) ;
96- const td = new TzDate ( { offset , timestamp : now } ) ;
96+ const td = new TimezoneDate ( { utcOffset , timestamp : now } ) ;
9797 const dt = new Date ( now ) ;
9898
9999 expect ( td . getTime ( ) ) . toBe ( dt . getTime ( ) ) ;
100- expect ( td . getTimezoneOrder ( ) ) . toBe ( 8 ) ;
100+ expect ( td . getUTCOffset ( ) ) . toBe ( 8 ) ;
101101 } ) ;
102102
103103 it ( '年月日' , ( ) => {
104104 const localOffset = new Date ( ) . getTimezoneOffset ( ) * 60 * 1000 ;
105- const targetOffset = offset * 60 * 1000 ;
105+ const targetOffset = TimezoneDate . getTimezoneOffset ( utcOffset ) * 60 * 1000 ;
106106 const value = [ 2025 , 5 , 3 , 12 , 34 , 56 , 789 ] as const ;
107- const td = new TzDate ( { offset , value : [ ... value ] } ) ;
107+ const td = new TimezoneDate ( { utcOffset , value } ) ;
108108
109109 expect ( td . toISOString ( ) ) . toEqual ( '2025-06-03T04:34:56.789Z' ) ;
110110
@@ -124,101 +124,88 @@ describe('东 8 时区', () => {
124124
125125 td . setFullYear ( 2024 ) ;
126126 expect ( td . getFullYear ( ) ) . toBe ( 2024 ) ;
127- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
127+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
128128
129129 td . setMonth ( 5 ) ;
130130 expect ( td . getMonth ( ) ) . toBe ( 5 ) ;
131- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
131+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
132132
133133 td . setDate ( 10 ) ;
134134 expect ( td . getDate ( ) ) . toBe ( 10 ) ;
135- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
135+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
136136
137137 td . setHours ( 12 ) ;
138138 expect ( td . getHours ( ) ) . toBe ( 12 ) ;
139- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
139+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
140140
141141 td . setMinutes ( 30 ) ;
142142 expect ( td . getMinutes ( ) ) . toBe ( 30 ) ;
143- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
143+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
144144
145145 td . setSeconds ( 45 ) ;
146146 expect ( td . getSeconds ( ) ) . toBe ( 45 ) ;
147- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
147+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
148148
149149 td . setMilliseconds ( 100 ) ;
150150 expect ( td . getMilliseconds ( ) ) . toBe ( 100 ) ;
151- expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , offset ) ) ;
151+ expect ( td . getTime ( ) ) . toEqual ( getUtcTimestamp ( td , utcOffset ) ) ;
152152 } ) ;
153153} ) ;
154154
155155describe ( '时区转换' , ( ) => {
156156 it ( '时间戳' , ( ) => {
157157 const timestamp = Date . now ( ) ;
158- const gmt8Date = new TzDate ( {
159- offset : TzDate . getOffset ( 8 ) ,
158+ const utc8Date = new TimezoneDate ( {
159+ utcOffset : 8 ,
160160 timestamp,
161161 } ) ;
162- const gmt0Date = TzDate . from ( gmt8Date , 0 ) ;
162+ const utc0Date = TimezoneDate . changeUtcOffset ( utc8Date , 0 ) ;
163163
164- console . log ( 'gmt8 :' , dateFormat ( gmt8Date ) ) ;
165- console . log ( 'gmt0 :' , dateFormat ( gmt0Date ) ) ;
164+ console . log ( 'utc8 :' , dateFormat ( utc8Date ) ) ;
165+ console . log ( 'utc0 :' , dateFormat ( utc0Date ) ) ;
166166
167- let gmt8Hours = gmt8Date . getHours ( ) ;
168- const gmt0Hours = gmt0Date . getHours ( ) ;
167+ let utc8Hours = utc8Date . getHours ( ) ;
168+ const utc0Hours = utc0Date . getHours ( ) ;
169169
170170 // 不是同一天
171- if ( gmt8Date . getDate ( ) !== gmt0Date . getDate ( ) ) {
172- gmt8Hours += 24 ;
171+ if ( utc8Date . getDate ( ) !== utc0Date . getDate ( ) ) {
172+ utc8Hours += 24 ;
173173 }
174174
175- expect ( gmt8Hours - gmt0Hours ) . toBe ( 8 ) ;
176- expect ( gmt0Date . getTime ( ) ) . toBe ( gmt0Date . getTime ( ) ) ;
175+ expect ( utc8Hours - utc0Hours ) . toBe ( 8 ) ;
176+ expect ( utc0Date . getTime ( ) ) . toBe ( utc0Date . getTime ( ) ) ;
177177 } ) ;
178178
179179 it ( '日期' , ( ) => {
180180 const value = [ 2024 , 5 , 10 , 12 , 30 , 45 , 100 ] as const ;
181- const gmt8Td = new TzDate ( {
182- offset : TzDate . getOffset ( 8 ) ,
181+ const utc8Td = new TimezoneDate ( {
182+ utcOffset : 8 ,
183183 value,
184184 } ) ;
185- const gmt0Td = TzDate . from ( gmt8Td , 0 ) ;
185+ const utc0Td = TimezoneDate . changeUtcOffset ( utc8Td , 0 ) ;
186186
187- expect ( gmt8Td . getHours ( ) ) . toBe ( 12 ) ;
188- expect ( gmt0Td . getHours ( ) ) . toBe ( 4 ) ;
189- expect ( gmt0Td . getTime ( ) ) . toBe ( gmt0Td . getTime ( ) ) ;
187+ expect ( utc8Td . getHours ( ) ) . toBe ( 12 ) ;
188+ expect ( utc0Td . getHours ( ) ) . toBe ( 4 ) ;
189+ expect ( utc0Td . getTime ( ) ) . toBe ( utc0Td . getTime ( ) ) ;
190190 } ) ;
191191} ) ;
192192
193193describe ( '时间转换' , ( ) => {
194- it ( 'from 时间转换后' , ( ) => {
195- const td1 = new TzDate ( {
196- offset : TzDate . getOffset ( 8 ) ,
197- value : [ 2024 , 5 , 10 , 12 , 30 , 45 , 100 ] as const ,
198- } ) ;
199-
200- td1 . setDate ( td1 . getDate ( ) - 1 ) ;
201- expect ( td1 . getDate ( ) ) . toBe ( 9 ) ;
202-
203- const td2 = TzDate . from ( td1 , td1 . getTimezoneOffset ( ) ) ;
204- expect ( td2 . getDate ( ) ) . toBe ( 9 ) ;
205- } ) ;
206-
207194 it ( 'new 时间转换后' , ( ) => {
208- const td1 = new TzDate ( {
209- offset : TzDate . getOffset ( 8 ) ,
195+ const td1 = new TimezoneDate ( {
196+ utcOffset : 8 ,
210197 value : [ 2024 , 5 , 10 , 12 , 30 , 45 , 100 ] as const ,
211198 } ) ;
212199
213200 td1 . setDate ( td1 . getDate ( ) - 1 ) ;
214201 expect ( td1 . getDate ( ) ) . toBe ( 9 ) ;
215202
216- const td2 = new TzDate ( td1 ) ;
203+ const td2 = new TimezoneDate ( td1 ) ;
217204 expect ( td2 . getDate ( ) ) . toBe ( 9 ) ;
218205 } ) ;
219206} ) ;
220207
221- function getUtcTimestamp ( td : TzDate , offset = 0 ) {
208+ function getUtcTimestamp ( td : TimezoneDate , utcOffset = 0 ) {
222209 return (
223210 Date . UTC (
224211 td . getFullYear ( ) ,
@@ -229,6 +216,6 @@ function getUtcTimestamp(td: TzDate, offset = 0) {
229216 td . getSeconds ( ) ,
230217 td . getMilliseconds ( ) ,
231218 ) +
232- offset * 60 * 1000
219+ TimezoneDate . getTimezoneOffset ( utcOffset ) * 60 * 1000
233220 ) ;
234221}
0 commit comments