mirror of
https://github.com/ckaczor/vscode-gitlens.git
synced 2026-02-12 19:18:32 -05:00
Reworks date parsing, formatting etc for perf
Isolates moment.js
This commit is contained in:
33
src/system/date.ts
Normal file
33
src/system/date.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
'use strict';
|
||||
import * as moment from 'moment';
|
||||
|
||||
const MillisecondsPerMinute = 60000; // 60 * 1000
|
||||
const MillisecondsPerDay = 86400000; // 24 * 60 * 60 * 1000
|
||||
|
||||
export namespace Dates {
|
||||
|
||||
export interface IDateFormatter {
|
||||
fromNow: () => string;
|
||||
format: (format: string) => string;
|
||||
}
|
||||
|
||||
export function dateDaysFromNow(date: Date, now: number = Date.now()) {
|
||||
const startOfDayLeft = startOfDay(now);
|
||||
const startOfDayRight = startOfDay(date);
|
||||
|
||||
const timestampLeft = startOfDayLeft.getTime() - startOfDayLeft.getTimezoneOffset() * MillisecondsPerMinute;
|
||||
const timestampRight = startOfDayRight.getTime() - startOfDayRight.getTimezoneOffset() * MillisecondsPerMinute;
|
||||
|
||||
return Math.round((timestampLeft - timestampRight) / MillisecondsPerDay);
|
||||
}
|
||||
|
||||
export function startOfDay(date: Date | number) {
|
||||
const newDate = new Date(typeof date === 'number' ? date : date.getTime());
|
||||
newDate.setHours(0, 0, 0, 0);
|
||||
return newDate;
|
||||
}
|
||||
|
||||
export function toFormatter(date: Date): IDateFormatter {
|
||||
return moment(date);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user