18
18
*/
19
19
20
20
import { int , isInt } from '../integer'
21
- import { Date , LocalDateTime , LocalTime } from '../temporal-types'
22
21
import { assertNumberOrInteger } from './util'
23
22
import { newError } from '../error'
24
23
@@ -57,24 +56,24 @@ class ValueRange {
57
56
}
58
57
}
59
58
60
- const YEAR_RANGE = new ValueRange ( - 999999999 , 999999999 )
61
- const MONTH_OF_YEAR_RANGE = new ValueRange ( 1 , 12 )
62
- const DAY_OF_MONTH_RANGE = new ValueRange ( 1 , 31 )
63
- const HOUR_OF_DAY_RANGE = new ValueRange ( 0 , 23 )
64
- const MINUTE_OF_HOUR_RANGE = new ValueRange ( 0 , 59 )
65
- const SECOND_OF_MINUTE_RANGE = new ValueRange ( 0 , 59 )
66
- const NANOSECOND_OF_SECOND_RANGE = new ValueRange ( 0 , 999999999 )
67
-
68
- const MINUTES_PER_HOUR = 60
69
- const SECONDS_PER_MINUTE = 60
70
- const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
71
- const NANOS_PER_SECOND = 1000000000
72
- const NANOS_PER_MILLISECOND = 1000000
73
- const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE
74
- const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR
75
- const DAYS_0000_TO_1970 = 719528
76
- const DAYS_PER_400_YEAR_CYCLE = 146097
77
- const SECONDS_PER_DAY = 86400
59
+ export const YEAR_RANGE = new ValueRange ( - 999999999 , 999999999 )
60
+ export const MONTH_OF_YEAR_RANGE = new ValueRange ( 1 , 12 )
61
+ export const DAY_OF_MONTH_RANGE = new ValueRange ( 1 , 31 )
62
+ export const HOUR_OF_DAY_RANGE = new ValueRange ( 0 , 23 )
63
+ export const MINUTE_OF_HOUR_RANGE = new ValueRange ( 0 , 59 )
64
+ export const SECOND_OF_MINUTE_RANGE = new ValueRange ( 0 , 59 )
65
+ export const NANOSECOND_OF_SECOND_RANGE = new ValueRange ( 0 , 999999999 )
66
+
67
+ export const MINUTES_PER_HOUR = 60
68
+ export const SECONDS_PER_MINUTE = 60
69
+ export const SECONDS_PER_HOUR = SECONDS_PER_MINUTE * MINUTES_PER_HOUR
70
+ export const NANOS_PER_SECOND = 1000000000
71
+ export const NANOS_PER_MILLISECOND = 1000000
72
+ export const NANOS_PER_MINUTE = NANOS_PER_SECOND * SECONDS_PER_MINUTE
73
+ export const NANOS_PER_HOUR = NANOS_PER_MINUTE * MINUTES_PER_HOUR
74
+ export const DAYS_0000_TO_1970 = 719528
75
+ export const DAYS_PER_400_YEAR_CYCLE = 146097
76
+ export const SECONDS_PER_DAY = 86400
78
77
79
78
export function normalizeSecondsForDuration ( seconds , nanoseconds ) {
80
79
return int ( seconds ) . add ( floorDiv ( nanoseconds , NANOS_PER_SECOND ) )
@@ -104,26 +103,6 @@ export function localTimeToNanoOfDay (hour, minute, second, nanosecond) {
104
103
return totalNanos . add ( nanosecond )
105
104
}
106
105
107
- /**
108
- * Converts nanoseconds of the day into local time.
109
- * @param {Integer|number|string } nanoOfDay the nanoseconds of the day to convert.
110
- * @return {LocalTime } the local time representing given nanoseconds of the day.
111
- */
112
- export function nanoOfDayToLocalTime ( nanoOfDay ) {
113
- nanoOfDay = int ( nanoOfDay )
114
-
115
- const hour = nanoOfDay . div ( NANOS_PER_HOUR )
116
- nanoOfDay = nanoOfDay . subtract ( hour . multiply ( NANOS_PER_HOUR ) )
117
-
118
- const minute = nanoOfDay . div ( NANOS_PER_MINUTE )
119
- nanoOfDay = nanoOfDay . subtract ( minute . multiply ( NANOS_PER_MINUTE ) )
120
-
121
- const second = nanoOfDay . div ( NANOS_PER_SECOND )
122
- const nanosecond = nanoOfDay . subtract ( second . multiply ( NANOS_PER_SECOND ) )
123
-
124
- return new LocalTime ( hour , minute , second , nanosecond )
125
- }
126
-
127
106
/**
128
107
* Converts given local date time into a single integer representing this same time in epoch seconds UTC.
129
108
* @param {Integer|number|string } year the year of the local date-time to convert.
@@ -149,30 +128,6 @@ export function localDateTimeToEpochSecond (
149
128
return epochDay . multiply ( SECONDS_PER_DAY ) . add ( localTimeSeconds )
150
129
}
151
130
152
- /**
153
- * Converts given epoch second and nanosecond adjustment into a local date time object.
154
- * @param {Integer|number|string } epochSecond the epoch second to use.
155
- * @param {Integer|number|string } nano the nanosecond to use.
156
- * @return {LocalDateTime } the local date time representing given epoch second and nano.
157
- */
158
- export function epochSecondAndNanoToLocalDateTime ( epochSecond , nano ) {
159
- const epochDay = floorDiv ( epochSecond , SECONDS_PER_DAY )
160
- const secondsOfDay = floorMod ( epochSecond , SECONDS_PER_DAY )
161
- const nanoOfDay = secondsOfDay . multiply ( NANOS_PER_SECOND ) . add ( nano )
162
-
163
- const localDate = epochDayToDate ( epochDay )
164
- const localTime = nanoOfDayToLocalTime ( nanoOfDay )
165
- return new LocalDateTime (
166
- localDate . year ,
167
- localDate . month ,
168
- localDate . day ,
169
- localTime . hour ,
170
- localTime . minute ,
171
- localTime . second ,
172
- localTime . nanosecond
173
- )
174
- }
175
-
176
131
/**
177
132
* Converts given local date into a single integer representing it's epoch day.
178
133
* @param {Integer|number|string } year the year of the local date to convert.
@@ -220,69 +175,6 @@ export function dateToEpochDay (year, month, day) {
220
175
return epochDay . subtract ( DAYS_0000_TO_1970 )
221
176
}
222
177
223
- /**
224
- * Converts given epoch day to a local date.
225
- * @param {Integer|number|string } epochDay the epoch day to convert.
226
- * @return {Date } the date representing the epoch day in years, months and days.
227
- */
228
- export function epochDayToDate ( epochDay ) {
229
- epochDay = int ( epochDay )
230
-
231
- let zeroDay = epochDay . add ( DAYS_0000_TO_1970 ) . subtract ( 60 )
232
- let adjust = int ( 0 )
233
- if ( zeroDay . lessThan ( 0 ) ) {
234
- const adjustCycles = zeroDay
235
- . add ( 1 )
236
- . div ( DAYS_PER_400_YEAR_CYCLE )
237
- . subtract ( 1 )
238
- adjust = adjustCycles . multiply ( 400 )
239
- zeroDay = zeroDay . add ( adjustCycles . multiply ( - DAYS_PER_400_YEAR_CYCLE ) )
240
- }
241
- let year = zeroDay
242
- . multiply ( 400 )
243
- . add ( 591 )
244
- . div ( DAYS_PER_400_YEAR_CYCLE )
245
- let dayOfYearEst = zeroDay . subtract (
246
- year
247
- . multiply ( 365 )
248
- . add ( year . div ( 4 ) )
249
- . subtract ( year . div ( 100 ) )
250
- . add ( year . div ( 400 ) )
251
- )
252
- if ( dayOfYearEst . lessThan ( 0 ) ) {
253
- year = year . subtract ( 1 )
254
- dayOfYearEst = zeroDay . subtract (
255
- year
256
- . multiply ( 365 )
257
- . add ( year . div ( 4 ) )
258
- . subtract ( year . div ( 100 ) )
259
- . add ( year . div ( 400 ) )
260
- )
261
- }
262
- year = year . add ( adjust )
263
- const marchDayOfYear = dayOfYearEst
264
-
265
- const marchMonth = marchDayOfYear
266
- . multiply ( 5 )
267
- . add ( 2 )
268
- . div ( 153 )
269
- const month = marchMonth
270
- . add ( 2 )
271
- . modulo ( 12 )
272
- . add ( 1 )
273
- const day = marchDayOfYear
274
- . subtract (
275
- marchMonth
276
- . multiply ( 306 )
277
- . add ( 5 )
278
- . div ( 10 )
279
- )
280
- . add ( 1 )
281
- year = year . add ( marchMonth . div ( 10 ) )
282
-
283
- return new Date ( year , month , day )
284
- }
285
-
286
178
/**
287
179
* Format given duration to an ISO 8601 string.
288
180
* @param {Integer|number|string } months the number of months.
@@ -529,7 +421,7 @@ function isLeapYear (year) {
529
421
* @param {Integer|number|string } y the divisor.
530
422
* @return {Integer } the result.
531
423
*/
532
- function floorDiv ( x , y ) {
424
+ export function floorDiv ( x , y ) {
533
425
x = int ( x )
534
426
y = int ( y )
535
427
@@ -545,7 +437,7 @@ function floorDiv (x, y) {
545
437
* @param {Integer|number|string } y the divisor.
546
438
* @return {Integer } the result.
547
439
*/
548
- function floorMod ( x , y ) {
440
+ export function floorMod ( x , y ) {
549
441
x = int ( x )
550
442
y = int ( y )
551
443
0 commit comments