Debian Codegraph @

src/js/Rickshaw.Fixtures.Time.Local.js

raw
1 Rickshaw.namespace('Rickshaw.Fixtures.Time.Local');
2
3 Rickshaw.Fixtures.Time.Local = function() {
4
5 var self = this;
6
7 this.months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
8
9 this.units = [
10 {
11 name: 'decade',
12 seconds: 86400 * 365.25 * 10,
13 formatter: function(d) { return (parseInt(d.getFullYear() / 10, 10) * 10) }
14 }, {
15 name: 'year',
16 seconds: 86400 * 365.25,
17 formatter: function(d) { return d.getFullYear() }
18 }, {
19 name: 'month',
20 seconds: 86400 * 30.5,
21 formatter: function(d) { return self.months[d.getMonth()] }
22 }, {
23 name: 'week',
24 seconds: 86400 * 7,
25 formatter: function(d) { return self.formatDate(d) }
26 }, {
27 name: 'day',
28 seconds: 86400,
29 formatter: function(d) { return d.getDate() }
30 }, {
31 name: '6 hour',
32 seconds: 3600 * 6,
33 formatter: function(d) { return self.formatTime(d) }
34 }, {
35 name: 'hour',
36 seconds: 3600,
37 formatter: function(d) { return self.formatTime(d) }
38 }, {
39 name: '15 minute',
40 seconds: 60 * 15,
41 formatter: function(d) { return self.formatTime(d) }
42 }, {
43 name: 'minute',
44 seconds: 60,
45 formatter: function(d) { return d.getMinutes() }
46 }, {
47 name: '15 second',
48 seconds: 15,
49 formatter: function(d) { return d.getSeconds() + 's' }
50 }, {
51 name: 'second',
52 seconds: 1,
53 formatter: function(d) { return d.getSeconds() + 's' }
54 }, {
55 name: 'decisecond',
56 seconds: 1/10,
57 formatter: function(d) { return d.getMilliseconds() + 'ms' }
58 }, {
59 name: 'centisecond',
60 seconds: 1/100,
61 formatter: function(d) { return d.getMilliseconds() + 'ms' }
62 }
63 ];
64
65 this.unit = function(unitName) {
66 return this.units.filter( function(unit) { return unitName == unit.name } ).shift();
67 };
68
69 this.formatDate = function(d) {
70 return d3.time.format('%b %e')(d);
71 };
72
73 this.formatTime = function(d) {
74 return d.toString().match(/(\d+:\d+):/)[1];
75 };
76
77 this.ceil = function(time, unit) {
78
79 var date, floor, year;
80
81 if (unit.name == 'day') {
82
83 var nearFuture = new Date((time + unit.seconds - 1) * 1000);
84
85 var rounded = new Date(0);
86 rounded.setMilliseconds(0);
87 rounded.setSeconds(0);
88 rounded.setMinutes(0);
89 rounded.setHours(0);
90 rounded.setDate(nearFuture.getDate());
91 rounded.setMonth(nearFuture.getMonth());
92 rounded.setFullYear(nearFuture.getFullYear());
93
94 return rounded.getTime() / 1000;
95 }
96
97 if (unit.name == 'month') {
98
99 date = new Date(time * 1000);
100
101 floor = new Date(date.getFullYear(), date.getMonth()).getTime() / 1000;
102 if (floor == time) return time;
103
104 year = date.getFullYear();
105 var month = date.getMonth();
106
107 if (month == 11) {
108 month = 0;
109 year = year + 1;
110 } else {
111 month += 1;
112 }
113
114 return new Date(year, month).getTime() / 1000;
115 }
116
117 if (unit.name == 'year') {
118
119 date = new Date(time * 1000);
120
121 floor = new Date(date.getUTCFullYear(), 0).getTime() / 1000;
122 if (floor == time) return time;
123
124 year = date.getFullYear() + 1;
125
126 return new Date(year, 0).getTime() / 1000;
127 }
128
129 return Math.ceil(time / unit.seconds) * unit.seconds;
130 };
131 };