-
Notifications
You must be signed in to change notification settings - Fork 11
/
time.js
291 lines (275 loc) · 9.02 KB
/
time.js
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
var AmpersandModel = require('ampersand-model');
var AmpersandColllection = require('ampersand-collection');
var moment = require('moment-timezone');
/*
* Time is grouped by truncating; the resolution is determined in util-time.getResolution()
* See [this table](http://momentjs.com/docs/#/durations/creating/) for accpetable values
* when using a crossfilter dataset.
*/
function refineUnits (units) {
if (units === 'minute' || units === 'minutes') {
units = 'seconds';
} else if (units === 'hour' || units === 'hours') {
units = 'minutes';
} else if (units === 'day' || units === 'days') {
units = 'hours';
} else if (units === 'week' || units === 'weeks') {
units = 'days';
} else if (units === 'month' || units === 'months') {
units = 'days';
} else if (units === 'year' || units === 'years') {
units = 'months';
}
return units;
}
function getFormat (units) {
var fmt;
if (units === 'seconds') {
fmt = 'mm:ss';
} else if (units === 'minutes') {
fmt = 'HH:mm';
} else if (units === 'hours') {
fmt = 'HH:00';
} else if (units === 'days') {
fmt = 'dddd do';
} else if (units === 'weeks') {
fmt = 'wo';
} else if (units === 'months') {
fmt = 'YY MMM';
} else if (units === 'years') {
fmt = 'YYYY';
}
return fmt;
}
function getDatetimeResolution (start, end) {
var humanized = end.from(start, true).split(' ');
var units = humanized[humanized.length - 1];
return refineUnits(units);
}
function getDurationResolution (min, max) {
var length = moment.duration(max.as('milliseconds') - min.as('milliseconds'), 'milliseconds');
var humanized = length.humanize().split(' ');
var units = humanized[humanized.length - 1];
return refineUnits(units);
}
var TimePart = AmpersandModel.extend({
props: {
/**
* The format string for momentjs
* @memberof! TimePart
* @type {string}
*/
momentFormat: ['string', true],
/**
* The format string for postgresql
* @memberof! TimePart
* @type {string}
*/
postgresFormat: ['string', true],
/**
* The human readable descprition of the datetime part
* @memberof! TimePart
* @type {string}
*/
description: ['string', true],
/**
* Data type after conversion: 'continuous', or 'categorial'
* @memberof! TimePart
* @type {string}
*/
type: ['string', true],
/**
* For continuous datetime parts (ie, day-of-year), the minimum value
* @memberof! TimePart
* @type {number}
*/
min: ['number', true, 0],
/**
* For continuous datetime parts (ie, day-of-year), the maximum value
* @memberof! TimePart
* @type {number}
*/
max: ['number', true, 1],
/**
* When true, calculate the minimum and maximum value from the
* original datetime limits. Used for continuous datetime parts (ie, year)
* @memberof! TimePart
* @type {boolean}
*/
calculate: ['boolean', true, false],
/**
* For categorial datetime parts (Mon, Tue, ..), the array of possible values
* @memberof! TimePart
* @type {String[]}
*/
groups: ['array']
}
});
var TimeParts = AmpersandColllection.extend({
model: TimePart,
indexes: ['description']
});
var timeParts = new TimeParts([
{ description: 'ISO8601', type: 'datetime', calculate: true },
{ postgresFormat: 'month', momentFormat: 'M', description: 'Month (1-12)', type: 'continuous', min: 1, max: 12 },
{ postgresFormat: 'quarter', momentFormat: 'Q', description: 'Quarter (1-4)', type: 'continuous', min: 1, max: 4 },
{ postgresFormat: 'day', momentFormat: 'D', description: 'Day of Month (1-31)', type: 'continuous', min: 1, max: 31 },
{ postgresFormat: 'doy', momentFormat: 'DDD', description: 'Day of Year (1-365)', type: 'continuous', min: 1, max: 365 },
{ postgresFormat: 'dow', momentFormat: 'd', description: 'Day of Week (0-6)', type: 'continuous', min: 0, max: 6 },
{ postgresFormat: 'isodow', momentFormat: 'E', description: 'Day of Week ISO (1-7)', type: 'continuous', min: 1, max: 7 },
{ postgresFormat: 'week', momentFormat: 'W', description: 'Week of Year ISO (1-53)', type: 'continuous', min: 1, max: 53 },
{ postgresFormat: 'year', momentFormat: 'Y', description: 'Year', type: 'continuous', calculate: true },
{ postgresFormat: 'hours', momentFormat: 'H', description: 'Hour (0-23)', type: 'continuous', min: 0, max: 23 },
{ postgresFormat: 'minute', momentFormat: 'm', description: 'Minute (0-59)', type: 'continuous', min: 0, max: 59 },
{ postgresFormat: 'second', momentFormat: 's', description: 'Second (0-59)', type: 'continuous', min: 0, max: 59 },
{ postgresFormat: 'milliseconds', momentFormat: 'SSS', description: 'Milliseconds (0-999)', type: 'continuous', min: 0, max: 999 },
{ postgresFormat: 'microseconds', momentFormat: 'SSSSSS', description: 'microseconds (0-999999)', type: 'continuous', min: 0, max: 999999 },
{ postgresFormat: 'epoch', momentFormat: 'X', description: 'Unix Timestamp', type: 'continuous', calculate: true },
{ postgresFormat: 'Mon', momentFormat: 'MMM', description: 'Month (Jan - Dec)', type: 'categorial', groups: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] },
{ postgresFormat: 'Month', momentFormat: 'MMMM', description: 'Month (January - December)', type: 'categorial', groups: ['January', 'Feburary', 'March', 'April', 'May', 'June', 'July', 'August', 'Septebmer', 'October', 'November', 'December'] },
{ postgresFormat: 'Dy', momentFormat: 'ddd', description: 'Day of Week (Sun-Sat)', type: 'categorial', groups: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'] },
{ postgresFormat: 'Day', momentFormat: 'dddd', description: 'Day of Week (Sunday-Saturday)', type: 'categorial', groups: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] },
{ postgresFormat: 'AM', momentFormat: 'A', description: 'AM/PM', type: 'categorial', groups: ['AM', 'PM'] }
]);
var DurationUnit = AmpersandModel.extend({
props: {
/**
* The descriptive name of the time unit
* @memberof! DurationUnit
* @type {string}
*/
description: ['string'],
/**
* Momentjs parsing format
* @memberof! DurationUnit
* @type {string}
*/
momentFormat: ['string'],
/**
* Postgres parsing format
* @memberof! DurationUnit
* @type {string}
*/
postgresFormat: ['string'],
/**
* Conversion factor to seconds
* @memberof! DurationUnit
* @type {string}
*/
seconds: ['number']
}
});
var DurationUnits = AmpersandColllection.extend({
indexes: ['description'],
model: DurationUnit
});
var durationUnits = new DurationUnits([
{
description: 'ISO8601',
seconds: 1
}, {
description: 'millenium',
momentFormat: 'millenium',
postgresFormat: 'millenium',
seconds: 100 * 365.25 * 24 * 60 * 60
}, {
description: 'century',
momentFormat: 'century',
postgresFormat: 'century',
seconds: 100 * 365.25 * 24 * 60 * 60
}, {
description: 'decades',
momentFormat: 'decades',
postgresFormat: 'decade',
seconds: 10 * 365.25 * 24 * 60 * 60
}, {
description: 'years',
momentFormat: 'years',
postgresFormat: 'year',
seconds: 365.25 * 24 * 60 * 60
}, {
description: 'quarters',
momentFormat: '',
postgresFormat: 'quarter',
seconds: 365.25 * 8 * 60 * 60
}, {
description: 'months',
momentFormat: 'months',
postgresFormat: 'month',
seconds: 30 * 24 * 60 * 60
}, {
description: 'weeks',
momentFormat: 'weeks',
postgresFormat: 'week',
seconds: 7 * 24 * 60 * 60
}, {
description: 'days',
momentFormat: 'days',
postgresFormat: 'day',
seconds: 24 * 60 * 60
}, {
description: 'hours',
momentFormat: 'hours',
postgresFormat: 'hour',
seconds: 60 * 60
}, {
description: 'minutes',
momentFormat: 'minutes',
postgresFormat: 'minute',
seconds: 60
}, {
description: 'seconds',
momentFormat: 'seconds',
postgresFormat: 'second',
seconds: 1
}, {
description: 'milliseconds',
momentFormat: 'milliseconds',
postgresFormat: 'milliseconds',
seconds: 0.001
}, {
description: 'microseconds',
momentFormat: 'microseconds',
postgresFormat: 'microseconds',
seconds: 0.000001
}
]);
var TimeZone = AmpersandModel.extend({
props: {
/**
* The descriptive name of the time zone
* @memberof! TimeZone
* @type {string}
*/
description: ['string'],
/**
* The time zone format
* @memberof! TimeZone
* @type {string}
*/
format: ['string']
}
});
var TimeZones = AmpersandColllection.extend({
indexes: ['description'],
model: TimeZone
});
var timeZones = new TimeZones();
timeZones.add({
description: 'ISO8601',
format: 'ISO8601'
});
moment.tz.names().forEach(function (tz) {
timeZones.add({
description: tz,
format: tz
});
});
module.exports = {
timeParts: timeParts,
timeZones: timeZones,
durationUnits: durationUnits,
getDatetimeResolution: getDatetimeResolution,
getDurationResolution: getDurationResolution,
getFormat: getFormat
};