Debian Codegraph @

src/js/Rickshaw.Fixtures.Time.js

raw
1 Rickshaw.namespace('Rickshaw.Fixtures.Time');
2
3 Rickshaw.Fixtures.Time = 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.getUTCFullYear() / 10, 10) * 10) }
14 }, {
15 name: 'year',
16 seconds: 86400 * 365.25,
17 formatter: function(d) { return d.getUTCFullYear() }
18 }, {
19 name: 'month',
20 seconds: 86400 * 30.5,
21 formatter: function(d) { return self.months[d.getUTCMonth()] }
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.getUTCDate() }
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.getUTCMinutes() }
46 }, {
47 name: '15 second',
48 seconds: 15,
49 formatter: function(d) { return d.getUTCSeconds() + 's' }
50 }, {
51 name: 'second',
52 seconds: 1,
53 formatter: function(d) { return d.getUTCSeconds() + 's' }
54 }, {
55 name: 'decisecond',
56 seconds: 1/10,
57 formatter: function(d) { return d.getUTCMilliseconds() + 'ms' }
58 }, {
59 name: 'centisecond',
60 seconds: 1/100,
61 formatter: function(d) { return d.getUTCMilliseconds() + '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.toUTCString().match(/(\d+:\d+):/)[1];
75 };
76
77 this.ceil = function(time, unit) {
78
79 var date, floor, year;
80
81 if (unit.name == 'month') {
82
83 date = new Date(time * 1000);
84
85 floor = Date.UTC(date.getUTCFullYear(), date.getUTCMonth()) / 1000;
86 if (floor == time) return time;
87
88 year = date.getUTCFullYear();
89 var month = date.getUTCMonth();
90
91 if (month == 11) {
92 month = 0;
93 year = year + 1;
94 } else {
95 month += 1;
96 }
97
98 return Date.UTC(year, month) / 1000;
99 }
100
101 if (unit.name == 'year') {
102
103 date = new Date(time * 1000);
104
105 floor = Date.UTC(date.getUTCFullYear(), 0) / 1000;
106 if (floor == time) return time;
107
108 year = date.getUTCFullYear() + 1;
109
110 return Date.UTC(year, 0) / 1000;
111 }
112
113 return Math.ceil(time / unit.seconds) * unit.seconds;
114 };
115 };