Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/test/common/date.test.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { strictEqual } from 'assert';
7
import { fromNow, fromNowByDay, getDurationString, safeIntl } from '../../common/date.js';
8
import { ensureNoDisposablesAreLeakedInTestSuite } from './utils.js';
9
import { LANGUAGE_DEFAULT } from '../../common/platform.js';
10
11
suite('Date', () => {
12
ensureNoDisposablesAreLeakedInTestSuite();
13
14
suite('fromNow', () => {
15
test('appendAgoLabel', () => {
16
strictEqual(fromNow(Date.now() - 35000), '35 secs');
17
strictEqual(fromNow(Date.now() - 35000, false), '35 secs');
18
strictEqual(fromNow(Date.now() - 35000, true), '35 secs ago');
19
});
20
test('useFullTimeWords', () => {
21
strictEqual(fromNow(Date.now() - 35000), '35 secs');
22
strictEqual(fromNow(Date.now() - 35000, undefined, false), '35 secs');
23
strictEqual(fromNow(Date.now() - 35000, undefined, true), '35 seconds');
24
});
25
test('disallowNow', () => {
26
strictEqual(fromNow(Date.now() - 5000), 'now');
27
strictEqual(fromNow(Date.now() - 5000, undefined, undefined, false), 'now');
28
strictEqual(fromNow(Date.now() - 5000, undefined, undefined, true), '5 secs');
29
});
30
});
31
32
suite('fromNowByDay', () => {
33
test('today', () => {
34
const now = new Date();
35
strictEqual(fromNowByDay(now), 'Today');
36
});
37
test('yesterday', () => {
38
const yesterday = new Date();
39
yesterday.setDate(yesterday.getDate() - 1);
40
yesterday.setHours(12);
41
strictEqual(fromNowByDay(yesterday), 'Yesterday');
42
});
43
test('daysAgo', () => {
44
const daysAgo = new Date();
45
daysAgo.setDate(daysAgo.getDate() - 5);
46
daysAgo.setHours(daysAgo.getHours() - 2); // 2 hours further to avoid DST issues
47
strictEqual(fromNowByDay(daysAgo, true), '5 days ago');
48
});
49
});
50
51
suite('getDurationString', () => {
52
test('basic', () => {
53
strictEqual(getDurationString(1), '1ms');
54
strictEqual(getDurationString(999), '999ms');
55
strictEqual(getDurationString(1000), '1s');
56
strictEqual(getDurationString(1000 * 60 - 1), '59.999s');
57
strictEqual(getDurationString(1000 * 60), '1 mins');
58
strictEqual(getDurationString(1000 * 60 * 60 - 1), '60 mins');
59
strictEqual(getDurationString(1000 * 60 * 60), '1 hrs');
60
strictEqual(getDurationString(1000 * 60 * 60 * 24 - 1), '24 hrs');
61
strictEqual(getDurationString(1000 * 60 * 60 * 24), '1 days');
62
});
63
test('useFullTimeWords', () => {
64
strictEqual(getDurationString(1, true), '1 milliseconds');
65
strictEqual(getDurationString(999, true), '999 milliseconds');
66
strictEqual(getDurationString(1000, true), '1 seconds');
67
strictEqual(getDurationString(1000 * 60 - 1, true), '59.999 seconds');
68
strictEqual(getDurationString(1000 * 60, true), '1 minutes');
69
strictEqual(getDurationString(1000 * 60 * 60 - 1, true), '60 minutes');
70
strictEqual(getDurationString(1000 * 60 * 60, true), '1 hours');
71
strictEqual(getDurationString(1000 * 60 * 60 * 24 - 1, true), '24 hours');
72
strictEqual(getDurationString(1000 * 60 * 60 * 24, true), '1 days');
73
});
74
75
suite('safeIntl', () => {
76
test('Collator fallback', () => {
77
const collator = safeIntl.Collator('en_IT').value;
78
const comparison = collator.compare('a', 'b');
79
strictEqual(comparison, -1);
80
});
81
82
test('Locale fallback', () => {
83
const locale = safeIntl.Locale('en_IT').value;
84
strictEqual(locale.baseName, LANGUAGE_DEFAULT);
85
});
86
});
87
});
88
});
89
90