forked from irinazheltisheva/vscode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate.ts
More file actions
131 lines (117 loc) · 4.12 KB
/
Copy pathdate.ts
File metadata and controls
131 lines (117 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { pad } from './strings';
import { localize } from 'vs/nls';
const minute = 60;
const hour = minute * 60;
const day = hour * 24;
const week = day * 7;
const month = day * 30;
const year = day * 365;
export function fromNow(date: number | Date, appendAgoLabel?: boolean): string {
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 (seconds < 30) {
return localize('date.fromNow.now', 'now');
}
let value: number;
if (seconds < minute) {
value = seconds;
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.seconds.singular.ago', '{0} sec ago', value)
: localize('date.fromNow.seconds.plural.ago', '{0} secs ago', value);
} else {
return value === 1
? localize('date.fromNow.seconds.singular', '{0} sec', value)
: localize('date.fromNow.seconds.plural', '{0} secs', value);
}
}
if (seconds < hour) {
value = Math.floor(seconds / minute);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.minutes.singular.ago', '{0} min ago', value)
: localize('date.fromNow.minutes.plural.ago', '{0} mins ago', value);
} else {
return value === 1
? localize('date.fromNow.minutes.singular', '{0} min', value)
: localize('date.fromNow.minutes.plural', '{0} mins', value);
}
}
if (seconds < day) {
value = Math.floor(seconds / hour);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.hours.singular.ago', '{0} hr ago', value)
: localize('date.fromNow.hours.plural.ago', '{0} hrs ago', value);
} else {
return value === 1
? localize('date.fromNow.hours.singular', '{0} hr', value)
: localize('date.fromNow.hours.plural', '{0} hrs', value);
}
}
if (seconds < week) {
value = Math.floor(seconds / day);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.days.singular.ago', '{0} day ago', value)
: localize('date.fromNow.days.plural.ago', '{0} days 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.floor(seconds / week);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.weeks.singular.ago', '{0} wk ago', value)
: localize('date.fromNow.weeks.plural.ago', '{0} wks ago', value);
} else {
return value === 1
? localize('date.fromNow.weeks.singular', '{0} wk', value)
: localize('date.fromNow.weeks.plural', '{0} wks', value);
}
}
if (seconds < year) {
value = Math.floor(seconds / month);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.months.singular.ago', '{0} mo ago', value)
: localize('date.fromNow.months.plural.ago', '{0} mos ago', value);
} else {
return value === 1
? localize('date.fromNow.months.singular', '{0} mo', value)
: localize('date.fromNow.months.plural', '{0} mos', value);
}
}
value = Math.floor(seconds / year);
if (appendAgoLabel) {
return value === 1
? localize('date.fromNow.years.singular.ago', '{0} yr ago', value)
: localize('date.fromNow.years.plural.ago', '{0} yrs ago', value);
} else {
return value === 1
? localize('date.fromNow.years.singular', '{0} yr', value)
: localize('date.fromNow.years.plural', '{0} yrs', value);
}
}
export function toLocalISOString(date: Date): string {
return date.getFullYear() +
'-' + pad(date.getMonth() + 1, 2) +
'-' + pad(date.getDate(), 2) +
'T' + pad(date.getHours(), 2) +
':' + pad(date.getMinutes(), 2) +
':' + pad(date.getSeconds(), 2) +
'.' + (date.getMilliseconds() / 1000).toFixed(3).slice(2, 5) +
'Z';
}