8
8
isZonedDateTime ,
9
9
} from "../type-utils.js" ;
10
10
import type { GenericDateConstructor , Temporal } from "../types.js" ;
11
+ import { createDateFromClockTime } from "./_createDateFromClockTime.js" ;
11
12
12
13
function parseIsoString ( date : string ) {
13
14
const res = / ^ ( \d { 4 , } | [ + - ] \d { 6 } ) - ( \d { 2 } ) - ( \d { 2 } ) / . exec ( date ) ;
@@ -25,6 +26,40 @@ function parseIsoString(date: string) {
25
26
} ;
26
27
}
27
28
29
+ // function to bypass an enigmatic TypeScript error "could be instantiated with a different subtype of constraint"
30
+ function createDate < DateType extends Date > (
31
+ DateConstructor : GenericDateConstructor < DateType > | undefined ,
32
+ year : number ,
33
+ month : number ,
34
+ day = 1 ,
35
+ hour = 0 ,
36
+ minute = 0 ,
37
+ second = 0 ,
38
+ millisecond = 0 ,
39
+ ) {
40
+ return DateConstructor ?
41
+ createDateFromClockTime (
42
+ DateConstructor ,
43
+ year ,
44
+ month ,
45
+ day ,
46
+ hour ,
47
+ minute ,
48
+ second ,
49
+ millisecond ,
50
+ )
51
+ : createDateFromClockTime (
52
+ UTCDate ,
53
+ year ,
54
+ month ,
55
+ day ,
56
+ hour ,
57
+ minute ,
58
+ second ,
59
+ millisecond ,
60
+ ) ;
61
+ }
62
+
28
63
/**
29
64
* Returns `Date` which represents clock (local) time of given temporal object,
30
65
* dropping timezone and calendar information.
@@ -79,22 +114,22 @@ export function toDateFromClockTime<DateType extends Date>(
79
114
| Temporal . PlainMonthDay ,
80
115
DateConstructor ?: GenericDateConstructor < DateType > ,
81
116
) {
82
- const DateConstructorFunction = DateConstructor ?? UTCDate ;
83
117
if ( isPlainYearMonth ( dateTime ) ) {
84
118
const pd = dateTime . toPlainDate ( { day : 1 } ) . withCalendar ( "iso8601" ) ;
85
- return new DateConstructorFunction ( pd . year , pd . month - 1 , pd . day ) ;
119
+ return createDate ( DateConstructor , pd . year , pd . month , pd . day ) ;
86
120
}
87
121
if ( isPlainMonthDay ( dateTime ) ) {
88
122
if ( dateTime . calendarId === "iso8601" ) {
89
123
const pd = dateTime . toPlainDate ( { year : 1972 } ) ;
90
- return new DateConstructorFunction ( pd . year , pd . month - 1 , pd . day ) ;
124
+ return createDate ( DateConstructor , pd . year , pd . month , pd . day ) ;
91
125
}
92
126
const { year, month, day } = parseIsoString ( dateTime . toString ( ) ) ;
93
- return new DateConstructorFunction ( year , month - 1 , day ) ;
127
+ return createDate ( DateConstructor , year , month , day ) ;
94
128
}
95
129
if ( isPlainTime ( dateTime ) ) {
96
130
// Set default date to 2000-01-01
97
- return new DateConstructorFunction (
131
+ return createDate (
132
+ DateConstructor ,
98
133
2000 ,
99
134
0 ,
100
135
1 ,
@@ -109,9 +144,10 @@ export function toDateFromClockTime<DateType extends Date>(
109
144
dateTime . toPlainDateTime ( ) . withCalendar ( "iso8601" )
110
145
: isPlainDate ( dateTime ) ? dateTime . toPlainDateTime ( ) . withCalendar ( "iso8601" )
111
146
: dateTime . withCalendar ( "iso8601" ) ;
112
- return new DateConstructorFunction (
147
+ return createDate (
148
+ DateConstructor ,
113
149
plainDateTime . year ,
114
- plainDateTime . month - 1 ,
150
+ plainDateTime . month ,
115
151
plainDateTime . day ,
116
152
plainDateTime . hour ,
117
153
plainDateTime . minute ,
0 commit comments