@@ -110,6 +110,117 @@ describe(DateTime, () => {
110110 } ) ;
111111 } ) ;
112112
113+ describe ( 'fromDate' , ( ) => {
114+ it . each ( [
115+ {
116+ year : 2024 ,
117+ month : 1 ,
118+ day : 15 ,
119+ hour : 14 ,
120+ minute : 30 ,
121+ second : 0 ,
122+ } ,
123+ {
124+ year : 2005 ,
125+ month : 9 ,
126+ day : 4 ,
127+ hour : 9 ,
128+ minute : 1 ,
129+ second : 2 ,
130+ } ,
131+ {
132+ year : 1970 ,
133+ month : 1 ,
134+ day : 1 ,
135+ hour : 0 ,
136+ minute : 0 ,
137+ second : 0 ,
138+ } ,
139+ ] ) (
140+ 'should create UTC datetime from Date $input' ,
141+ ( { year, month, day, hour, minute, second } ) => {
142+ const datetime = DateTime . fromDate (
143+ new Date ( Date . UTC ( year , month - 1 , day , hour , minute , second ) ) ,
144+ ) ;
145+
146+ expect ( datetime . year ) . toBe ( year ) ;
147+ expect ( datetime . month ) . toBe ( month ) ;
148+ expect ( datetime . day ) . toBe ( day ) ;
149+ expect ( datetime . time . hour ) . toBe ( hour ) ;
150+ expect ( datetime . time . minute ) . toBe ( minute ) ;
151+ expect ( datetime . time . second ) . toBe ( second ) ;
152+ expect ( datetime . time . utc ) . toBe ( true ) ;
153+ } ,
154+ ) ;
155+
156+ it ( 'should be convertible back to Date' , ( ) => {
157+ const originalDate = new Date ( Date . UTC ( 2024 , 5 , 20 , 10 , 30 , 45 ) ) ;
158+ const datetime = DateTime . fromDate ( originalDate ) ;
159+ const convertedDate = datetime . toDate ( ) ;
160+
161+ expect ( convertedDate . getTime ( ) ) . toBe ( originalDate . getTime ( ) ) ;
162+ } ) ;
163+ } ) ;
164+
165+ describe ( 'fromTimestamp' , ( ) => {
166+ it . each ( [
167+ {
168+ timestamp : Date . UTC ( 2024 , 0 , 15 , 14 , 30 , 0 ) ,
169+ expected : {
170+ year : 2024 ,
171+ month : 1 ,
172+ day : 15 ,
173+ hour : 14 ,
174+ minute : 30 ,
175+ second : 0 ,
176+ } ,
177+ } ,
178+ {
179+ timestamp : Date . UTC ( 2005 , 8 , 4 , 9 , 1 , 2 ) ,
180+ expected : {
181+ year : 2005 ,
182+ month : 9 ,
183+ day : 4 ,
184+ hour : 9 ,
185+ minute : 1 ,
186+ second : 2 ,
187+ } ,
188+ } ,
189+ {
190+ timestamp : 0 ,
191+ expected : {
192+ year : 1970 ,
193+ month : 1 ,
194+ day : 1 ,
195+ hour : 0 ,
196+ minute : 0 ,
197+ second : 0 ,
198+ } ,
199+ } ,
200+ ] ) (
201+ 'should create UTC datetime from timestamp $input' ,
202+ ( { timestamp, expected } ) => {
203+ const datetime = DateTime . fromTimestamp ( timestamp ) ;
204+
205+ expect ( datetime . year ) . toBe ( expected . year ) ;
206+ expect ( datetime . month ) . toBe ( expected . month ) ;
207+ expect ( datetime . day ) . toBe ( expected . day ) ;
208+ expect ( datetime . time . hour ) . toBe ( expected . hour ) ;
209+ expect ( datetime . time . minute ) . toBe ( expected . minute ) ;
210+ expect ( datetime . time . second ) . toBe ( expected . second ) ;
211+ expect ( datetime . time . utc ) . toBe ( true ) ;
212+ } ,
213+ ) ;
214+
215+ it ( 'should be convertible back to timestamp' , ( ) => {
216+ const originalTimestamp = Date . UTC ( 2024 , 5 , 20 , 10 , 30 , 45 ) ;
217+ const datetime = DateTime . fromTimestamp ( originalTimestamp ) ;
218+ const convertedTimestamp = datetime . toTimestamp ( ) ;
219+
220+ expect ( convertedTimestamp ) . toBe ( originalTimestamp ) ;
221+ } ) ;
222+ } ) ;
223+
113224 describe ( 'fromPlain' , ( ) => {
114225 it . each ( [
115226 {
0 commit comments