src/vs/base/common/date.ts
317 LOC · 156 covered · 161 uncovered · 53 ranges · 3570 concepts · 18 introducers · 1919 tests
File neighbourhood
The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.
Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file
In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.
Graph controls are ready.
Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.
/*---------------------------------------------------------------------------------------------
date.ts ×10
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { localize } from '../../nls.js';
import { Lazy } from './lazy.js';
import { LANGUAGE_DEFAULT } from './platform.js';
const minute = 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
const year = day * 365;
/**
* Create a localized difference of the time between now and the specified date.
* @param date The date to generate the difference from.
* @param appendAgoLabel Whether to append the " ago" to the end.
* @param useFullTimeWords Whether to use full words (eg. seconds) instead of
* shortened (eg. secs).
* @param disallowNow Whether to disallow the string "now" when the difference
* is less than 30 seconds.
*/
export function fromNow(date: number | Date, appendAgoLabel?: boolean, useFullTimeWords?: boolean, disallowNow?: boolean): string {
return localize('date.fromNow.unknown', 'unknown');
}
if (typeof date !== 'number') {
date = date.getTime();
}
const seconds = Math.round((new Date().getTime() - date) / 1000);
if (seconds < -30) {
return localize('date.fromNow.in', 'in {0}', fromNow(new Date().getTime() + seconds * 1000, false));
}
if (!disallowNow && seconds < 30) {
}
let value: number;
if (seconds < minute) {
if (appendAgoLabel) {
return useFullTimeWords
? localize('date.fromNow.seconds.singular.ago.fullWord', '{0} second ago', value)
: localize('date.fromNow.seconds.singular.ago', '{0} sec ago', value);
return useFullTimeWords
? localize('date.fromNow.seconds.plural.ago.fullWord', '{0} seconds ago', value)
}
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.seconds.singular.fullWord', '{0} second', value)
: localize('date.fromNow.seconds.singular', '{0} sec', value);
return useFullTimeWords
}
}
}
if (seconds < hour) {
value = Math.round(seconds / minute);
if (appendAgoLabel) {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.minutes.singular.ago.fullWord', '{0} minute ago', value)
: localize('date.fromNow.minutes.singular.ago', '{0} min ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.minutes.plural.ago.fullWord', '{0} minutes ago', value)
: localize('date.fromNow.minutes.plural.ago', '{0} mins ago', value);
}
} else {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.minutes.singular.fullWord', '{0} minute', value)
: localize('date.fromNow.minutes.singular', '{0} min', value);
} else {
return useFullTimeWords
? localize('date.fromNow.minutes.plural.fullWord', '{0} minutes', value)
: localize('date.fromNow.minutes.plural', '{0} mins', value);
}
}
}
if (seconds < day) {
value = Math.round(seconds / hour);
if (appendAgoLabel) {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.hours.singular.ago.fullWord', '{0} hour ago', value)
: localize('date.fromNow.hours.singular.ago', '{0} hr ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.hours.plural.ago.fullWord', '{0} hours ago', value)
: localize('date.fromNow.hours.plural.ago', '{0} hrs ago', value);
}
} else {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.hours.singular.fullWord', '{0} hour', value)
: localize('date.fromNow.hours.singular', '{0} hr', value);
} else {
return useFullTimeWords
? localize('date.fromNow.hours.plural.fullWord', '{0} hours', value)
: localize('date.fromNow.hours.plural', '{0} hrs', value);
}
}
}
if (seconds < week) {
value = Math.round(seconds / day);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.days.singular.ago', '{0} day ago', value)
} else {
return value === 1
? localize('date.fromNow.days.singular', '{0} day', value)
: localize('date.fromNow.days.plural', '{0} days', value);
}
if (seconds < month) {
value = Math.round(seconds / week);
if (appendAgoLabel) {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.weeks.singular.ago.fullWord', '{0} week ago', value)
: localize('date.fromNow.weeks.singular.ago', '{0} wk ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.weeks.plural.ago.fullWord', '{0} weeks ago', value)
: localize('date.fromNow.weeks.plural.ago', '{0} wks ago', value);
}
} else {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.weeks.singular.fullWord', '{0} week', value)
: localize('date.fromNow.weeks.singular', '{0} wk', value);
} else {
return useFullTimeWords
? localize('date.fromNow.weeks.plural.fullWord', '{0} weeks', value)
: localize('date.fromNow.weeks.plural', '{0} wks', value);
}
}
}
if (seconds < year) {
value = Math.round(seconds / month);
if (appendAgoLabel) {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.months.singular.ago.fullWord', '{0} month ago', value)
: localize('date.fromNow.months.singular.ago', '{0} mo ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.months.plural.ago.fullWord', '{0} months ago', value)
: localize('date.fromNow.months.plural.ago', '{0} mos ago', value);
}
} else {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.months.singular.fullWord', '{0} month', value)
: localize('date.fromNow.months.singular', '{0} mo', value);
} else {
return useFullTimeWords
? localize('date.fromNow.months.plural.fullWord', '{0} months', value)
: localize('date.fromNow.months.plural', '{0} mos', value);
}
}
}
value = Math.round(seconds / year);
if (appendAgoLabel) {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.years.singular.ago.fullWord', '{0} year ago', value)
: localize('date.fromNow.years.singular.ago', '{0} yr ago', value);
} else {
return useFullTimeWords
? localize('date.fromNow.years.plural.ago.fullWord', '{0} years ago', value)
: localize('date.fromNow.years.plural.ago', '{0} yrs ago', value);
}
} else {
if (value === 1) {
return useFullTimeWords
? localize('date.fromNow.years.singular.fullWord', '{0} year', value)
: localize('date.fromNow.years.singular', '{0} yr', value);
} else {
return useFullTimeWords
? localize('date.fromNow.years.plural.fullWord', '{0} years', value)
: localize('date.fromNow.years.plural', '{0} yrs', value);
}
}
export function fromNowByDay(date: number | Date, appendAgoLabel?: boolean, useFullTimeWords?: boolean): string {
date = date.getTime();
}
const todayMidnightTime = new Date();
todayMidnightTime.setHours(0, 0, 0, 0);
const yesterdayMidnightTime = new Date(todayMidnightTime.getTime());
yesterdayMidnightTime.setDate(yesterdayMidnightTime.getDate() - 1);
if (date > todayMidnightTime.getTime()) {
}
if (date > yesterdayMidnightTime.getTime()) {
}
return fromNow(date, appendAgoLabel, useFullTimeWords);
}
/**
* Gets a readable duration with intelligent/lossy precision. For example "40ms" or "3.040s")
* @param ms The duration to get in milliseconds.
* @param useFullTimeWords Whether to use full words (eg. seconds) instead of
* shortened (eg. secs).
*/
export function getDurationString(ms: number, useFullTimeWords?: boolean) {
if (seconds < 1) {
return useFullTimeWords
if (seconds < minute) {
return useFullTimeWords
if (seconds < hour) {
return useFullTimeWords
if (seconds < day) {
return useFullTimeWords
return localize('duration.d', '{0} days', Math.round(ms / (1000 * day)));
}
export function toLocalISOString(date: Date): string {
'-' + String(date.getMonth() + 1).padStart(2, '0') +
'-' + String(date.getDate()).padStart(2, '0') +
'T' + String(date.getHours()).padStart(2, '0') +
':' + String(date.getMinutes()).padStart(2, '0') +
':' + String(date.getSeconds()).padStart(2, '0') +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
'Z';
}
export const safeIntl = {
DateTimeFormat(locales?: Intl.LocalesArgument, options?: Intl.DateTimeFormatOptions): Lazy<Intl.DateTimeFormat> {
return new Lazy(() => {
try {
return new Intl.DateTimeFormat(locales, options);
} catch {
return new Intl.DateTimeFormat(undefined, options);
}
});
},
Collator(locales?: Intl.LocalesArgument, options?: Intl.CollatorOptions): Lazy<Intl.Collator> {
date.ts ×10
try {
return new Intl.Collator(locales, options);
} catch {
}
},
Segmenter(locales?: Intl.LocalesArgument, options?: Intl.SegmenterOptions): Lazy<Intl.Segmenter> {
date.ts ×10
return new Lazy(() => {
try {
return new Intl.Segmenter(locales, options);
} catch {
return new Intl.Segmenter(undefined, options);
}
});
},
Locale(tag: Intl.Locale | string, options?: Intl.LocaleOptions): Lazy<Intl.Locale> {
date.ts ×10
try {
return new Intl.Locale(tag, options);
} catch {
return new Intl.Locale(LANGUAGE_DEFAULT, options);
}
});
},
NumberFormat(locales?: Intl.LocalesArgument, options?: Intl.NumberFormatOptions): Lazy<Intl.NumberFormat> {
date.ts ×10
return new Lazy(() => {
try {
return new Intl.NumberFormat(locales, options);
} catch {
return new Intl.NumberFormat(undefined, options);
}
});
}