@@ -74,15 +74,18 @@ export class Date {
74
74
}
75
75
76
76
getUTCFullYear ( ) : i32 {
77
- return ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) . year ;
77
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
78
+ return year ;
78
79
}
79
80
80
81
getUTCMonth ( ) : i32 {
81
- return ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) . month - 1 ;
82
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
83
+ return month - 1 ;
82
84
}
83
85
84
86
getUTCDate ( ) : i32 {
85
- return ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) . day ;
87
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
88
+ return day ;
86
89
}
87
90
88
91
getUTCHours ( ) : i32 {
@@ -121,43 +124,42 @@ export class Date {
121
124
}
122
125
123
126
setUTCDate ( value : i32 ) : void {
124
- const ymd = ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
125
- throwIfNotInRange ( value , 1 , lastDayOfMonth ( ymd . year , ymd . month ) ) ;
127
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
128
+ throwIfNotInRange ( value , 1 , lastDayOfMonth ( year , month ) ) ;
126
129
const mills = this . epochMillis % MILLIS_PER_DAY ;
127
130
this . epochMillis =
128
- i64 ( daysSinceEpoch ( ymd . year , ymd . month , value ) ) * MILLIS_PER_DAY + mills ;
131
+ i64 ( daysSinceEpoch ( year , month , value ) ) * MILLIS_PER_DAY + mills ;
129
132
}
130
133
131
134
setUTCMonth ( value : i32 ) : void {
132
135
throwIfNotInRange ( value , 1 , 12 ) ;
133
- const ymd = ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
136
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
134
137
const mills = this . epochMillis % MILLIS_PER_DAY ;
135
138
this . epochMillis =
136
- i64 ( daysSinceEpoch ( ymd . year , value + 1 , ymd . day ) ) * MILLIS_PER_DAY +
137
- mills ;
139
+ i64 ( daysSinceEpoch ( year , value + 1 , day ) ) * MILLIS_PER_DAY + mills ;
138
140
}
139
141
140
142
setUTCFullYear ( value : i32 ) : void {
141
- const ymd = ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
143
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
142
144
const mills = this . epochMillis % MILLIS_PER_DAY ;
143
145
this . epochMillis =
144
- i64 ( daysSinceEpoch ( value , ymd . month , ymd . day ) ) * MILLIS_PER_DAY + mills ;
146
+ i64 ( daysSinceEpoch ( value , month , day ) ) * MILLIS_PER_DAY + mills ;
145
147
}
146
148
147
149
toISOString ( ) : string {
148
- const ymd = ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
150
+ ymdFromEpochDays ( i32 ( this . epochMillis / MILLIS_PER_DAY ) ) ;
149
151
150
- let yearStr = ymd . year . toString ( ) ;
152
+ let yearStr = year . toString ( ) ;
151
153
if ( yearStr . length > 4 ) {
152
154
yearStr = "+" + yearStr . padStart ( 6 , "0" ) ;
153
155
}
154
156
155
157
return (
156
158
yearStr +
157
159
"-" +
158
- ymd . month . toString ( ) . padStart ( 2 , "0" ) +
160
+ month . toString ( ) . padStart ( 2 , "0" ) +
159
161
"-" +
160
- ymd . day . toString ( ) . padStart ( 2 , "0" ) +
162
+ day . toString ( ) . padStart ( 2 , "0" ) +
161
163
"T" +
162
164
this . getUTCHours ( ) . toString ( ) . padStart ( 2 , "0" ) +
163
165
":" +
@@ -198,12 +200,6 @@ const MILLIS_PER_HOUR = 1_000 * 60 * 60;
198
200
const MILLIS_PER_MINUTE = 1_000 * 60 ;
199
201
const MILLIS_PER_SECOND = 1_000 ;
200
202
201
- class YMD {
202
- year : i32 ;
203
- month : i32 ;
204
- day : i32 ;
205
- }
206
-
207
203
// http://howardhinnant.github.io/date_algorithms.html#is_leap
208
204
function isLeap ( y : i32 ) : bool {
209
205
return y % 4 == 0 && ( y % 100 != 0 || y % 400 == 0 ) ;
@@ -220,22 +216,20 @@ function lastDayOfMonth(y: i32, m: i32): i32 {
220
216
return m != 2 || ! isLeap ( y ) ? lastDayOfMonthNonLeapYear ( m ) : 29 ;
221
217
}
222
218
219
+ // ymdFromEpochDays returns values via globals to avoid allocations
220
+ let year : i32 , month : i32 , day : i32 ;
223
221
// see: http://howardhinnant.github.io/date_algorithms.html#civil_from_days
224
- function ymdFromEpochDays ( z : i32 ) : YMD {
222
+ function ymdFromEpochDays ( z : i32 ) : void {
225
223
z += 719468 ;
226
224
const era = ( z >= 0 ? z : z - 146096 ) / 146097 ;
227
225
const doe = z - era * 146097 ; // [0, 146096]
228
226
const yoe = ( doe - doe / 1460 + doe / 36524 - doe / 146096 ) / 365 ; // [0, 399]
229
- const y = yoe + era * 400 ;
227
+ year = yoe + era * 400 ;
230
228
const doy = doe - ( 365 * yoe + yoe / 4 - yoe / 100 ) ; // [0, 365]
231
229
const mp = ( 5 * doy + 2 ) / 153 ; // [0, 11]
232
- const d = doy - ( 153 * mp + 2 ) / 5 + 1 ; // [1, 31]
233
- const m = mp + ( mp < 10 ? 3 : - 9 ) ; // [1, 12]
234
- return {
235
- year : y + ( m <= 2 ? 1 : 0 ) ,
236
- month : m ,
237
- day : d ,
238
- } ;
230
+ day = doy - ( 153 * mp + 2 ) / 5 + 1 ; // [1, 31]
231
+ month = mp + ( mp < 10 ? 3 : - 9 ) ; // [1, 12]
232
+ year += ( month <= 2 ? 1 : 0 ) ;
239
233
}
240
234
241
235
// http://howardhinnant.github.io/date_algorithms.html#days_from_civil
0 commit comments