Skip to content

Commit

Permalink
fix: Remove microsecond conversion (#1110)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S authored Jan 5, 2025
1 parent 7dfc54e commit 91d10cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion extensions/timestamp-hover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Display the ISO time and date while hovering over timestamps.

- Automatically converts timestamps from seconds, milliseconds, or microseconds to ISO Time.
- Automatically converts timestamps from seconds or milliseconds to ISO Time.
- Does not convert timestamps before 1980 to avoid being noisy.

![Example](./images/Screenshot1.png)
38 changes: 22 additions & 16 deletions extensions/timestamp-hover/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
const options: FormatTimestampOptions = {
minDate: new Date('1980-01-01'),
}
};

const hoverProvider = vscode.languages.registerHoverProvider({ scheme: '*', language: '*' }, {
provideHover(document, position) {
const range = document.getWordRangeAtPosition(position);
if (!range) {
return undefined;
}
const word = document.getText(range);
const hoverMessage = formatTimestamp(word, options);
return hoverMessage ? new vscode.Hover(`_${hoverMessage}_`) : undefined;
}
});
const hoverProvider = vscode.languages.registerHoverProvider(
{ scheme: '*', language: '*' },
{
provideHover(document, position) {
const range = document.getWordRangeAtPosition(position);
if (!range) {
return undefined;
}
const word = document.getText(range);
const hoverMessage = formatTimestamp(word, options);
return hoverMessage ? new vscode.Hover(`_${hoverMessage}_`) : undefined;
},
},
);
context.subscriptions.push(hoverProvider);
}

Expand All @@ -39,15 +42,18 @@ function formatTimestamp(text: string, options: FormatTimestampOptions): string
timestamp *= 1000;
}
if (timestamp > 1e14) {
// microseconds to milliseconds
timestamp /= 1000;
// too large
return undefined;
}
if (timestamp < options.minDate.getTime()) {
return undefined;
}
return new Date(timestamp).toISOString();
try {
return new Date(timestamp).toISOString();
} catch {
return undefined;
}
}


// this method is called when your extension is deactivated
export function deactivate() {}

0 comments on commit 91d10cc

Please sign in to comment.