Skip to content

Commit

Permalink
Support milliseconds when constructing from a Date (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
prowe committed Oct 19, 2022
1 parent 30a5a7f commit e8ceb26
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
10 changes: 4 additions & 6 deletions src/IonTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,10 @@ export class Timestamp {
) {
if (dateOrLocalOffset instanceof Date) {
const date = dateOrLocalOffset;
const seconds = new Decimal(
// The coefficient is the total number of milliseconds as an integer
date.getSeconds() + date.getMilliseconds(),
// And the exponent is 0 to indicate the scale of that integer
0
);
const seconds =
date.getMilliseconds() === 0
? new Decimal(date.getSeconds(), 0)
: new Decimal(date.getSeconds() * 1000 + date.getMilliseconds(), -3);

this._localOffset = date.getTimezoneOffset() * -1;
this._year = date.getFullYear();
Expand Down
4 changes: 2 additions & 2 deletions test/IonTimestamp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ describe("Timestamp", () => {

const date2 = new Date(2000, 0, 1, 12, 30, 20, 20);
let timestamp3 = new ion.Timestamp(date2.getTimezoneOffset() * -1, 2000, 1, 1, 12, 30, new ion.Decimal(
40,
0
20020,
-3
));
let timestamp4 = new ion.Timestamp(date2);
assert.isTrue(timestamp3!.equals(timestamp4!));
Expand Down

0 comments on commit e8ceb26

Please sign in to comment.