@@ -20,13 +20,14 @@ import { Temporal } from '..';
2020import { DateTimeFormat } from './intl' ;
2121import type { ZonedDateTimeParams as Params , ZonedDateTimeReturn as Return } from './internaltypes' ;
2222
23- import bigInt from 'big-integer' ;
23+ import JSBI from 'jsbi' ;
24+ import { BILLION , MILLION , THOUSAND , ZERO } from './ecmascript' ;
2425
2526const ArrayPrototypePush = Array . prototype . push ;
2627
2728export class ZonedDateTime implements Temporal . ZonedDateTime {
2829 constructor (
29- epochNanosecondsParam : bigInt . BigInteger ,
30+ epochNanosecondsParam : bigint | JSBI ,
3031 timeZoneParam : Temporal . TimeZoneProtocol | string ,
3132 calendarParam : Temporal . CalendarProtocol | string = ES . GetISO8601Calendar ( )
3233 ) {
@@ -102,21 +103,21 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
102103 get epochSeconds ( ) : Return [ 'epochSeconds' ] {
103104 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
104105 const value = GetSlot ( this , EPOCHNANOSECONDS ) ;
105- return + value . divide ( 1e9 ) ;
106+ return JSBI . toNumber ( JSBI . divide ( value , BILLION ) ) ;
106107 }
107108 get epochMilliseconds ( ) : Return [ 'epochMilliseconds' ] {
108109 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
109110 const value = GetSlot ( this , EPOCHNANOSECONDS ) ;
110- return + value . divide ( 1e6 ) ;
111+ return JSBI . toNumber ( JSBI . divide ( value , MILLION ) ) ;
111112 }
112113 get epochMicroseconds ( ) : Return [ 'epochMicroseconds' ] {
113114 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
114115 const value = GetSlot ( this , EPOCHNANOSECONDS ) ;
115- return bigIntIfAvailable ( value . divide ( 1e3 ) ) ;
116+ return ES . ToBigIntExternal ( JSBI . divide ( value , THOUSAND ) ) ;
116117 }
117118 get epochNanoseconds ( ) : Return [ 'epochNanoseconds' ] {
118119 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
119- return bigIntIfAvailable ( GetSlot ( this , EPOCHNANOSECONDS ) ) ;
120+ return ES . ToBigIntExternal ( GetSlot ( this , EPOCHNANOSECONDS ) ) ;
120121 }
121122 get dayOfWeek ( ) : Return [ 'dayOfWeek' ] {
122123 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
@@ -143,7 +144,7 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
143144 const timeZone = GetSlot ( this , TIME_ZONE ) ;
144145 const todayNs = GetSlot ( ES . BuiltinTimeZoneGetInstantFor ( timeZone , today , 'compatible' ) , EPOCHNANOSECONDS ) ;
145146 const tomorrowNs = GetSlot ( ES . BuiltinTimeZoneGetInstantFor ( timeZone , tomorrow , 'compatible' ) , EPOCHNANOSECONDS ) ;
146- return tomorrowNs . subtract ( todayNs ) . toJSNumber ( ) / 3.6e12 ;
147+ return JSBI . toNumber ( JSBI . subtract ( tomorrowNs , todayNs ) ) / 3.6e12 ;
147148 }
148149 get daysInWeek ( ) : Return [ 'daysInWeek' ] {
149150 if ( ! ES . IsTemporalZonedDateTime ( this ) ) throw new TypeError ( 'invalid receiver' ) ;
@@ -622,8 +623,8 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
622623 const dtStart = new DateTime ( GetSlot ( dt , ISO_YEAR ) , GetSlot ( dt , ISO_MONTH ) , GetSlot ( dt , ISO_DAY ) , 0 , 0 , 0 , 0 , 0 , 0 ) ;
623624 const instantStart = ES . BuiltinTimeZoneGetInstantFor ( timeZone , dtStart , 'compatible' ) ;
624625 const endNs = ES . AddZonedDateTime ( instantStart , timeZone , calendar , 0 , 0 , 0 , 1 , 0 , 0 , 0 , 0 , 0 , 0 ) ;
625- const dayLengthNs = endNs . subtract ( GetSlot ( instantStart , EPOCHNANOSECONDS ) ) ;
626- if ( dayLengthNs . isZero ( ) ) {
626+ const dayLengthNs = JSBI . subtract ( endNs , JSBI . BigInt ( GetSlot ( instantStart , EPOCHNANOSECONDS ) ) ) ;
627+ if ( JSBI . equal ( dayLengthNs , ZERO ) ) {
627628 throw new RangeError ( 'cannot round a ZonedDateTime in a calendar with zero-length days' ) ;
628629 }
629630 ( { year, month, day, hour, minute, second, millisecond, microsecond, nanosecond } = ES . RoundISODateTime (
@@ -641,7 +642,7 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
641642 roundingMode ,
642643 // Days are guaranteed to be shorter than Number.MAX_SAFE_INTEGER
643644 // (which can hold up to 104 days in nanoseconds)
644- dayLengthNs . toJSNumber ( )
645+ JSBI . toNumber ( dayLengthNs )
645646 ) ) ;
646647
647648 // Now reset all DateTime fields but leave the TimeZone. The offset will
@@ -675,7 +676,7 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
675676 const other = ES . ToTemporalZonedDateTime ( otherParam ) ;
676677 const one = GetSlot ( this , EPOCHNANOSECONDS ) ;
677678 const two = GetSlot ( other , EPOCHNANOSECONDS ) ;
678- if ( ! bigInt ( one ) . equals ( two ) ) return false ;
679+ if ( ! JSBI . equal ( JSBI . BigInt ( one ) , JSBI . BigInt ( two ) ) ) return false ;
679680 if ( ! ES . TimeZoneEquals ( GetSlot ( this , TIME_ZONE ) , GetSlot ( other , TIME_ZONE ) ) ) return false ;
680681 return ES . CalendarEquals ( GetSlot ( this , CALENDAR ) , GetSlot ( other , CALENDAR ) ) ;
681682 }
@@ -797,19 +798,15 @@ export class ZonedDateTime implements Temporal.ZonedDateTime {
797798 const two = ES . ToTemporalZonedDateTime ( twoParam ) ;
798799 const ns1 = GetSlot ( one , EPOCHNANOSECONDS ) ;
799800 const ns2 = GetSlot ( two , EPOCHNANOSECONDS ) ;
800- if ( bigInt ( ns1 ) . lesser ( ns2 ) ) return - 1 ;
801- if ( bigInt ( ns1 ) . greater ( ns2 ) ) return 1 ;
801+ if ( JSBI . lessThan ( JSBI . BigInt ( ns1 ) , JSBI . BigInt ( ns2 ) ) ) return - 1 ;
802+ if ( JSBI . greaterThan ( JSBI . BigInt ( ns1 ) , JSBI . BigInt ( ns2 ) ) ) return 1 ;
802803 return 0 ;
803804 }
804805 [ Symbol . toStringTag ] ! : 'Temporal.ZonedDateTime' ;
805806}
806807
807808MakeIntrinsicClass ( ZonedDateTime , 'Temporal.ZonedDateTime' ) ;
808809
809- function bigIntIfAvailable ( wrapper : bigInt . BigInteger | bigint ) {
810- return typeof ( globalThis as any ) . BigInt === 'undefined' ? wrapper : ( wrapper as any ) . value ;
811- }
812-
813810function dateTime ( zdt : Temporal . ZonedDateTime ) {
814811 return ES . BuiltinTimeZoneGetPlainDateTimeFor ( GetSlot ( zdt , TIME_ZONE ) , GetSlot ( zdt , INSTANT ) , GetSlot ( zdt , CALENDAR ) ) ;
815812}
0 commit comments