Skip to content

Commit c828a45

Browse files
committed
Add toDateFromExactTime function
1 parent 6bfbe05 commit c828a45

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- `fromJulianDate`, `fromModifiedJulianDate`, `modifiedJulianDate` functions
13+
- `toDateFromExactTime` function
1314

1415
### Changed
1516

src/datetime/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,5 @@ export { startOfMonth } from "./startOfMonth.js";
3838
export { startOfSecond } from "./startOfSecond.js";
3939
export { startOfYear } from "./startOfYear.js";
4040
export { toDateFromClockTime } from "./toDateFromClockTime.js";
41+
export { toDateFromExactTime } from "./toDateFromExactTime.js";
4142
export { toTemporalFromClockTime } from "./toTemporalFromClockTime.js";
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
});

src/datetime/toDateFromExactTime.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

0 commit comments

Comments
 (0)