@@ -40,10 +40,6 @@ export class ParsingComponents implements ParsedComponents {
40
40
return null ;
41
41
}
42
42
43
- date ( ) : Date {
44
- return this . dayjs ( ) . toDate ( ) ;
45
- }
46
-
47
43
isCertain ( component : Component ) : boolean {
48
44
return component in this . knownValues ;
49
45
}
@@ -104,49 +100,54 @@ export class ParsingComponents implements ParsedComponents {
104
100
}
105
101
106
102
isValidDate ( ) : boolean {
107
- let dateMoment = this . dayjs ( ) ;
108
- if ( this . isCertain ( "timezoneOffset" ) ) {
109
- const adjustTimezoneOffset = this . get ( "timezoneOffset" ) - dateMoment . utcOffset ( ) ;
110
- dateMoment = dateMoment . add ( adjustTimezoneOffset , "minute" ) ;
111
- }
103
+ const date = this . isCertain ( "timezoneOffset" ) ? this . dateWithoutTimezoneAdjustment ( ) : this . date ( ) ;
112
104
113
- if ( dateMoment . get ( "year" ) != this . get ( "year" ) ) return false ;
114
- if ( dateMoment . get ( "month" ) != this . get ( "month" ) - 1 ) return false ;
115
- if ( dateMoment . get ( "date" ) != this . get ( "day" ) ) return false ;
116
- if ( this . get ( "hour" ) != null && dateMoment . get ( "hour" ) != this . get ( "hour" ) ) return false ;
117
- if ( this . get ( "minute" ) != null && dateMoment . get ( "minute" ) != this . get ( "minute" ) ) return false ;
105
+ if ( date . getFullYear ( ) != = this . get ( "year" ) ) return false ;
106
+ if ( date . getMonth ( ) != = this . get ( "month" ) - 1 ) return false ;
107
+ if ( date . getDate ( ) != = this . get ( "day" ) ) return false ;
108
+ if ( this . get ( "hour" ) != null && date . getHours ( ) != this . get ( "hour" ) ) return false ;
109
+ if ( this . get ( "minute" ) != null && date . getMinutes ( ) != this . get ( "minute" ) ) return false ;
118
110
119
111
return true ;
120
112
}
121
113
122
- dayjs ( ) {
123
- let result = dayjs ( ) ;
124
-
125
- result = result . year ( this . get ( "year" ) ) ;
126
- result = result . month ( this . get ( "month" ) - 1 ) ;
127
- result = result . date ( this . get ( "day" ) ) ;
128
- result = result . hour ( this . get ( "hour" ) ) ;
129
- result = result . minute ( this . get ( "minute" ) ) ;
130
- result = result . second ( this . get ( "second" ) ) ;
131
- result = result . millisecond ( this . get ( "millisecond" ) ) ;
132
-
133
- // Javascript Date Object return minus timezone offset
134
- const currentTimezoneOffset = result . utcOffset ( ) ;
135
- const targetTimezoneOffset =
136
- this . get ( "timezoneOffset" ) !== null ? this . get ( "timezoneOffset" ) : currentTimezoneOffset ;
137
-
138
- const adjustTimezoneOffset = targetTimezoneOffset - currentTimezoneOffset ;
139
- result = result . add ( - adjustTimezoneOffset , "minute" ) ;
140
-
141
- return result ;
142
- }
143
-
144
114
toString ( ) {
145
115
return `[ParsingComponents {knownValues: ${ JSON . stringify ( this . knownValues ) } , impliedValues: ${ JSON . stringify (
146
116
this . impliedValues
147
117
) } }]`;
148
118
}
149
119
120
+ dayjs ( ) {
121
+ return dayjs ( this . date ( ) ) ;
122
+ }
123
+
124
+ date ( ) : Date {
125
+ const date = this . dateWithoutTimezoneAdjustment ( ) ;
126
+ return new Date ( date . getTime ( ) + this . getTimezoneAdjustmentMinute ( date ) * 60000 ) ;
127
+ }
128
+
129
+ private dateWithoutTimezoneAdjustment ( ) {
130
+ const date = new Date (
131
+ this . get ( "year" ) ,
132
+ this . get ( "month" ) - 1 ,
133
+ this . get ( "day" ) ,
134
+ this . get ( "hour" ) ,
135
+ this . get ( "minute" ) ,
136
+ this . get ( "second" ) ,
137
+ this . get ( "millisecond" )
138
+ ) ;
139
+
140
+ date . setFullYear ( this . get ( "year" ) ) ;
141
+ return date ;
142
+ }
143
+
144
+ private getTimezoneAdjustmentMinute ( date ?: Date ) {
145
+ date = date ?? new Date ( ) ;
146
+ const currentTimezoneOffset = - date . getTimezoneOffset ( ) ;
147
+ const targetTimezoneOffset = this . get ( "timezoneOffset" ) ?? currentTimezoneOffset ;
148
+ return currentTimezoneOffset - targetTimezoneOffset ;
149
+ }
150
+
150
151
static createRelativeFromRefDate (
151
152
refDate : Date ,
152
153
fragments : { [ c in OpUnitType | QUnitType ] ?: number }
0 commit comments