Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DateTimeImpl should work with any instances of type DateTime #79

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion src/dateTime/dateTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,29 @@ function absRound(v: number) {
return Math.round(sign * v) * sign;
}

function valueOf(o: unknown): number {
if (o === null || o === undefined) {
return NaN;
}

if (typeof o === 'string') {
return NaN;
}

if (typeof o === 'number' || typeof o === 'bigint') {
return Number(o);
}

if (typeof o === 'object') {
const v = o.valueOf();
if (typeof v === 'number' || typeof v === 'bigint') {
return Number(v);
}
}

return NaN;
}

function createDateTime({
ts,
timeZone,
Expand All @@ -808,7 +831,12 @@ function getTimestamp(
): [ts: number, offset: number] {
let ts: number;
let offset: number | undefined;
if (isDateTime(input) || typeof input === 'number' || input instanceof Date) {
if (
isDateTime(input) ||
typeof input === 'number' ||
input instanceof Date ||
!isNaN(valueOf(input))
) {
ts = Number(input);
} else if (input === null || input === undefined) {
ts = Date.now();
Expand Down