Skip to content

Commit

Permalink
fix: DateTimeImpl should work with any instances of type DateTime (#79)
Browse files Browse the repository at this point in the history
  • Loading branch information
ValeraS authored Oct 30, 2024
1 parent f819783 commit b552c5f
Showing 1 changed file with 29 additions and 1 deletion.
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

0 comments on commit b552c5f

Please sign in to comment.