/
time.js
123 lines (110 loc) · 3.21 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
/** Add time spinner to prototype */
$.widget("ui.timespinner", $.ui.spinner, {
options: {
step: 60 * 1000,
page: 60
},
_parse: function(value) {
if (typeof value === 'string') {
if (Number(value) == value) {
return Number(value);
}
return +Globalize.parseDate(value);
}
return value;
},
_format: function(value) {
return Globalize.format(new Date(value), "t");
}
});
/**
* Uses date input and baseline selection to update the baseline display date
*/
function loadBaseline() {
var date = $("#date-picker").val()
var baseline = $("#baseline").val()
var baselineMillis = baseline * 7 * 24 * 3600 * 1000
var dateMillis = Date.parse(date)
var baselineDate = new Date(dateMillis - baselineMillis)
$("#baseline-display").html(baselineDate.toDateString())
}
/**
* Ensures that the query time is within the loaded data set
*/
function checkTime(collectionTime) {
var minTime = parseInt($("#min-time").html())
var maxTime = parseInt($("#max-time").html())
if (collectionTime < minTime || collectionTime > maxTime) {
throw "Out of range " + tickFormatter(minTime) + " to " + tickFormatter(maxTime)
}
}
/**
* Formats a collection time as a UTC string
*/
function tickFormatter(collectionTime) {
var date = new Date(collectionTimeToMillis(collectionTime));
return date.toUTCString();
}
/**
* Computes the baseline date given current date and delta
*/
function getBaselineDate(currentDate, deltaWeeks) {
date = new Date(currentDate.getTime());
date.setDate(date.getDate() - deltaWeeks * 7);
return date;
}
/**
* Extracts the date / time inputs and creates a Date object
*/
function getCurrentDate() {
date = $("#date-picker").val();
time = $("#spinner").val();
millis = Date.parse(date + " " + time + " GMT");
return new Date(millis)
}
/**
* Extracts the timeWindow from slider
*/
function getTimeWindowMillis() {
return collectionTimeToMillis($("#time-window").val());
}
/**
* Converts milliseconds to the collection time
*/
function millisToCollectionTime(millis) {
return Math.floor(millis / getFactor() / parseInt($("#time-window-size").html()));
}
/**
* Converts collection time to milliseconds
*/
function collectionTimeToMillis(collectionTime) {
return collectionTime * getFactor() * parseInt($("#time-window-size").html());
}
function getFactor() {
var unit = $("#time-window-unit").html();
if (unit == 'SECONDS') {
return 1000;
} else if (unit == 'MINUTES') {
return 60 * 1000;
} else if (unit == 'HOURS') {
return 60 * 60 * 1000;
} else if (unit == 'DAYS') {
return 24 * 60 * 60 * 1000;
}
return 1;
}
function formatDatePickerInput(date) {
month = date.getUTCMonth() + 1;
day = date.getUTCDate();
year = date.getUTCFullYear();
return month + '/' + day + '/' + year;
}
function formatSpinnerInput(date) {
hours = date.getUTCHours();
minutes = date.getUTCMinutes();
ampm = hours >= 12 ? 'pm' : 'am';
hours %= 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0' + minutes : minutes;
return hours + ':' + minutes + ' ' + ampm;
}