Commit c828a45 1 parent 6bfbe05 commit c828a45 Copy full SHA for c828a45
File tree 4 files changed +52
-0
lines changed
4 files changed +52
-0
lines changed Original file line number Diff line number Diff line change @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
### Added
11
11
12
12
- ` fromJulianDate ` , ` fromModifiedJulianDate ` , ` modifiedJulianDate ` functions
13
+ - ` toDateFromExactTime ` function
13
14
14
15
### Changed
15
16
Original file line number Diff line number Diff line change @@ -38,4 +38,5 @@ export { startOfMonth } from "./startOfMonth.js";
38
38
export { startOfSecond } from "./startOfSecond.js" ;
39
39
export { startOfYear } from "./startOfYear.js" ;
40
40
export { toDateFromClockTime } from "./toDateFromClockTime.js" ;
41
+ export { toDateFromExactTime } from "./toDateFromExactTime.js" ;
41
42
export { toTemporalFromClockTime } from "./toTemporalFromClockTime.js" ;
Original file line number Diff line number Diff line change
1
+ import { UTCDate } from "@date-fns/utc" ;
2
+ import { Temporal } from "@js-temporal/polyfill" ;
3
+ import { expect , test } from "vitest" ;
4
+
5
+ import { toDateFromExactTime } from "./toDateFromExactTime.js" ;
6
+
7
+ test ( "Instant" , ( ) => {
8
+ expect (
9
+ toDateFromExactTime ( Temporal . Instant . from ( "2024-01-01T01:23:45.678Z" ) ) ,
10
+ ) . toEqual ( new Date ( "2024-01-01T01:23:45.678Z" ) ) ;
11
+ } ) ;
12
+
13
+ test ( "units smaller than millisecond" , ( ) => {
14
+ expect (
15
+ toDateFromExactTime ( Temporal . Instant . from ( "2024-01-01T00:00:00.0009Z" ) ) ,
16
+ ) . toEqual ( new Date ( "2024-01-01T00:00:00Z" ) ) ;
17
+ } ) ;
18
+
19
+ test ( "ZonedDateTime" , ( ) => {
20
+ expect (
21
+ toDateFromExactTime (
22
+ Temporal . ZonedDateTime . from ( "2024-01-01T00:00:00+09:00[Asia/Tokyo]" ) ,
23
+ ) ,
24
+ ) . toEqual ( new Date ( "2023-12-31T15:00:00Z" ) ) ;
25
+ } ) ;
26
+
27
+ test ( "DateConstructor option" , ( ) => {
28
+ const result = toDateFromExactTime (
29
+ Temporal . ZonedDateTime . from ( "2024-01-01T00:00:00+09:00[Asia/Tokyo]" ) ,
30
+ UTCDate ,
31
+ ) ;
32
+ expect ( result ) . toEqual ( new UTCDate ( "2023-12-31T15:00:00Z" ) ) ;
33
+ expect ( result ) . toBeInstanceOf ( UTCDate ) ;
34
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import type { GenericDateConstructor , Temporal } from "../types.js" ;
2
+
3
+ /**
4
+ * Returns `Date` which represents exact time of the given temporal object.
5
+ *
6
+ * @param dateTime Temporal object which includes exact time info
7
+ * @param DateConstructor `Date` or extended class for return type, default is `Date`
8
+ * @returns `Date` (or its subclass) object which represents the exact time of the given Temporal object
9
+ */
10
+ export function toDateFromExactTime < DateType extends Date = Date > (
11
+ dateTime : Temporal . ZonedDateTime | Temporal . Instant ,
12
+ DateConstructor ?: GenericDateConstructor < DateType > ,
13
+ ) : DateType {
14
+ const DateConstructorFunction = DateConstructor ?? Date ;
15
+ return new DateConstructorFunction ( dateTime . epochMilliseconds ) as DateType ;
16
+ }
You can’t perform that action at this time.
0 commit comments